Sha256: 66a383128369fb9a4c904512d1bce795659a80dcef800c3c5e98568caf9e7e9d

Contents?: true

Size: 915 Bytes

Versions: 2

Compression:

Stored size: 915 Bytes

Contents

require 'app_store/helper'

# Proxy class, call class methods on given class (<tt>to</tt>) with <tt>extra</tt> arguments
# for each method called on a proxy instance
class AppStore::Helper::Proxy
  # Instanciate a new proxy object. Acceptable arguments :
  # * <tt>to</tt>: receiver class
  # * <tt>extra</tt>: extra arguments passed to each methods
  def initialize(args = {})
    @to = args[:to]
    @extra = args[:extra]
  end
  
  private
  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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
app_store-0.1.2 lib/app_store/helpers/proxy.rb
app_store-0.1.0 lib/app_store/helpers/proxy.rb