Class: Puppet::Pops::Types::PNumericType
- Inherits:
-
PScalarType
- Object
- TypedModelObject
- PAnyType
- PScalarType
- Puppet::Pops::Types::PNumericType
- Defined in:
- lib/puppet/pops/types/types.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT =
PNumericType.new(-Float::INFINITY)
Instance Method Summary collapse
- #eql?(o) ⇒ Boolean
-
#from ⇒ Float, Integer
Returns the lower bound of the numeric range or ‘nil` if no lower bound is set.
- #hash ⇒ Object
-
#initialize(from, to = Float::INFINITY) ⇒ PNumericType
constructor
A new instance of PNumericType.
- #instance?(o) ⇒ Boolean
-
#intersect?(o) ⇒ Boolean
Checks if this numeric range intersects with another.
-
#numeric_from ⇒ Float, Integer
Same as #from but will return ‘-Float::Infinity` instead of `nil` if no lower bound is set.
-
#numeric_to ⇒ Float, Integer
Same as #to but will return ‘Float::Infinity` instead of `nil` if no lower bound is set.
-
#to ⇒ Float, Integer
Returns the upper bound of the numeric range or ‘nil` if no upper bound is set.
- #unbounded? ⇒ Boolean
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
Constructor Details
#initialize(from, to = Float::INFINITY) ⇒ PNumericType
Returns a new instance of PNumericType.
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
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 |
#from ⇒ Float, Integer
Returns the lower bound of the numeric range or ‘nil` if no lower bound is set.
617 618 619 |
# File 'lib/puppet/pops/types/types.rb', line 617 def from @from == -Float::INFINITY ? nil : @from end |
#hash ⇒ Object
639 640 641 |
# File 'lib/puppet/pops/types/types.rb', line 639 def hash @from.hash ^ @to.hash end |
#instance?(o) ⇒ 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
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_from ⇒ Float, Integer
Same as #from but will return ‘-Float::Infinity` instead of `nil` if no lower bound is set.
629 630 631 |
# File 'lib/puppet/pops/types/types.rb', line 629 def numeric_from @from end |
#numeric_to ⇒ Float, Integer
Same as #to but will return ‘Float::Infinity` instead of `nil` if no lower bound is set.
635 636 637 |
# File 'lib/puppet/pops/types/types.rb', line 635 def numeric_to @to end |
#to ⇒ Float, Integer
Returns the upper bound of the numeric range or ‘nil` if no upper bound is set.
623 624 625 |
# File 'lib/puppet/pops/types/types.rb', line 623 def to @to == Float::INFINITY ? nil : @to end |
#unbounded? ⇒ Boolean
651 652 653 |
# File 'lib/puppet/pops/types/types.rb', line 651 def unbounded? @from == -Float::INFINITY && @to == Float::INFINITY end |