Class: Roleful::Role
Instance Attribute Summary collapse
-
#handlers ⇒ Object
readonly
Returns the value of attribute handlers.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#permissions ⇒ Object
readonly
Returns the value of attribute permissions.
Instance Method Summary collapse
- #can(sym, &block) ⇒ Object
-
#initialize(klass, name, options = {}) ⇒ Role
constructor
A new instance of Role.
-
#method_missing(sym, *args) ⇒ Object
Used when the permission in question is granted for the role in question.
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, ={}) @klass, @name, @options = klass, name, @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 (method_id) ? superuser? : super end |
Instance Attribute Details
#handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
3 4 5 |
# File 'lib/roleful/role.rb', line 3 def handlers @handlers end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/roleful/role.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/roleful/role.rb', line 3 def @options end |
#permissions ⇒ Object (readonly)
Returns the value of attribute permissions.
3 4 5 |
# File 'lib/roleful/role.rb', line 3 def @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 .class_eval(<<-END, __FILE__, __LINE__) def can_#{sym}?(target, *args) handle(target, #{sym.inspect}, *args) end END (sym) end |