Class: AmpersandX::Proxy
- Inherits:
-
Object
- Object
- AmpersandX::Proxy
- Defined in:
- lib/ampersand_x.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#actual(x) ⇒ Object
Get the actual value after calling all methods from
path. -
#initialize(path = []) ⇒ Proxy
constructor
Initialize a
Proxy. - #method_missing(*args) ⇒ Object
-
#to_proc ⇒ Object
Convert proxy to a proc.
Constructor Details
#initialize(path = []) ⇒ Proxy
Initialize a Proxy.
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.
25 26 27 |
# File 'lib/ampersand_x.rb', line 25 def actual(x) @path.reduce(x) { |memo, prop| memo.send(*prop) } end |
#to_proc ⇒ Object
Convert proxy to a proc.
30 31 32 |
# File 'lib/ampersand_x.rb', line 30 def to_proc -> x { actual(x) } end |