Class: Policy::Proxy

Inherits:
Object show all
Defined in:
lib/clean-policy/proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(policy) ⇒ Proxy

Returns a new instance of Proxy.



12
13
14
# File 'lib/clean-policy/proxy.rb', line 12

def initialize policy
  @policy = policy
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/clean-policy/proxy.rb', line 16

def method_missing name, &block
  name   = name.to_s.sub(/(.)$/, '')
  action = $1

  if action == '!'
    @policy.can?(name, &block)
    true
  elsif action == '?'
    raise "Block given, not allowed in boolean (?) policy, use bang .#{name}! { .. }" if block_given?

    begin
      @policy.can?(name)
      true
    rescue Policy::Error
      yield if block_given?
      false
    end
  else
    raise ArgumentError.new('Bad policy method name')
  end
end