Class: Puppet::Pops::Types::PNumericType

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

Direct Known Subclasses

PFloatType, PIntegerType

Constant Summary collapse

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

Instance Method Summary collapse

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #generalize, #iterable?, #iterable_type, #kind_of_callable?, #normalize, #simple_name, #to_alias_expanded_s, #to_s

Methods included from Visitable

#accept

Constructor Details

#initialize(from, to = Float::INFINITY) ⇒ PNumericType

Returns a new instance of PNumericType.

Raises:

  • (ArgumentError)


598
599
600
601
602
603
604
# File 'lib/puppet/pops/types/types.rb', line 598

def initialize(from, to = Float::INFINITY)
  from = -Float::INFINITY if from.nil? || from == :default
  to = Float::INFINITY if to.nil? || to == :default
  raise ArgumentError, "'from' must be less or equal to 'to'. Got (#{from}, #{to}" if from.is_a?(Numeric) && to.is_a?(Numeric) && from > to
  @from = from
  @to = to
end

Instance Method Details

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


643
644
645
# File 'lib/puppet/pops/types/types.rb', line 643

def eql?(o)
  self.class == o.class && @from == o.numeric_from && @to == o.numeric_to
end

#fromFloat, Integer

Returns the lower bound of the numeric range or ‘nil` if no lower bound is set.

Returns:

  • (Float, Integer)


617
618
619
# File 'lib/puppet/pops/types/types.rb', line 617

def from
  @from == -Float::INFINITY ? nil : @from
end

#hashObject



639
640
641
# File 'lib/puppet/pops/types/types.rb', line 639

def hash
  @from.hash ^ @to.hash
end

#instance?(o) ⇒ Boolean

Returns:

  • (Boolean)


647
648
649
# File 'lib/puppet/pops/types/types.rb', line 647

def instance?(o)
  o.is_a?(Numeric) && o >= @from && o <= @to
end

#intersect?(o) ⇒ Boolean

Checks if this numeric range intersects with another

Parameters:

Returns:

  • (Boolean)

    ‘true` if this range intersects with the other range



611
612
613
# File 'lib/puppet/pops/types/types.rb', line 611

def intersect?(o)
  self.class == o.class && !(@to < o.from || o.to < @from)
end

#numeric_fromFloat, Integer

Same as #from but will return ‘-Float::Infinity` instead of `nil` if no lower bound is set.

Returns:

  • (Float, Integer)


629
630
631
# File 'lib/puppet/pops/types/types.rb', line 629

def numeric_from
  @from
end

#numeric_toFloat, Integer

Same as #to but will return ‘Float::Infinity` instead of `nil` if no lower bound is set.

Returns:

  • (Float, Integer)


635
636
637
# File 'lib/puppet/pops/types/types.rb', line 635

def numeric_to
  @to
end

#toFloat, Integer

Returns the upper bound of the numeric range or ‘nil` if no upper bound is set.

Returns:

  • (Float, Integer)


623
624
625
# File 'lib/puppet/pops/types/types.rb', line 623

def to
  @to == Float::INFINITY ? nil : @to
end

#unbounded?Boolean

Returns:

  • (Boolean)


651
652
653
# File 'lib/puppet/pops/types/types.rb', line 651

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