Class: Bali::RuleGroup

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, alias_tgt, subtarget) ⇒ RuleGroup

Returns a new instance of RuleGroup.



31
32
33
34
35
36
37
38
# File 'lib/bali/foundations/rule_group.rb', line 31

def initialize(target, alias_tgt, subtarget)
  self.target = target
  self.alias_tgt = alias_tgt
  self.subtarget = Bali::RuleGroup.canon_name(subtarget)

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

Instance Attribute Details

#alias_tgtObject

the alias name for the target



6
7
8
# File 'lib/bali/foundations/rule_group.rb', line 6

def alias_tgt
  @alias_tgt
end

#cansObject

what can be done and what cannot be done



12
13
14
# File 'lib/bali/foundations/rule_group.rb', line 12

def cans
  @cans
end

#cantsObject

what can be done and what cannot be done



12
13
14
# File 'lib/bali/foundations/rule_group.rb', line 12

def cants
  @cants
end

#plantObject Also known as: plant?

if set to true, well, cannot do anything



19
20
21
# File 'lib/bali/foundations/rule_group.rb', line 19

def plant
  @plant
end

#subtargetObject

the user to which this rule group is applied



9
10
11
# File 'lib/bali/foundations/rule_group.rb', line 9

def subtarget
  @subtarget
end

#targetObject

the target class



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

def target
  @target
end

#zeusObject Also known as: zeus?

if set to true then the subtarget can do anything



15
16
17
# File 'lib/bali/foundations/rule_group.rb', line 15

def zeus
  @zeus
end

Class Method Details

.canon_name(subtarget) ⇒ Object

allowing “general user” and :general_user to route to the same rule group



23
24
25
26
27
28
29
# File 'lib/bali/foundations/rule_group.rb', line 23

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:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bali/foundations/rule_group.rb', line 40

def add_rule(rule)
  raise Bali::DslError, "Rule must be of class Bali::Rule" 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

#get_rule(auth_val, operation) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bali/foundations/rule_group.rb', line 57

def get_rule(auth_val, operation)
  rule = nil
  case auth_val
  when :can, "can"
    rule = self.cans[operation.to_sym]
  when :cant, "cant"
    rule = self.cants[operation.to_sym]
  else
    raise Bali::DslError, "Undefined operation: #{auth_val}"
  end

  rule
end

#rulesObject

all rules



72
73
74
# File 'lib/bali/foundations/rule_group.rb', line 72

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