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.



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

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

#current_subtargetsObject

all to be processed subtargets



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

def current_subtargets
  @current_subtargets
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(*args) ⇒ Object



74
75
76
# File 'lib/bali/dsl/rules_for_dsl.rb', line 74

def can(*args)
  Bali::Integrator::Rule.add_can(self.current_rule_group, *args)
end

#can_allObject



87
88
89
# File 'lib/bali/dsl/rules_for_dsl.rb', line 87

def can_all
  Bali::Integrator::RuleGroup.make_zeus(self.current_rule_group)
end

#cannot(*args) ⇒ Object



78
79
80
# File 'lib/bali/dsl/rules_for_dsl.rb', line 78

def cannot(*args)
  Bali::Integrator::Rule.add_cannot(self.current_rule_group, *args)
end

#cannot_allObject



91
92
93
# File 'lib/bali/dsl/rules_for_dsl.rb', line 91

def cannot_all
  Bali::Integrator::RuleGroup.make_plant(self.current_rule_group)
end

#cant(*operations) ⇒ Object



82
83
84
85
# File 'lib/bali/dsl/rules_for_dsl.rb', line 82

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



95
96
97
98
# File 'lib/bali/dsl/rules_for_dsl.rb', line 95

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



69
70
71
72
# File 'lib/bali/dsl/rules_for_dsl.rb', line 69

def clear_rules
  self.current_rule_group.clear_rules
  true
end

#describe(*params) ⇒ Object

role



48
49
50
51
52
53
54
55
56
57
# File 'lib/bali/dsl/rules_for_dsl.rb', line 48

def describe(*params)
  puts "Bali Deprecation Warning: describing rules using describe will be deprecated on major release 3.0, use role instead"
  if block_given?
    role(*params) do
      yield
    end
  else
    role(*params)
  end
end

#others(*params) ⇒ Object

others block



60
61
62
63
64
65
66
# File 'lib/bali/dsl/rules_for_dsl.rb', line 60

def others(*params)
  if block_given?
    role("__*__") do
      yield
    end
  end
end

#role(*params) ⇒ Object



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

def role(*params)
  @@lock.synchronize do
    bali_scrap_actors(*params)
    current_subtargets.each do |subtarget|
      bali_set_subtarget(subtarget)

      if block_given?
        yield
      else
        # if no block, then rules are defined using shortcut notation, eg:
        # role :user, can: [:edit]
        # the last element of which params must be a hash
        shortcut_rules = params[-1]
        unless shortcut_rules.is_a?(Hash)
          raise Bali::DslError, "Pass a hash for shortcut notation"
        end

        shortcut_can_rules = shortcut_rules[:can] || shortcut_rules["can"]
        shortcut_cannot_rules = shortcut_rules[:cannot] || shortcut_rules["cannot"]

        shortcut_rules.each do |auth_val, args|
          Bali::Integrator::Rule.add(auth_val, self.current_rule_group, *args)
        end # each shortcut rules
      end # whether block is given or not
    end # each subtarget
  end # sync
end