Class: Bali::RuleGroup
- Inherits:
-
Object
- Object
- Bali::RuleGroup
- Defined in:
- lib/bali/foundations/rule/rule_group.rb
Instance Attribute Summary collapse
-
#cans ⇒ Object
what can be done and what cannot be done.
-
#cants ⇒ Object
what can be done and what cannot be done.
-
#plant ⇒ Object
(also: #plant?)
if set to true, well, cannot do anything.
-
#subtarget ⇒ Object
the user to which this rule group is applied.
-
#target ⇒ Object
the target class.
-
#zeus ⇒ Object
(also: #zeus?)
if set to true then the subtarget can do anything.
Class Method Summary collapse
-
.canon_name(subtarget) ⇒ Object
allowing “general user” and :general_user to route to the same rule group.
Instance Method Summary collapse
- #add_rule(rule) ⇒ Object
- #clear_rules ⇒ Object
- #clone ⇒ Object
- #get_rule(auth_val, operation) ⇒ Object
-
#initialize(target, subtarget) ⇒ RuleGroup
constructor
A new instance of RuleGroup.
-
#rules ⇒ Object
all rules.
Constructor Details
#initialize(target, subtarget) ⇒ RuleGroup
Returns a new instance of RuleGroup.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bali/foundations/rule/rule_group.rb', line 28 def initialize(target, subtarget) self.target = target self.subtarget = Bali::RuleGroup.canon_name(subtarget) self.cans = {} self.cants = {} # by default, rule group is neither zeus nor plant # it is neutral. # meaning, it is neither allowed to do everything, nor it is # prohibited to do anything. neutral. self.zeus = false self.plant = false end |
Instance Attribute Details
#cans ⇒ Object
what can be done and what cannot be done
9 10 11 |
# File 'lib/bali/foundations/rule/rule_group.rb', line 9 def cans @cans end |
#cants ⇒ Object
what can be done and what cannot be done
9 10 11 |
# File 'lib/bali/foundations/rule/rule_group.rb', line 9 def cants @cants end |
#plant ⇒ Object Also known as: plant?
if set to true, well, cannot do anything
16 17 18 |
# File 'lib/bali/foundations/rule/rule_group.rb', line 16 def plant @plant end |
#subtarget ⇒ Object
the user to which this rule group is applied
6 7 8 |
# File 'lib/bali/foundations/rule/rule_group.rb', line 6 def subtarget @subtarget end |
#target ⇒ Object
the target class
3 4 5 |
# File 'lib/bali/foundations/rule/rule_group.rb', line 3 def target @target end |
#zeus ⇒ Object Also known as: zeus?
if set to true then the subtarget can do anything
12 13 14 |
# File 'lib/bali/foundations/rule/rule_group.rb', line 12 def zeus @zeus end |
Class Method Details
.canon_name(subtarget) ⇒ Object
allowing “general user” and :general_user to route to the same rule group
20 21 22 23 24 25 26 |
# File 'lib/bali/foundations/rule/rule_group.rb', line 20 def self.canon_name(subtarget) if subtarget.is_a?(String) return subtarget.gsub(" ", "_").to_sym else return subtarget end end |
Instance Method Details
#add_rule(rule) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bali/foundations/rule/rule_group.rb', line 53 def add_rule(rule) # operation cannot be defined twice operation = rule.operation.to_sym return if self.cants[operation] && self.cans[operation] if rule.is_discouragement? self.cants[operation] = rule self.cans.delete operation else self.cans[operation] = rule self.cants.delete operation end end |
#clear_rules ⇒ Object
68 69 70 71 |
# File 'lib/bali/foundations/rule/rule_group.rb', line 68 def clear_rules self.cans = {} self.cants = {} end |
#clone ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/bali/foundations/rule/rule_group.rb', line 43 def clone cloned_rg = Bali::RuleGroup.new(target, subtarget) cans.each_value { |can_rule| cloned_rg.add_rule(can_rule.clone) } cants.each_value { |cant_rule| cloned_rg.add_rule(cant_rule.clone) } cloned_rg.zeus = zeus cloned_rg.plant = plant cloned_rg end |
#get_rule(auth_val, operation) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/bali/foundations/rule/rule_group.rb', line 73 def get_rule(auth_val, operation) rule = nil case auth_val when :can, "can" rule = self.cans[operation.to_sym] when :cannot, "cannot" rule = self.cants[operation.to_sym] else fail Bali::DslError, "Undefined operation: #{auth_val}" end rule end |
#rules ⇒ Object
all rules
88 89 90 |
# File 'lib/bali/foundations/rule/rule_group.rb', line 88 def rules self.cans.values + self.cants.values end |