Class: Roleful::Role

Inherits:
Object show all
Defined in:
lib/roleful/role.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, options = {}) ⇒ Role

Returns a new instance of Role.


5
6
7
8
9
10
# File 'lib/roleful/role.rb', line 5

def initialize(klass, name, options={})
  @klass, @name, @options = klass, name, options
  @permissions = Set.new
  @handlers = { }
  define_predicates
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Used when the permission in question is granted for the role in question. TODO: Is this really necessary?


30
31
32
33
# File 'lib/roleful/role.rb', line 30

def method_missing(sym, *args)
  method_id = sym.to_s
  match_permission_or_predicate?(method_id) ? superuser? : super
end

Instance Attribute Details

#handlersObject (readonly)

Returns the value of attribute handlers


3
4
5
# File 'lib/roleful/role.rb', line 3

def handlers
  @handlers
end

#nameObject (readonly)

Returns the value of attribute name


3
4
5
# File 'lib/roleful/role.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options


3
4
5
# File 'lib/roleful/role.rb', line 3

def options
  @options
end

#permissionsObject (readonly)

Returns the value of attribute permissions


3
4
5
# File 'lib/roleful/role.rb', line 3

def permissions
  @permissions
end

Instance Method Details

#can(sym, &block) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/roleful/role.rb', line 12

def can(sym, &block)
  handlers[sym] = block || if RUBY_VERSION < "1.9.0"
    proc { true }
  else
    proc { |arg| true }
  end
  
  metaclass.class_eval(<<-END, __FILE__, __LINE__)
    def can_#{sym}?(target, *args)
      handle(target, #{sym.inspect}, *args)
    end
  END

  register_permission(sym)
end