Class: Bali::RuleGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/bali/foundations/rule/rule_group.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, subtarget) ⇒ RuleGroup

Returns a new instance of RuleGroup.



28
29
30
31
32
33
34
35
36
37
38
# 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
  self.zeus = false
  self.plant = false
end

Instance Attribute Details

#cansObject

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

#cantsObject

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

#plantObject 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

#subtargetObject

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

#targetObject

the target class



3
4
5
# File 'lib/bali/foundations/rule/rule_group.rb', line 3

def target
  @target
end

#zeusObject 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

Raises:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bali/foundations/rule/rule_group.rb', line 50

def add_rule(rule)
  raise Bali::DslError, "Rule must be of class Bali::Rule, got: #{rule.class.name}" unless rule.is_a?(Bali::Rule)

  # operation cannot be defined twice
  operation = rule.operation.to_sym

  raise Bali::DslError, "Rule is defined twice for operation #{operation}" 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_rulesObject



67
68
69
70
# File 'lib/bali/foundations/rule/rule_group.rb', line 67

def clear_rules
  self.cans = {}
  self.cants = {}
end

#cloneObject



40
41
42
43
44
45
46
47
48
# File 'lib/bali/foundations/rule/rule_group.rb', line 40

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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bali/foundations/rule/rule_group.rb', line 72

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
    raise Bali::DslError, "Undefined operation: #{auth_val}"
  end

  rule
end

#rulesObject

all rules



87
88
89
# File 'lib/bali/foundations/rule/rule_group.rb', line 87

def rules
  self.cans.values + self.cants.values
end