Class: Puppet::Pops::Types::IntegerRangeIterator Private

Inherits:
Iterator show all
Includes:
Enumerable
Defined in:
lib/puppet/pops/types/iterable.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Methods included from Enumerable

#uniq

Methods inherited from Iterator

#method_missing, #respond_to_missing?, #step, #to_s

Methods included from Iterable

asserted_iterable, #each, on, #step, #to_a, unbounded?

Constructor Details

#initialize(range, step = 1) ⇒ IntegerRangeIterator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of IntegerRangeIterator.

Raises:

  • (ArgumentError)


257
258
259
260
261
262
# File 'lib/puppet/pops/types/iterable.rb', line 257

def initialize(range, step = 1)
  raise ArgumentError if step == 0
  @range = range
  @step_size = step
  @current = (step < 0 ? range.to : range.from) - step
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Puppet::Pops::Types::Iterator

Instance Method Details

#element_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



264
265
266
# File 'lib/puppet/pops/types/iterable.rb', line 264

def element_type
  @range
end

#nextObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



268
269
270
271
272
273
274
275
276
# File 'lib/puppet/pops/types/iterable.rb', line 268

def next
  value = @current + @step_size
  if @step_size < 0
    raise StopIteration if value < @range.from
  else
    raise StopIteration if value > @range.to
  end
  @current = value
end

#reverse_each(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



278
279
280
281
# File 'lib/puppet/pops/types/iterable.rb', line 278

def reverse_each(&block)
  r = IntegerRangeIterator.new(@range, -@step_size)
  block_given? ? r.each(&block) : r
end

#sizeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



283
284
285
# File 'lib/puppet/pops/types/iterable.rb', line 283

def size
  (@range.to - @range.from) / @step_size.abs
end

#step_iterator(step) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/puppet/pops/types/iterable.rb', line 287

def step_iterator(step)
  # The step iterator must use a range that has its logical end truncated at an even step boundary. This will
  # fulfil two objectives:
  # 1. The element_type method should not report excessive integers as possible numbers
  # 2. A reversed iterator must start at the correct number
  #
  range = @range
  step = @step_size * step
  mod = (range.to - range.from) % step
  if mod < 0
    range = PIntegerType.new(range.from - mod, range.to)
  elsif mod > 0
    range = PIntegerType.new(range.from, range.to - mod)
  end
  IntegerRangeIterator.new(range, step)
end

#unbounded?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


304
305
306
# File 'lib/puppet/pops/types/iterable.rb', line 304

def unbounded?
  false
end