Class: AppStore::Helper::Proxy

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

Overview

Proxy class, call class methods on given class (to) with extra arguments for each method called on a proxy instance

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Proxy

Instanciate a new proxy object. Acceptable arguments :

  • to: receiver class

  • extra: extra arguments passed to each methods



9
10
11
12
# File 'lib/app_store/helpers/proxy.rb', line 9

def initialize(args = {})
  @to = args[:to]
  @extra = args[:extra]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/app_store/helpers/proxy.rb', line 15

def method_missing(method, *args)
  raise "method #{method} not found for #{@to}" unless @to.methods.include?(method.to_s)
  # OPTIMIZE: define method instead of sending each time
  hash = args.last.is_a?(Hash) ? args.last : {}
  case @to.method(method).arity
  when -2
    @to.send(method, args.first, @extra.merge(hash))
  when -1
    @to.send(method, @extra.merge(hash))
  else
    raise "method #{method} not supported by proxy"
  end
end