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

#assignable?, #callable?, #callable_args?, #enumerable?, #generalize, #kind_of_callable?, #simple_name, #to_s

Methods included from Visitable

#accept

Constructor Details

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

Returns a new instance of PNumericType.



423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/puppet/pops/types/types.rb', line 423

def initialize(from, to = Float::INFINITY)
  from = -Float::INFINITY if from.nil? || from == :default
  to = Float::INFINITY if to.nil? || to == :default

  # Always create in right order
  if from <= to
    @from = from
    @to = to
  else
    @to = from
    @from = to
  end
end

Instance Method Details

#==(o) ⇒ Object



465
466
467
# File 'lib/puppet/pops/types/types.rb', line 465

def ==(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.



439
440
441
# File 'lib/puppet/pops/types/types.rb', line 439

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

#hashObject



461
462
463
# File 'lib/puppet/pops/types/types.rb', line 461

def hash
  @from.hash * 31 + @to.hash
end

#instance?(o) ⇒ Boolean



469
470
471
# File 'lib/puppet/pops/types/types.rb', line 469

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

#numeric_fromFloat, Integer

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



451
452
453
# File 'lib/puppet/pops/types/types.rb', line 451

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.



457
458
459
# File 'lib/puppet/pops/types/types.rb', line 457

def numeric_to
  @to
end

#toFloat, Integer

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



445
446
447
# File 'lib/puppet/pops/types/types.rb', line 445

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

#unbounded?Boolean



473
474
475
# File 'lib/puppet/pops/types/types.rb', line 473

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