Class: Peaty::Proxy

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, connection, options = {}) ⇒ Proxy

Returns a new instance of Proxy.



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

def initialize(target, connection, options = {})
  @target, @connection, @options = target, connection, options.with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



10
11
12
13
14
15
# File 'lib/peaty/proxy.rb', line 10

def method_missing(method, *args)
  return @target.method(method).unbind.bind(self).call(*args) if @target.class.respond_to?(method)
  options = args.extract_options!
  args << @options.merge(options)
  @target.with_connection(@connection).send(method, *args)
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



4
5
6
# File 'lib/peaty/proxy.rb', line 4

def connection
  @connection
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/peaty/proxy.rb', line 4

def options
  @options
end

#targetObject

Returns the value of attribute target.



4
5
6
# File 'lib/peaty/proxy.rb', line 4

def target
  @target
end

Class Method Details

.build(attrs = {}) ⇒ Object



28
29
30
# File 'lib/peaty/proxy.rb', line 28

def self.build(attrs = {})
  @target.build(@options.merge(attrs.with_indifferent_access))
end

Instance Method Details

#filter(*args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/peaty/proxy.rb', line 17

def filter(*args)
  options = args.extract_options!
  options.each do |(key, value)|
    @options[key] = Array.wrap(@options.delete(:key)).concat(Array.wrap(value))
  end
  
  @options[:rest] = Array.wrap(@options.delete(:rest)).concat(args)
  
  self
end