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

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

Constant Summary collapse

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

Instance Method Summary collapse

Methods inherited from PNumericType

#eql?, #from, #hash, #initialize, #intersect?, #numeric_from, #numeric_to, #to, #unbounded?

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #eql?, #hash, #kind_of_callable?, #normalize, #simple_name, #to_alias_expanded_s, #to_s

Methods included from Visitable

#accept

Constructor Details

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

Instance Method Details

#adjacent?(o) ⇒ Boolean

Checks if this range is adjacent to the given range

Parameters:

Returns:

  • (Boolean)

    ‘true` if this range is adjacent to the other range



690
691
692
# File 'lib/puppet/pops/types/types.rb', line 690

def adjacent?(o)
  o.is_a?(PIntegerType) &&  (@to + 1 == o.from || o.to + 1 == @from)
end

#each(&block) ⇒ Object

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



733
734
735
736
# File 'lib/puppet/pops/types/types.rb', line 733

def each(&block)
  r = Iterable.on(self)
  block_given? ? r.each(&block) : r
end

#finite_range?Boolean

Will respond ‘true` for any range that is bounded at both ends.

Returns:

  • (Boolean)

    ‘true` if the type describes a finite range.



673
674
675
# File 'lib/puppet/pops/types/types.rb', line 673

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

#generalizeObject



677
678
679
# File 'lib/puppet/pops/types/types.rb', line 677

def generalize
  DEFAULT
end

#instance?(o) ⇒ Boolean

Returns:

  • (Boolean)


681
682
683
# File 'lib/puppet/pops/types/types.rb', line 681

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

#iterable?(guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


710
711
712
# File 'lib/puppet/pops/types/types.rb', line 710

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object



714
715
716
717
# File 'lib/puppet/pops/types/types.rb', line 714

def iterable_type(guard = nil)
  # It's unknown if the iterable will be a range (min, max) or a "times" (0, max)
  PIterableType.new(PIntegerType::DEFAULT)
end

#merge(o) ⇒ PIntegerType?

Concatenates this range with another range provided that the ranges intersect or are adjacent. When that’s not the case, this method will return ‘nil`

Parameters:

  • o (PIntegerType)

    the range to concatenate with this range

Returns:

  • (PIntegerType, nil)

    the concatenated range or ‘nil` when the ranges were apart



700
701
702
703
704
705
706
707
708
# File 'lib/puppet/pops/types/types.rb', line 700

def merge(o)
  if intersect?(o) || adjacent?(o)
    min = @from <= o.from ? @from : o.from
    max = @to >= o.to ? @to : o.to
    PIntegerType.new(min, max)
  else
    nil
  end
end

#rangeObject

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



727
728
729
# File 'lib/puppet/pops/types/types.rb', line 727

def range
  [@from, @to]
end

#sizeObject

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



720
721
722
723
# File 'lib/puppet/pops/types/types.rb', line 720

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:



741
742
743
# File 'lib/puppet/pops/types/types.rb', line 741

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