Class: Ninja::Delegator

Inherits:
Object
  • Object
show all
Defined in:
lib/ninja/delegator.rb

Instance Method Summary collapse

Constructor Details

#initialize(context, opts = {}) ⇒ Delegator

Returns a new instance of Delegator.



3
4
5
6
7
8
# File 'lib/ninja/delegator.rb', line 3

def initialize(context, opts={})
  @context = context
  # TODO(mtwilliams): Hide standard methods, too.
  @hidden = ((opts[:except] || []).map(&:to_sym))
  @hooks = Hash[((opts.select{|k,_| /^on_.+/.match(k)}).map{|k,v| [k[3..-1].to_sym, v]})]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



10
11
12
13
14
15
16
17
18
19
# File 'lib/ninja/delegator.rb', line 10

def method_missing(name, *args, &block)
  name = name.to_sym
  super if @hidden.include? name
  if @context.respond_to? name
    response = @context.send(name, *args, &block)
    @hooks[name].call(response) if @hooks.include? name
  else
    super
  end
end