Class: Puppet::Pops::Types::PIntegerType

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

Constant Summary collapse

DEFAULT =
PIntegerType.new(-Float::INFINITY)

Instance Method Summary collapse

Methods inherited from PNumericType

#==, #from, #hash, #initialize, #numeric_from, #numeric_to, #to, #unbounded?

Methods inherited from PAnyType

#==, #assignable?, #callable?, #callable_args?, #hash, #kind_of_callable?, #simple_name, #to_s

Methods included from Visitable

#accept

Constructor Details

This class inherits a constructor from Puppet::Pops::Types::PNumericType

Instance Method Details

#eachObject

Returns Enumerator if no block is given Returns nil if size is infinity (does not yield)



521
522
523
524
525
526
527
# File 'lib/puppet/pops/types/types.rb', line 521

def each
  if block_given?
    enumerable? ? @from.upto(@to) { |x| yield x } : nil
  else
    to_enum
  end
end

#enumerable?Boolean

Returns:

  • (Boolean)


495
496
497
# File 'lib/puppet/pops/types/types.rb', line 495

def enumerable?
  @from != -Float::INFINITY && @to != Float::INFINITY
end

#generalizeObject



499
500
501
# File 'lib/puppet/pops/types/types.rb', line 499

def generalize
  DEFAULT
end

#instance?(o) ⇒ Boolean

Returns:

  • (Boolean)


503
504
505
# File 'lib/puppet/pops/types/types.rb', line 503

def instance?(o)
  o.is_a?(Integer) && o >= numeric_from && o <= numeric_to
end

#rangeObject

Returns the range as an array ordered so the smaller number is always first. The number may be Infinity or -Infinity.



515
516
517
# File 'lib/puppet/pops/types/types.rb', line 515

def range
  [@from, @to]
end

#sizeObject

Returns Float.Infinity if one end of the range is unbound



508
509
510
511
# File 'lib/puppet/pops/types/types.rb', line 508

def size
  return Float::INFINITY if @from == -Float::INFINITY || @to == Float::INFINITY
  1+(to-from).abs
end

#to_sizePIntegerType

Returns a range where both to and from are positive numbers. Negative numbers are converted to zero

Returns:



532
533
534
# File 'lib/puppet/pops/types/types.rb', line 532

def to_size
  @from >= 0 ? self : PIntegerType.new(0, @to < 0 ? 0 : @to)
end