Class: XProc::Proc

Inherits:
BasicObject
Defined in:
lib/xproc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, invocations = []) ⇒ Proc

Returns a new instance of Proc.



7
8
9
10
# File 'lib/xproc.rb', line 7

def initialize(index, invocations = [])
  @index = index
  @invocations = invocations
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



33
34
35
# File 'lib/xproc.rb', line 33

def method_missing(name, *args, &block)
  ::XProc::Proc.new(@index, @invocations + [[name, args, block]])
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



5
6
7
# File 'lib/xproc.rb', line 5

def index
  @index
end

Instance Method Details

#respond_to_missing?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/xproc.rb', line 29

def respond_to_missing?(*)
  true
end

#to_procObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/xproc.rb', line 12

def to_proc
  invocations = @invocations
  index = @index
  ::Kernel.lambda do |*proc_args|
    invocations.reduce(proc_args[index]) do |val, (name, args, block)|
      args = args.map do |arg|
        if arg.is_a?(::XProc::Proc)
          proc_args[arg.index]
        else
          arg
        end
      end
      val.send(name, *args, &block)
    end
  end
end