Class: AmpersandX::Proxy

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = []) ⇒ Proxy

Initialize a Proxy.

Parameters:

  • path (Array<Array>) (defaults to: [])

    path array



14
15
16
# File 'lib/ampersand_x.rb', line 14

def initialize(path = [])
  @path = path
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



18
19
20
# File 'lib/ampersand_x.rb', line 18

def method_missing(*args)
  self.class.new(@path + [args])
end

Class Method Details

.method_missing(name, *args) ⇒ Object



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

def method_missing(name, *args)
  new([name, *args])
end

Instance Method Details

#actual(x) ⇒ Object

Get the actual value after calling all methods from path.

Parameters:

  • x (Object)

    object to call path methods on



25
26
27
# File 'lib/ampersand_x.rb', line 25

def actual(x)
  @path.reduce(x) { |memo, prop| memo.send(*prop) }
end

#to_procObject

Convert proxy to a proc.



30
31
32
# File 'lib/ampersand_x.rb', line 30

def to_proc
  -> x { actual(x) }
end