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

#bali_process_auth_rules(auth_val, args) ⇒ Object

to define can and cant is basically using this method



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/bali/dsl/rules_for_dsl.rb', line 104

def bali_process_auth_rules(auth_val, 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)
    if conditional_hash
      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
    end
    self.current_rule_group.add_rule(rule)
  end
end

#can(*operations) ⇒ Object



139
140
141
# File 'lib/bali/dsl/rules_for_dsl.rb', line 139

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

#can_allObject



152
153
154
155
# File 'lib/bali/dsl/rules_for_dsl.rb', line 152

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

#cannot(*operations) ⇒ Object



143
144
145
# File 'lib/bali/dsl/rules_for_dsl.rb', line 143

def cannot(*operations)
  bali_process_auth_rules(:cannot, operations)
end

#cannot_allObject



157
158
159
160
# File 'lib/bali/dsl/rules_for_dsl.rb', line 157

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

#cant(*operations) ⇒ Object



147
148
149
150
# File 'lib/bali/dsl/rules_for_dsl.rb', line 147

def cant(*operations)
  puts "Deprecation Warning: declaring rules with cant will be deprecated on major release 3.0, use cannot instead"
  cannot(*operations)
end

#cant_allObject



162
163
164
165
# File 'lib/bali/dsl/rules_for_dsl.rb', line 162

def cant_all
  puts "Deprecation Warning: declaring rules with cant_all will be deprecated on major release 3.0, use cannot_all instead"
  cannot_all
end

#clear_rulesObject

clear all defined rules



134
135
136
137
# File 'lib/bali/dsl/rules_for_dsl.rb', line 134

def clear_rules
  self.current_rule_group.clear_rules
  true
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
# File 'lib/bali/dsl/rules_for_dsl.rb', line 16

def describe(*params)
  @@lock.synchronize do
    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 for describe: symbol, string, nil and hash"
      end
    end

    target_class = self.map_rules_dsl.current_rule_class.target_class

    subtargets.each do |subtarget|
      rule_group = self.current_rule_class.rules_for(subtarget)
      if rule_group.nil?
        rule_group = Bali::RuleGroup.new(target_class, 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 # each subtarget
  end # sync block
end

#others(*params) ⇒ Object

others block



70
71
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
101
# File 'lib/bali/dsl/rules_for_dsl.rb', line 70

def others(*params)
  @@lock.synchronize do
    rules = {}

    params.each do |passed_argument|
      if passed_argument.is_a?(Hash)
        rules = passed_argument
      else
        raise Bali::DslError, "Allowed argument for others: hash"
      end
    end

    self.current_rule_group  = self.map_rules_dsl.current_rule_class.others_rule_group

    if block_given?
      yield
    else
      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
          rule = Bali::Rule.new(auth_val, operation)
          self.current_rule_group.add_rule(rule)
        end
      end # each rules
    end # block_given?
  end # synchronize
end