Class: OpenWFE::Iterator

Inherits:
Object
  • Object
show all
Defined in:
lib/openwfe/expressions/fe_iterator.rb

Overview

Iterator instances keep track of the position of an iteration. This class is meant to be used both by <iterator> and <concurrent-iterator>.

Constant Summary collapse

ITERATOR_COUNT =
"__ic__"
ITERATOR_POSITION =
"__ip__"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iterator_expression, workitem) ⇒ Iterator

Builds a new iterator, serving a given iterator_expression. The second parameter is the workitem (as applied to the iterator expression).



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/openwfe/expressions/fe_iterator.rb', line 162

def initialize (iterator_expression, workitem)

    @to_field = iterator_expression\
        .lookup_attribute(:to_field, workitem)
    @to_variable = iterator_expression\
        .lookup_attribute(:to_variable, workitem)

    @value_separator = iterator_expression\
        .lookup_attribute(:value_separator, workitem)

    @value_separator = /,\s*/ unless @value_separator

    @iteration_index = 0

    #raw_list = iterator_expression.lookup_vf_attribute(
    #    workitem, :value, :prefix => :on)
    #raw_list ||= iterator_expression.lookup_attribute(:on, workitem)

    raw_list = 
        iterator_expression.lookup_vf_attribute(
            workitem, :value, :prefix => :on) ||
        iterator_expression.lookup_vf_attribute(
            workitem, nil, :prefix => :on)

    @iteration_list = extract_iteration_list raw_list

    workitem.attributes[ITERATOR_COUNT] = @iteration_list.length
end

Instance Attribute Details

#iteration_indexObject

Returns the value of attribute iteration_index.



151
152
153
# File 'lib/openwfe/expressions/fe_iterator.rb', line 151

def iteration_index
  @iteration_index
end

#iteration_listObject

Returns the value of attribute iteration_list.



152
153
154
# File 'lib/openwfe/expressions/fe_iterator.rb', line 152

def iteration_list
  @iteration_list
end

#to_fieldObject

Returns the value of attribute to_field.



153
154
155
# File 'lib/openwfe/expressions/fe_iterator.rb', line 153

def to_field
  @to_field
end

#to_variableObject

Returns the value of attribute to_variable.



154
155
156
# File 'lib/openwfe/expressions/fe_iterator.rb', line 154

def to_variable
  @to_variable
end

#value_separatorObject

Returns the value of attribute value_separator.



155
156
157
# File 'lib/openwfe/expressions/fe_iterator.rb', line 155

def value_separator
  @value_separator
end

Instance Method Details

#has_next?Boolean

Has the iteration a next element ?

Returns:

  • (Boolean)


194
195
196
197
# File 'lib/openwfe/expressions/fe_iterator.rb', line 194

def has_next?

    @iteration_index < @iteration_list.size
end

#indexObject

The current index (whereas @iteration_index already points to the next element).



253
254
255
256
# File 'lib/openwfe/expressions/fe_iterator.rb', line 253

def index

    @iteration_index - 1
end

#jump(workitem, index) ⇒ Object

Jumps to a given position in the iterator



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/openwfe/expressions/fe_iterator.rb', line 228

def jump (workitem, index)

    index = if index < 0
        0
    elsif index >= @iteration_list.size
        @iteration_list.size
    else
        index
    end

    position_at workitem, index
end

#next(workitem) ⇒ Object

Prepares the iterator expression and the workitem for the next iteration.



212
213
214
215
# File 'lib/openwfe/expressions/fe_iterator.rb', line 212

def next (workitem)

    position_at workitem, @iteration_index
end

#rewind(workitem) ⇒ Object

Positions the iterator back at position 0.



220
221
222
223
# File 'lib/openwfe/expressions/fe_iterator.rb', line 220

def rewind (workitem)

    position_at workitem, 0
end

#sizeObject

Returns the size of this iterator, or rather, the size of the underlying iteration list.



203
204
205
206
# File 'lib/openwfe/expressions/fe_iterator.rb', line 203

def size

    @iteration_list.size
end

#skip(workitem, offset) ⇒ Object

Jumps a certain number of positions in the iterator.



244
245
246
247
# File 'lib/openwfe/expressions/fe_iterator.rb', line 244

def skip (workitem, offset)

    jump workitem, @iteration_index + offset
end