Class: Puppet::Pops::Types::PIntegerType

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

Overview

API:

  • public

Constant Summary collapse

DEFAULT =

API:

  • public

PIntegerType.new(-Float::INFINITY)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PNumericType

new_function

Methods inherited from PAbstractRangeType

#eql?, #from, #hash, #initialize, #intersect?, #numeric_from, #numeric_to, #to, #unbounded?

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #check_self_recursion, #create, #eql?, #hash, #kind_of_callable?, #name, new_function, #normalize, #really_instance?, #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

This class inherits a constructor from Puppet::Pops::Types::PAbstractRangeType

Class Method Details

.register_ptype(loader, ir) ⇒ Object

API:

  • public



904
905
906
# File 'lib/puppet/pops/types/types.rb', line 904

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'NumericType')
end

Instance Method Details

#adjacent?(o) ⇒ Boolean

Checks if this range is adjacent to the given range

Parameters:

  • the range to compare with

Returns:

  • ‘true` if this range is adjacent to the other range

API:

  • public



928
929
930
# File 'lib/puppet/pops/types/types.rb', line 928

def adjacent?(o)
  o.is_a?(PIntegerType) &&  (@to + 1 == o.from || o.to + 1 == @from)
end

#each(&block) ⇒ Object

Returns Enumerator if no block is given Returns nil if size is infinity (does not yield)

API:

  • public



971
972
973
974
# File 'lib/puppet/pops/types/types.rb', line 971

def each(&block)
  r = Iterable.on(self)
  block_given? ? r.each(&block) : r
end

#finite_range?Boolean

Will respond ‘true` for any range that is bounded at both ends.

Returns:

  • ‘true` if the type describes a finite range.

API:

  • public



911
912
913
# File 'lib/puppet/pops/types/types.rb', line 911

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

#generalizeObject

API:

  • public



915
916
917
# File 'lib/puppet/pops/types/types.rb', line 915

def generalize
  DEFAULT
end

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

Returns:

API:

  • public



919
920
921
# File 'lib/puppet/pops/types/types.rb', line 919

def instance?(o, guard = nil)
  o.is_a?(Integer) && o >= numeric_from && o <= numeric_to
end

#iterable?(guard = nil) ⇒ Boolean

Returns:

API:

  • public



948
949
950
# File 'lib/puppet/pops/types/types.rb', line 948

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object

API:

  • public



952
953
954
955
# File 'lib/puppet/pops/types/types.rb', line 952

def iterable_type(guard = nil)
  # It's unknown if the iterable will be a range (min, max) or a "times" (0, max)
  PIterableType.new(PIntegerType::DEFAULT)
end

#merge(o) ⇒ PIntegerType?

Concatenates this range with another range provided that the ranges intersect or are adjacent. When that’s not the case, this method will return ‘nil`

Parameters:

  • the range to concatenate with this range

Returns:

  • the concatenated range or ‘nil` when the ranges were apart

API:

  • public



938
939
940
941
942
943
944
945
946
# File 'lib/puppet/pops/types/types.rb', line 938

def merge(o)
  if intersect?(o) || adjacent?(o)
    min = @from <= o.numeric_from ? @from : o.numeric_from
    max = @to >= o.numeric_to ? @to : o.numeric_to
    PIntegerType.new(min, max)
  else
    nil
  end
end

#new_function(loader) ⇒ Object

API:

  • public



983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
# File 'lib/puppet/pops/types/types.rb', line 983

def new_function(loader)
  @@new_function ||= Puppet::Functions.create_loaded_function(:new, loader) do
    local_types do
      type 'Radix       = Variant[Default, Integer[2,2], Integer[8,8], Integer[10,10], Integer[16,16]]'
      type 'Convertible = Variant[Undef, Numeric, Boolean, String, Timespan, Timestamp]'
      type 'NamedArgs   = Struct[{from => Convertible, Optional[radix] => Radix, Optional[abs] => Boolean}]'
    end

    dispatch :from_args do
      param          'Convertible',  :from
      optional_param 'Radix',   :radix
      optional_param 'Boolean', :abs
    end

    dispatch :from_hash do
      param          'NamedArgs',  :hash_args
    end

    def from_args(from, radix = :default, abs = false)
      result = from_convertible(from, radix)
      abs ? result.abs : result
    end

    def from_hash(args_hash)
      from_args(args_hash['from'], args_hash['radix'] || :default, args_hash['abs'] || false)
    end

    def from_convertible(from, radix)
      case from
      when NilClass
        throw :undefined_value
      when Float, Time::TimeData
        from.to_i
      when Integer
        from
      when TrueClass
        1
      when FalseClass
        0
      when String
        begin
          radix == :default ? Integer(from) : Integer(from, assert_radix(radix))
        rescue TypeError => e
          raise TypeConversionError.new(e.message)
        rescue ArgumentError => e
          # Test for special case where there is whitespace between sign and number
          match = Patterns::WS_BETWEEN_SIGN_AND_NUMBER.match(from)
          if match
            begin
              # Try again, this time with whitespace removed
              return from_args(match[1] + match[2], radix)
            rescue TypeConversionError
              # Ignored to retain original error
            end
          end
          raise TypeConversionError.new(e.message)
        end
      else
        t = Puppet::Pops::Types::TypeCalculator.singleton.infer(from).generalize
        raise TypeConversionError.new("Value of type '#{t}' cannot be converted to an Integer")
      end
    end

    def assert_radix(radix)
      case radix
      when 2, 8, 10, 16, :default
      else
        raise ArgumentError.new("Illegal radix: '#{radix}', expected 2, 8, 10, 16, or default")
      end
      radix
    end

  end
end

#rangeObject

Returns the range as an array ordered so the smaller number is always first. The number may be Infinity or -Infinity.

API:

  • public



965
966
967
# File 'lib/puppet/pops/types/types.rb', line 965

def range
  [@from, @to]
end

#sizeObject

Returns Float.Infinity if one end of the range is unbound

API:

  • public



958
959
960
961
# File 'lib/puppet/pops/types/types.rb', line 958

def size
  return Float::INFINITY if @from == -Float::INFINITY || @to == Float::INFINITY
  1+(to-from).abs
end

#to_sizePIntegerType

Returns a range where both to and from are positive numbers. Negative numbers are converted to zero

Returns:

  • a positive range

API:

  • public



979
980
981
# File 'lib/puppet/pops/types/types.rb', line 979

def to_size
  @from >= 0 ? self : PIntegerType.new(0, @to < 0 ? 0 : @to)
end