Class: PipeOperator::Pipe

Inherits:
BasicObject
Defined in:
lib/pipe_operator/pipe.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, *args, &block) ⇒ Pipe

Returns a new instance of Pipe.



27
28
29
30
31
32
# File 'lib/pipe_operator/pipe.rb', line 27

def initialize(object, *args, &block)
  @args = args
  @block = block
  @object = object
  @pipeline = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pipe_operator/pipe.rb', line 72

def method_missing(method, *curry, &block)
  closure = Closure.new(self, method, *curry, &block)

  pipe = Pipe.open
  pipe && [*curry, block].each { |o| pipe.__pop__(o) }

  if pipe == self
    __push__(closure.__shift__)
  elsif pipe
    pipe.__push__(closure)
  else
    closure
  end
end

Class Method Details

.new(object, *args) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/pipe_operator/pipe.rb', line 9

def self.new(object, *args)
  if block_given?
    super.__call__
  elsif args.none? || Closure === args[0]
    super
  else
    super(object).__send__(*args)
  end
end

.open(pipe = nil) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/pipe_operator/pipe.rb', line 19

def self.open(pipe = nil)
  @pipeline ||= []
  @pipeline << pipe if pipe
  block_given? ? yield : @pipeline.last
ensure
  @pipeline.pop if pipe
end

Instance Method Details

#__call__Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pipe_operator/pipe.rb', line 34

def __call__
  if defined?(@pipe)
    return @pipe
  elsif @block
    ProxyResolver.new(::Object).proxy
    @args.each { |arg| ProxyResolver.new(arg).proxy }
    Pipe.open(self) { instance_exec(*@args, &@block) }
  end

  @pipe = @object
  @pipeline.each { |closure| @pipe = closure.call(@pipe) }
  @pipe
end

#inspectObject



48
49
50
51
52
# File 'lib/pipe_operator/pipe.rb', line 48

def inspect
  return method_missing(__method__) if Pipe.open
  inspect = ::PipeOperator.inspect(@object)
  "#<#{Pipe.name}:#{inspect}>"
end