| Class | OpenWFE::Iterator |
| In: |
lib/openwfe/expressions/fe_iterator.rb
|
| Parent: | Object |
Iterator instances keep track of the position of an iteration. This class is meant to be used both by <iterator> and <concurrent-iterator>.
| ITERATOR_COUNT | = | "__ic__" |
| ITERATOR_POSITION | = | "__ip__" |
| iteration_index | [RW] | |
| iteration_list | [RW] | |
| to_field | [RW] | |
| to_variable | [RW] | |
| value_separator | [RW] |
Builds a new iterator, serving a given iterator_expression. The second parameter is the workitem (as applied to the iterator expression).
# File lib/openwfe/expressions/fe_iterator.rb, line 162
162: def initialize (iterator_expression, workitem)
163:
164: @to_field = iterator_expression\
165: .lookup_attribute(:to_field, workitem)
166: @to_variable = iterator_expression\
167: .lookup_attribute(:to_variable, workitem)
168:
169: @value_separator = iterator_expression\
170: .lookup_attribute(:value_separator, workitem)
171:
172: @value_separator = /,\s*/ unless @value_separator
173:
174: @iteration_index = 0
175:
176: #raw_list = iterator_expression.lookup_vf_attribute(
177: # workitem, :value, :prefix => :on)
178: #raw_list ||= iterator_expression.lookup_attribute(:on, workitem)
179:
180: raw_list =
181: iterator_expression.lookup_vf_attribute(
182: workitem, :value, :prefix => :on) ||
183: iterator_expression.lookup_vf_attribute(
184: workitem, nil, :prefix => :on)
185:
186: @iteration_list = extract_iteration_list raw_list
187:
188: workitem.attributes[ITERATOR_COUNT] = @iteration_list.length
189: end
Jumps to a given position in the iterator
# File lib/openwfe/expressions/fe_iterator.rb, line 228
228: def jump (workitem, index)
229:
230: index = if index < 0
231: 0
232: elsif index >= @iteration_list.size
233: @iteration_list.size
234: else
235: index
236: end
237:
238: position_at workitem, index
239: end
Positions the iterator back at position 0.
# File lib/openwfe/expressions/fe_iterator.rb, line 220
220: def rewind (workitem)
221:
222: position_at workitem, 0
223: end
Jumps a certain number of positions in the iterator.
# File lib/openwfe/expressions/fe_iterator.rb, line 244
244: def skip (workitem, offset)
245:
246: jump workitem, @iteration_index + offset
247: end
Extracts the iteration list from any value.
# File lib/openwfe/expressions/fe_iterator.rb, line 288
288: def extract_iteration_list (raw_list)
289:
290: if is_suitable_list?(raw_list)
291: raw_list
292: else
293: extract_list_from_string raw_list.to_s
294: end
295: end
Extracts the list from the string (comma separated list usually).
# File lib/openwfe/expressions/fe_iterator.rb, line 312
312: def extract_list_from_string (s)
313:
314: s.split @value_separator
315: end
Returns true if the given instance can be directly used as a list.
# File lib/openwfe/expressions/fe_iterator.rb, line 301
301: def is_suitable_list? (instance)
302:
303: (not instance.is_a?(String)) and \
304: instance.respond_to? :[] and \
305: instance.respond_to? :length
306: end
Positions the iterator absolutely.
# File lib/openwfe/expressions/fe_iterator.rb, line 263
263: def position_at (workitem, position)
264:
265: result = {}
266:
267: value = @iteration_list[position]
268:
269: return nil unless value
270:
271: if @to_field
272: workitem.attributes[@to_field] = value
273: else
274: result[@to_variable] = value
275: end
276:
277: workitem.attributes[ITERATOR_POSITION] = position
278: result[ITERATOR_POSITION] = position
279:
280: @iteration_index = position + 1
281:
282: result
283: end