Class: Puppet::Pops::Types::PAbstractRangeType

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

Overview

Abstract class that encapsulates behavior common to PNumericType and PAbstractTimeDataType

Direct Known Subclasses

PAbstractTimeDataType, PNumericType

Constant Summary

Constants inherited from PScalarType

Puppet::Pops::Types::PScalarType::DEFAULT

Constants inherited from PAnyType

Puppet::Pops::Types::PAnyType::DEFAULT

Instance Method Summary collapse

Methods inherited from PScalarType

register_ptype

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #check_self_recursion, #generalize, #iterable?, #iterable_type, #kind_of_callable?, #name, new_function, #new_function, #normalize, #really_instance?, register_ptype, #resolve, #simple_name, simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_ptype, create_ptype, register_ptypes

Methods included from Visitable

#accept

Methods included from PuppetObject

#_ptype

Constructor Details

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

Returns a new instance of PAbstractRangeType.

Raises:

  • (ArgumentError)


747
748
749
750
751
752
753
# File 'lib/puppet/pops/types/types.rb', line 747

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)


792
793
794
# File 'lib/puppet/pops/types/types.rb', line 792

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)


766
767
768
# File 'lib/puppet/pops/types/types.rb', line 766

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

#hashObject



788
789
790
# File 'lib/puppet/pops/types/types.rb', line 788

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

#instance?(o, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


796
797
798
# File 'lib/puppet/pops/types/types.rb', line 796

def instance?(o, guard = nil)
  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



760
761
762
# File 'lib/puppet/pops/types/types.rb', line 760

def intersect?(o)
  self.class == o.class && !(@to < o.numeric_from || o.numeric_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)


778
779
780
# File 'lib/puppet/pops/types/types.rb', line 778

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)


784
785
786
# File 'lib/puppet/pops/types/types.rb', line 784

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)


772
773
774
# File 'lib/puppet/pops/types/types.rb', line 772

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

#unbounded?Boolean

Returns:

  • (Boolean)


800
801
802
# File 'lib/puppet/pops/types/types.rb', line 800

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