summaryrefslogtreecommitdiff
path: root/lib/declarative_policy/dsl.rb
blob: b26807a762235a8550acaaf73dcb554894ecaf82 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
102
103
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.size == 0 && !block_given?

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

  # Used when the name of a delegate is mentioned in
  # the rule DSL.
  class DelegateDsl
    def initialize(rule_dsl, delegate_name)
      @rule_dsl = rule_dsl
      @delegate_name = delegate_name
    end

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

      @rule_dsl.delegate(@delegate_name, m)
    end
  end

  # The return value of a rule { ... } declaration.
  # Can call back to register rules with the containing
  # Policy class (context_class here). See Base.rule
  #
  # Note that the #policy method just performs an #instance_eval,
  # which is useful for multiple #enable or #prevent callse.
  #
  # Also provides a #method_missing proxy to the context
  # class's class methods, so that helper methods can be
  # defined and used in a #policy { ... } block.
  class PolicyDsl
    def initialize(context_class, rule)
      @context_class = context_class
      @rule = rule
    end

    def policy(&b)
      instance_eval(&b)
    end

    def enable(*abilities)
      @context_class.enable_when(abilities, @rule)
    end

    def prevent(*abilities)
      @context_class.prevent_when(abilities, @rule)
    end

    def prevent_all
      @context_class.prevent_all_when(@rule)
    end

    def method_missing(m, *a, &b)
      return super unless @context_class.respond_to?(m)

      @context_class.__send__(m, *a, &b)
    end

    def respond_to_missing?(m)
      @context_class.respond_to?(m) || super
    end
  end
end