summaryrefslogtreecommitdiff
path: root/lib/declarative_policy/rule_dsl.rb
blob: e948b7f2de16eb216c14ffb5e1eaff3407607372 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
module DeclarativePolicy
  # The DSL evaluation context inside rule { ... } blocks.
  # Responsible for creating and combining Rule objects.
  #
  # See Base.rule
  class RuleDsl
    def initialize(context_class)
      @context_class = context_class
    end

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

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

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

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

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

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

    def method_missing(m, *a, &b)
      return super unless a.empty? && !block_given?

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