Class: Pacer::Transform::SortSection::SortBySectionPipe

Inherits:
Pipes::RubyPipe
  • Object
show all
Defined in:
lib/pacer/transform/sort_section.rb

Direct Known Subclasses

CustomSortPipe

Instance Attribute Summary collapse

Attributes inherited from Pipes::RubyPipe

#starts

Instance Method Summary collapse

Methods inherited from Pipes::RubyPipe

#enablePath, #reset

Constructor Details

#initialize(route, visitor_pipe, pipe_function, nil_value) ⇒ SortBySectionPipe

Returns a new instance of SortBySectionPipe.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pacer/transform/sort_section.rb', line 57

def initialize(route, visitor_pipe, pipe_function, nil_value)
  super()
  @to_emit = []
  @visitor_pipe = visitor_pipe
  @route = route
  @to_sort = []
  @paths = []
  @nil_value = nil_value
  if visitor_pipe
    visitor_pipe.visitor = self
  else
    on_element nil
  end
  if pipe_function
    if pipe_function.arity == 1
      @pf_1 = pipe_function
      visitor_pipe.use_on_element = false
    else
      @pf_2 = pipe_function
    end
  else
    visitor_pipe.use_on_element = false
  end
end

Instance Attribute Details

#getPathToHereObject (readonly)

Returns the value of attribute getPathToHere.



55
56
57
# File 'lib/pacer/transform/sort_section.rb', line 55

def getPathToHere
  @getPathToHere
end

#nil_valueObject (readonly)

Returns the value of attribute nil_value.



54
55
56
# File 'lib/pacer/transform/sort_section.rb', line 54

def nil_value
  @nil_value
end

#pf_1Object (readonly)

Returns the value of attribute pf_1.



54
55
56
# File 'lib/pacer/transform/sort_section.rb', line 54

def pf_1
  @pf_1
end

#pf_2Object (readonly)

Returns the value of attribute pf_2.



54
55
56
# File 'lib/pacer/transform/sort_section.rb', line 54

def pf_2
  @pf_2
end

#routeObject (readonly)

Returns the value of attribute route.



54
55
56
# File 'lib/pacer/transform/sort_section.rb', line 54

def route
  @route
end

#sectionObject (readonly)

Returns the value of attribute section.



54
55
56
# File 'lib/pacer/transform/sort_section.rb', line 54

def section
  @section
end

#to_emitObject (readonly)

Returns the value of attribute to_emit.



54
55
56
# File 'lib/pacer/transform/sort_section.rb', line 54

def to_emit
  @to_emit
end

#to_sortObject (readonly)

Returns the value of attribute to_sort.



54
55
56
# File 'lib/pacer/transform/sort_section.rb', line 54

def to_sort
  @to_sort
end

Instance Method Details

#after_elementObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/pacer/transform/sort_section.rb', line 113

def after_element
  if to_sort.any?
    if pf_1
      if nil_value
        sorted = to_sort.sort_by do |element, path|
          v = pf_1.call element
          if v.nil?
            nil_value
          else
            v
          end
        end
      else
        sorted = to_sort.sort_by do |element, path|
          pf_1.call element
        end
      end
    elsif pf_2
      if nil_value
        sorted = to_sort.sort_by do |element, path|
          v = pf_2.call_with_args element, @section_element, pf_2.wrap_path(path)
          if v.nil?
            nil_value
          else
            v
          end
        end
      else
        sorted = to_sort.sort_by do |element, path|
          pf_2.call_with_args element, @section_element, pf_2.wrap_path(path)
        end
      end
    else
      sorted = to_sort.sort_by do |element, path|
        if element.nil?
          nil_value
        else
          element
        end
      end
    end
    to_emit.concat sorted
    @to_sort.clear
  end
end

#on_element(element) ⇒ Object



109
110
111
# File 'lib/pacer/transform/sort_section.rb', line 109

def on_element(element)
  @section_element = element
end

#processNextStartObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pacer/transform/sort_section.rb', line 87

def processNextStart
  if pathEnabled
    while to_emit.empty?
      to_sort << [starts.next, starts.getCurrentPath]
    end
  else
    while to_emit.empty?
      to_sort << [starts.next, nil]
    end
  end
  raise EmptyPipe.instance if to_emit.empty?
  element, @getPathToHere = to_emit.shift
  element
rescue EmptyPipe, java.util.NoSuchElementException
  if to_emit.empty?
    raise EmptyPipe.instance
  else
    after_element
    retry
  end
end

#setStarts(starts) ⇒ Object



82
83
84
85
# File 'lib/pacer/transform/sort_section.rb', line 82

def setStarts(starts)
  super
  enablePath(true) if pf_2
end