Module: Bali::Integrator::Rule

Defined in:
lib/bali/integrators/rule_integrator.rb

Class Method Summary collapse

Class Method Details

.add(auth_val, rule_group, *args) ⇒ Object

to define can and cant is basically using this method args can comprises of symbols, and hash (for condition)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bali/integrators/rule_integrator.rb', line 24

def add(auth_val, rule_group, *args)
  conditional_hash = nil
  operations = []

  # scan args for options
  args.each do |elm|
    if elm.is_a?(Hash)
      conditional_hash = elm
    else
      operations << elm
    end
  end

  # add operation one by one
  operations.each do |op|
    rule = Bali::Rule.new(auth_val, op)
    Bali::Integrator::Rule.embed_condition(rule, conditional_hash)
    rule_group.add_rule(rule)
  end
end

.add_can(rule_group, *args) ⇒ Object

add can rule programatically



46
47
48
# File 'lib/bali/integrators/rule_integrator.rb', line 46

def add_can(rule_group, *args)
  add :can, rule_group, *args
end

.add_cannot(rule_group, *args) ⇒ Object

add cannot rule programatically



51
52
53
# File 'lib/bali/integrators/rule_integrator.rb', line 51

def add_cannot(rule_group, *args)
  add :cannot, rule_group, *args
end

.embed_condition(rule, conditional_hash = nil) ⇒ Object

process conditional statement in rule definition conditional hash: proc or proc



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bali/integrators/rule_integrator.rb', line 9

def embed_condition(rule, conditional_hash = nil)
  return if conditional_hash.nil?

  condition_type = conditional_hash.keys[0].to_s.downcase
  condition_type_symb = condition_type.to_sym

  if condition_type_symb == :if || condition_type_symb == :unless
    rule.decider = conditional_hash.values[0]
    rule.decider_type = condition_type_symb
  end
  nil
end