Class: Bali::Role
- Inherits:
-
Object
- Object
- Bali::Role
- Defined in:
- lib/bali/role.rb
Constant Summary collapse
- RIGHTS =
[ INHERIT = :inherit, DEFAULT_DENY = :default_deny, DEFAULT_ALLOW = :default_allow ].freeze
- IDENTIFIER_CLASSES =
[ String, Symbol, NilClass, ].freeze
Instance Attribute Summary collapse
-
#can_all ⇒ Object
Returns the value of attribute can_all.
-
#cans ⇒ Object
Returns the value of attribute cans.
-
#cants ⇒ Object
Returns the value of attribute cants.
-
#name ⇒ Object
Returns the value of attribute name.
-
#right_level ⇒ Object
Returns the value of attribute right_level.
-
#scope(&block) ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(rule) ⇒ Object
DSL METHODS.
- #add(term, *operations, block) ⇒ Object
-
#can(*args, &block) ⇒ Object
DSL METHODS.
-
#can_all? ⇒ Boolean
Returns the value of attribute can_all.
- #cant(*args, &block) ⇒ Object
- #cant_all ⇒ Object
- #cant_all? ⇒ Boolean
- #find_rule(term, operation) ⇒ Object
-
#initialize(name) ⇒ Role
constructor
A new instance of Role.
- #rules ⇒ Object
Constructor Details
#initialize(name) ⇒ Role
Returns a new instance of Role.
39 40 41 42 43 44 45 |
# File 'lib/bali/role.rb', line 39 def initialize(name) @name = name.to_sym if name @right_level = INHERIT @cans = {} @cants = {} end |
Instance Attribute Details
#can_all ⇒ Object
Returns the value of attribute can_all.
18 19 20 |
# File 'lib/bali/role.rb', line 18 def can_all @can_all end |
#cans ⇒ Object
Returns the value of attribute cans.
15 16 17 |
# File 'lib/bali/role.rb', line 15 def cans @cans end |
#cants ⇒ Object
Returns the value of attribute cants.
15 16 17 |
# File 'lib/bali/role.rb', line 15 def cants @cants end |
#name ⇒ Object
Returns the value of attribute name.
14 15 16 |
# File 'lib/bali/role.rb', line 14 def name @name end |
#right_level ⇒ Object
Returns the value of attribute right_level.
21 22 23 |
# File 'lib/bali/role.rb', line 21 def right_level @right_level end |
#scope(&block) ⇒ Object (readonly)
Returns the value of attribute scope.
16 17 18 |
# File 'lib/bali/role.rb', line 16 def scope @scope end |
Class Method Details
.extract_roles_from_object(object) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/bali/role.rb', line 31 def self.extract_roles_from_object(object) method_name = object.class. method_name ? formalize(object.send(method_name)) : formalize(nil) end |
.formalize(object) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/bali/role.rb', line 23 def self.formalize(object) case object when *IDENTIFIER_CLASSES then [object] when Array then (object.count == 0 ? nil : object) else formalize(extract_roles_from_object(object)) end end |
Instance Method Details
#<<(rule) ⇒ Object
DSL METHODS
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/bali/role.rb', line 88 def << rule operation = rule.operation.to_sym if rule.term == :cant cants[operation] = rule cans.delete operation else cans[operation] = rule cants.delete operation end end |
#add(term, *operations, block) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/bali/role.rb', line 79 def add(term, *operations, block) operations.each do |operation| rule = Bali::Rule.new(term, operation) rule.conditional = block if block self << rule end end |
#can(*args, &block) ⇒ Object
DSL METHODS
56 57 58 |
# File 'lib/bali/role.rb', line 56 def can(*args, &block) add :can, *args, block end |
#can_all? ⇒ Boolean
Returns the value of attribute can_all.
19 20 21 |
# File 'lib/bali/role.rb', line 19 def can_all @can_all end |
#cant(*args, &block) ⇒ Object
60 61 62 |
# File 'lib/bali/role.rb', line 60 def cant(*args, &block) add :cant, *args, block end |
#cant_all ⇒ Object
68 69 70 |
# File 'lib/bali/role.rb', line 68 def cant_all @right_level = DEFAULT_DENY end |
#cant_all? ⇒ Boolean
51 52 53 |
# File 'lib/bali/role.rb', line 51 def cant_all? right_level == DEFAULT_DENY end |
#find_rule(term, operation) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/bali/role.rb', line 100 def find_rule(term, operation) case term when :can then cans[operation.to_sym] when :cant then cants[operation.to_sym] end end |
#rules ⇒ Object
107 108 109 |
# File 'lib/bali/role.rb', line 107 def rules cans.values + cants.values end |