Class: DeclarativePolicy::RuleDsl

Inherits:
Object
  • Object
show all
Defined in:
lib/declarative_policy/rule_dsl.rb

Overview

The DSL evaluation context inside rule { … } blocks. Responsible for creating and combining Rule objects.

See Base.rule

Instance Method Summary collapse

Constructor Details

#initialize(context_class) ⇒ RuleDsl

Returns a new instance of RuleDsl.



9
10
11
# File 'lib/declarative_policy/rule_dsl.rb', line 9

def initialize(context_class)
  @context_class = context_class
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msg, *args) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/declarative_policy/rule_dsl.rb', line 37

def method_missing(msg, *args)
  return super unless args.empty? && !block_given?

  if @context_class.delegations.key?(msg)
    DelegateDsl.new(self, msg)
  else
    cond(msg.to_sym)
  end
end

Instance Method Details

#all?(*rules) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/declarative_policy/rule_dsl.rb', line 17

def all?(*rules)
  Rule::And.make(rules)
end

#any?(*rules) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/declarative_policy/rule_dsl.rb', line 21

def any?(*rules)
  Rule::Or.make(rules)
end

#can?(ability) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/declarative_policy/rule_dsl.rb', line 13

def can?(ability)
  Rule::Ability.new(ability)
end

#cond(condition) ⇒ Object



29
30
31
# File 'lib/declarative_policy/rule_dsl.rb', line 29

def cond(condition)
  Rule::Condition.new(condition)
end

#delegate(delegate_name, condition) ⇒ Object



33
34
35
# File 'lib/declarative_policy/rule_dsl.rb', line 33

def delegate(delegate_name, condition)
  Rule::DelegatedCondition.new(delegate_name, condition)
end

#none?(*rules) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/declarative_policy/rule_dsl.rb', line 25

def none?(*rules)
  ~Rule::Or.new(rules)
end

#respond_to_missing?(symbol, include_all) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/declarative_policy/rule_dsl.rb', line 47

def respond_to_missing?(symbol, include_all)
  true
end