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, #create, #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)


762
763
764
765
766
767
768
# File 'lib/puppet/pops/types/types.rb', line 762

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)


807
808
809
# File 'lib/puppet/pops/types/types.rb', line 807

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)


781
782
783
# File 'lib/puppet/pops/types/types.rb', line 781

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

#hashObject



803
804
805
# File 'lib/puppet/pops/types/types.rb', line 803

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

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

Returns:

  • (Boolean)


811
812
813
# File 'lib/puppet/pops/types/types.rb', line 811

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



775
776
777
# File 'lib/puppet/pops/types/types.rb', line 775

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)


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

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)


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

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)


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

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

#unbounded?Boolean

Returns:

  • (Boolean)


815
816
817
# File 'lib/puppet/pops/types/types.rb', line 815

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