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>.

Methods

Constants

ITERATOR_COUNT = "__ic__"
ITERATOR_POSITION = "__ip__"

Attributes

iteration_index  [RW] 
iteration_list  [RW] 
to_field  [RW] 
to_variable  [RW] 
value_separator  [RW] 

Public Class methods

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

[Source]

     # 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

Public Instance methods

Has the iteration a next element ?

[Source]

     # File lib/openwfe/expressions/fe_iterator.rb, line 194
194:         def has_next?
195: 
196:             @iteration_index < @iteration_list.size
197:         end

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

[Source]

     # File lib/openwfe/expressions/fe_iterator.rb, line 253
253:         def index
254: 
255:             @iteration_index - 1
256:         end

Jumps to a given position in the iterator

[Source]

     # 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

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

[Source]

     # File lib/openwfe/expressions/fe_iterator.rb, line 212
212:         def next (workitem)
213: 
214:             position_at workitem, @iteration_index
215:         end

Positions the iterator back at position 0.

[Source]

     # File lib/openwfe/expressions/fe_iterator.rb, line 220
220:         def rewind (workitem)
221: 
222:             position_at workitem, 0
223:         end

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

[Source]

     # File lib/openwfe/expressions/fe_iterator.rb, line 203
203:         def size
204: 
205:             @iteration_list.size
206:         end

Jumps a certain number of positions in the iterator.

[Source]

     # File lib/openwfe/expressions/fe_iterator.rb, line 244
244:         def skip (workitem, offset)
245: 
246:             jump workitem, @iteration_index + offset
247:         end

Protected Instance methods

Extracts the iteration list from any value.

[Source]

     # 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).

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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

[Validate]