Class: PipeOperator::Closure

Inherits:
Proc
  • Object
show all
Defined in:
lib/pipe_operator/closure.rb

Constant Summary collapse

RESERVED =
%i[
  ==
  []
  __send__
  call
  class
  kind_of?
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClosure

:nodoc:



37
38
39
40
# File 'lib/pipe_operator/closure.rb', line 37

def initialize(*) # :nodoc:
  @__chain__ ||= []
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



65
66
67
68
# File 'lib/pipe_operator/closure.rb', line 65

def method_missing(method, *args, &block)
  __chain__ << [method, args, block]
  self
end

Class Method Details

.curry(curry, search, args) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/pipe_operator/closure.rb', line 14

def self.curry(curry, search, args)
  index = curry.index(search)
  prefix = index ? curry[0...index] : curry
  suffix = index ? curry[index - 1..-1] : []

  (prefix + args + suffix).map do |object|
    self === object ? object.call : object
  end
end

.new(pipe = nil, method = nil, *curry, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pipe_operator/closure.rb', line 24

def self.new(pipe = nil, method = nil, *curry, &block)
  return super(&block) unless pipe && method

  search = Pipe.open || pipe

  closure = super() do |*args, &code|
    code ||= block
    curried = curry(curry, search, args)
    value = pipe.__call__.__send__(method, *curried, &code)
    closure.__chain__(value)
  end
end

Instance Method Details

#__chain__(*args) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/pipe_operator/closure.rb', line 46

def __chain__(*args)
  return @__chain__ if args.empty?

  @__chain__.reduce(args[0]) do |object, chain|
    method, args, block = chain
    object.__send__(method, *args, &block)
  end
end

#__shift__Object



55
56
57
58
59
60
61
# File 'lib/pipe_operator/closure.rb', line 55

def __shift__
  closure = self.class.new do |*args, &block|
    args.shift
    value = call(*args, &block)
    closure.__chain__(value)
  end
end

#|(*args, &block) ⇒ Object



42
43
44
# File 'lib/pipe_operator/closure.rb', line 42

def |(*args, &block)
  Pipe.new(self, *args, &block)
end