Class: Marso::Enumerate

Inherits:
Object show all
Defined in:
lib/marso/toolbelt/fiberpiping.rb

Direct Known Subclasses

FiberFilter, FiberProjection, FiberProjectionMany

Constant Summary collapse

STOP_MSG =
"End of seed"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(process, source = {}) ⇒ Enumerate

Returns a new instance of Enumerate.



21
22
23
24
25
26
27
28
# File 'lib/marso/toolbelt/fiberpiping.rb', line 21

def initialize(process, source={})
  @src = source
  @process = process

  @fiber_delegate = Fiber.new do
    process.call
  end
end

Instance Attribute Details

#processObject (readonly)

Returns the value of attribute process.



7
8
9
# File 'lib/marso/toolbelt/fiberpiping.rb', line 7

def process
  @process
end

#srcObject (readonly)

Returns the value of attribute src.



7
8
9
# File 'lib/marso/toolbelt/fiberpiping.rb', line 7

def src
  @src
end

Class Method Details

.from(collection) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/marso/toolbelt/fiberpiping.rb', line 9

def self.from(collection)
  _process = lambda {
    collection.each { |x|
      Fiber.yield x
    }

    raise StopIteration, STOP_MSG
  }

  Enumerate.new(_process)
end

Instance Method Details

#cloneObject



42
43
44
45
46
47
48
49
# File 'lib/marso/toolbelt/fiberpiping.rb', line 42

def clone
  class_name = self.class.to_s
  if @src.is_a?(Marso::Enumerate)
    return Object.const_get(class_name).new(@process, @src.clone)
  else
    return Object.const_get(class_name).new(@process)
  end
end

#execute(max_iteration = 1000000) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/marso/toolbelt/fiberpiping.rb', line 67

def execute(max_iteration=1000000)
  begin
    if max_iteration.zero?
      loop do
        self.resume
      end
    else
      max_iteration.times { self.resume }
    end
  rescue StopIteration => ex
    raise ex unless ex.message == STOP_MSG
  rescue Exception => ex
    raise ex
  end
end

#resumeObject



30
31
32
33
34
35
36
# File 'lib/marso/toolbelt/fiberpiping.rb', line 30

def resume
  if @src.is_a?(Marso::Enumerate)
    @fiber_delegate.resume(@src.resume)
  else # case where @src is nil
    @fiber_delegate.resume
  end
end

#select(&block) ⇒ Object



59
60
61
# File 'lib/marso/toolbelt/fiberpiping.rb', line 59

def select(&block)
  FiberProjection.new(block, clone_original_source_to_protect_it)
end

#select_many(&block) ⇒ Object



63
64
65
# File 'lib/marso/toolbelt/fiberpiping.rb', line 63

def select_many(&block)
  FiberProjectionMany.new(block, clone_original_source_to_protect_it)
end

#source(other_source) ⇒ Object



38
39
40
# File 'lib/marso/toolbelt/fiberpiping.rb', line 38

def source(other_source)
  Enumerate.new(@process, other_source)
end

#to_aObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/marso/toolbelt/fiberpiping.rb', line 83

def to_a
  a = []
  begin
    loop do
      a << self.resume
    end
  rescue StopIteration => ex
    raise ex unless ex.message == STOP_MSG
  rescue Exception => ex
    raise ex
  end

  return a
end

#where(&block) ⇒ Object



55
56
57
# File 'lib/marso/toolbelt/fiberpiping.rb', line 55

def where(&block)
  FiberFilter.new(block, clone_original_source_to_protect_it)
end

#|(other_source) ⇒ Object



51
52
53
# File 'lib/marso/toolbelt/fiberpiping.rb', line 51

def |(other_source)
  other_source.source(clone_original_source_to_protect_it)
end