Class: Concealer::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/concealer/proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(target, strategy) ⇒ Proxy

Returns a new instance of Proxy.



5
6
7
8
# File 'lib/concealer/proxy.rb', line 5

def initialize(target, strategy)
  @target = target
  @strategy = strategy
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/concealer/proxy.rb', line 12

def method_missing(name, *args, &block)

  check_concealed = /(.+)_allowed\?$/.match(name.to_s).try(:[], 1).try(:to_sym)
  
  if check_concealed && @target.respond_to?(check_concealed)
    return @strategy.allow?(@target, check_concealed, args)
  end
  
  if !@target.respond_to?(name)
    raise NoMethodError, "#{@target} does not respond to #{name}"
  elsif @strategy.allow?(@target, name, args)
    @target.send(name, *args, &block)
  else
    Concealer.fallback_for(@target, name).call(@target, name, args)
  end
end