class NonGrata::Privilege

Attributes

action[R]
resource[R]

Public Class Methods

new(action, resource) click to toggle source
# File lib/non_grata/privilege.rb, line 7
def initialize(action, resource)
    self.resource = resource
    self.action = action
end

Public Instance Methods

action=(value) click to toggle source
# File lib/non_grata/privilege.rb, line 33
def action=(value)
    @action = value.to_sym
end
applies_to?(resource_obj) click to toggle source

APPLIES_TO? @params:

resource - an resource to test if the privilege applies to

Will determen if the privilege applies to the given passed resource. This resourceect can be a class type, class instance, or a string.

# File lib/non_grata/privilege.rb, line 44
def applies_to?(resource_obj)
    resource_sym = nil
    case resource_obj
    when String
        resource_sym = resource_obj.underscore.to_sym
    when Symbol
        resource_sym = resource_obj
    when Class
        resource_sym = resource_obj.name.underscore.to_sym 
    else 
        resource_sym = resource_obj.class.name.underscore.to_sym
    end

    return (resource_sym == @resource)
end
resource=(value) click to toggle source

OBJECT SETTER accepts various types of values and retains them in string form this value can be set as a class type, a class instance, a symbol or a string. The value is internally stored as a string so if a class is passed the name of the class is stored. this allows privileges to be set on a class itself and checked using an instance of that class.

for example privlege.resource = User privilege.applies_to?(@user)

# File lib/non_grata/privilege.rb, line 26
def resource=(value)
    return @resource = value.name.underscore.to_sym if value.is_a? Class
    return @resource = value if value.is_a? Symbol
    return @resource = value.underscore.to_sym if value.is_a? String
    return @resource = value.class.name.underscore.to_sym if value.is_a? Object
end