Class: Bali::RulesForDsl

Inherits:
Object
  • Object
show all
Defined in:
lib/bali/dsl/rules_for_dsl.rb

Overview

this class is used to define DSL after rules_for is invoked.

Author:

  • Adam Pahlevi Baihaqi

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map_rules_dsl) ⇒ RulesForDsl

Returns a new instance of RulesForDsl.



7
8
9
10
# File 'lib/bali/dsl/rules_for_dsl.rb', line 7

def initialize(map_rules_dsl)
  @@lock = Mutex.new
  self.map_rules_dsl = map_rules_dsl
end

Instance Attribute Details

#current_rule_groupObject

Returns the value of attribute current_rule_group.



5
6
7
# File 'lib/bali/dsl/rules_for_dsl.rb', line 5

def current_rule_group
  @current_rule_group
end

#map_rules_dslObject

Returns the value of attribute map_rules_dsl.



4
5
6
# File 'lib/bali/dsl/rules_for_dsl.rb', line 4

def map_rules_dsl
  @map_rules_dsl
end

Instance Method Details

#can(*operations) ⇒ Object

process_auth_rules



102
103
104
# File 'lib/bali/dsl/rules_for_dsl.rb', line 102

def can(*operations)
  process_auth_rules(:can, operations)
end

#can_allObject



110
111
112
113
# File 'lib/bali/dsl/rules_for_dsl.rb', line 110

def can_all
  self.current_rule_group.zeus = true
  self.current_rule_group.plant = false
end

#cant(*operations) ⇒ Object



106
107
108
# File 'lib/bali/dsl/rules_for_dsl.rb', line 106

def cant(*operations)
  process_auth_rules(:cant, operations)
end

#cant_allObject



115
116
117
118
# File 'lib/bali/dsl/rules_for_dsl.rb', line 115

def cant_all
  self.current_rule_group.plant = true
  self.current_rule_group.zeus = false
end

#current_rule_classObject



12
13
14
# File 'lib/bali/dsl/rules_for_dsl.rb', line 12

def current_rule_class
  self.map_rules_dsl.current_rule_class
end

#describe(*params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bali/dsl/rules_for_dsl.rb', line 16

def describe(*params)

  subtargets = []
  rules = {}

  params.each do |passed_argument|
    if passed_argument.is_a?(Symbol) || passed_argument.is_a?(String)
      subtargets << passed_argument
    elsif passed_argument.is_a?(NilClass)
      subtargets << passed_argument
    elsif passed_argument.is_a?(Array)
      subtargets += passed_argument
    elsif passed_argument.is_a?(Hash)
      rules = passed_argument
    else 
      raise Bali::DslError, "Allowed argument: symbol, string, nil and hash"
    end
  end

  target_class = self.map_rules_dsl.current_rule_class.target_class
  target_alias = self.map_rules_dsl.current_rule_class.alias_name

  subtargets.each do |subtarget|
    @@lock.synchronize do
      rule_group = self.current_rule_class.rules_for(subtarget)
      if rule_group.nil?
        rule_group = Bali::RuleGroup.new(target_class, target_alias, subtarget)
      end

      self.current_rule_group = rule_group

      if block_given?
        yield
      else
        # auth_val is either can or cant
        rules.each do |auth_val, operations|
          if operations.is_a?(Array)
            operations.each do |op|
              rule = Bali::Rule.new(auth_val, op)
              self.current_rule_group.add_rule(rule)
            end
          else
            operation = operations # well, basically is 1 only
            rule = Bali::Rule.new(auth_val, operation) 
            self.current_rule_group.add_rule(rule)
          end
        end # each rules
      end # block_given?

      # add current_rule_group
      self.map_rules_dsl.current_rule_class.add_rule_group(self.current_rule_group)
    end # mutex synchronize
  end # each subtarget
end

#process_auth_rules(auth_val, operations) ⇒ Object

to define can and cant is basically using this method



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bali/dsl/rules_for_dsl.rb', line 72

def process_auth_rules(auth_val, operations)
  conditional_hash = nil
  
  # scan operations for options
  operations.each do |elm|
    if elm.is_a?(Hash)
      conditional_hash = elm
    end
  end

  if conditional_hash
    op = operations[0]
    rule = Bali::Rule.new(auth_val, op)
    if conditional_hash[:if] || conditional_hash["if"]
      rule.decider = conditional_hash[:if] || conditional_hash["if"]
      rule.decider_type = :if
    elsif conditional_hash[:unless] || conditional_hash[:unless]
      rule.decider = conditional_hash[:unless] || conditional_hash["unless"]
      rule.decider_type = :unless
    end
    self.current_rule_group.add_rule(rule)
  else
    # no conditional hash, proceed adding operations one by one
    operations.each do |op|
      rule = Bali::Rule.new(auth_val, op)
      self.current_rule_group.add_rule(rule)
    end
  end
end