class Puppet::Pops::Types::PIntegerType

@api public

Constants

DEFAULT

Public Class Methods

register_ptype(loader, ir) click to toggle source
     # File lib/puppet/pops/types/types.rb
1011 def self.register_ptype(loader, ir)
1012   create_ptype(loader, ir, 'NumericType')
1013 end

Public Instance Methods

adjacent?(o) click to toggle source

Checks if this range is adjacent to the given range

@param o [PIntegerType] the range to compare with @return [Boolean] ‘true` if this range is adjacent to the other range @api public

     # File lib/puppet/pops/types/types.rb
1035 def adjacent?(o)
1036   o.is_a?(PIntegerType) &&  (@to + 1 == o.from || o.to + 1 == @from)
1037 end
assert_radix(radix) click to toggle source
     # File lib/puppet/pops/types/types.rb
1178 def assert_radix(radix)
1179   case radix
1180   when 2, 8, 10, 16
1181   else
1182     raise ArgumentError.new(_("Illegal radix: %{radix}, expected 2, 8, 10, 16, or default") % { radix: radix })
1183   end
1184   radix
1185 end
each(&block) click to toggle source

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

     # File lib/puppet/pops/types/types.rb
1078 def each(&block)
1079   r = Iterable.on(self)
1080   block_given? ? r.each(&block) : r
1081 end
finite_range?() click to toggle source

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

@return [Boolean] ‘true` if the type describes a finite range.

     # File lib/puppet/pops/types/types.rb
1018 def finite_range?
1019   @from != -Float::INFINITY && @to != Float::INFINITY
1020 end
from_args(from, radix = :default, abs = false) click to toggle source
     # File lib/puppet/pops/types/types.rb
1118 def from_args(from, radix = :default, abs = false)
1119   result = from_convertible(from, radix)
1120   abs ? result.abs : result
1121 end
from_convertible(from, radix) click to toggle source
     # File lib/puppet/pops/types/types.rb
1127 def from_convertible(from, radix)
1128   case from
1129   when Float, Time::TimeData
1130     from.to_i
1131   when Integer
1132     from
1133   when TrueClass
1134     1
1135   when FalseClass
1136     0
1137   else
1138     begin
1139       radix == :default ? Integer(from) : Integer(from, radix)
1140     rescue TypeError => e
1141       raise TypeConversionError.new(e.message)
1142     rescue ArgumentError => e
1143       # Test for special case where there is whitespace between sign and number
1144       match = Patterns::WS_BETWEEN_SIGN_AND_NUMBER.match(from)
1145       if match
1146         begin
1147           # Try again, this time with whitespace removed
1148           return from_args(match[1] + match[2], radix)
1149         rescue TypeConversionError
1150           # Ignored to retain original error
1151         end
1152       end
1153       raise TypeConversionError.new(e.message)
1154     end
1155   end
1156 end
from_hash(args_hash) click to toggle source
     # File lib/puppet/pops/types/types.rb
1123 def from_hash(args_hash)
1124   from_args(args_hash['from'], args_hash['radix'] || :default, args_hash['abs'] || false)
1125 end
generalize() click to toggle source
     # File lib/puppet/pops/types/types.rb
1022 def generalize
1023   DEFAULT
1024 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1026 def instance?(o, guard = nil)
1027   o.is_a?(Integer) && o >= numeric_from && o <= numeric_to
1028 end
iterable?(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1055 def iterable?(guard = nil)
1056   true
1057 end
iterable_type(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1059 def iterable_type(guard = nil)
1060   # It's unknown if the iterable will be a range (min, max) or a "times" (0, max)
1061   PIterableType.new(PIntegerType::DEFAULT)
1062 end
merge(o) click to toggle source

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`

@param o [PIntegerType] the range to concatenate with this range @return [PIntegerType,nil] the concatenated range or ‘nil` when the ranges were apart @api public

     # File lib/puppet/pops/types/types.rb
1045 def merge(o)
1046   if intersect?(o) || adjacent?(o)
1047     min = @from <= o.numeric_from ? @from : o.numeric_from
1048     max = @to >= o.numeric_to ? @to : o.numeric_to
1049     PIntegerType.new(min, max)
1050   else
1051     nil
1052   end
1053 end
new_function() click to toggle source
     # File lib/puppet/pops/types/types.rb
1090 def new_function
1091   @@new_function ||= Puppet::Functions.create_loaded_function(:new, loader) do
1092     local_types do
1093       type 'Radix       = Variant[Default, Integer[2,2], Integer[8,8], Integer[10,10], Integer[16,16]]'
1094       type "Convertible = Variant[Numeric, Boolean, Pattern[/#{INTEGER_PATTERN_LENIENT}/], Timespan, Timestamp]"
1095       type 'NamedArgs   = Struct[{from => Convertible, Optional[radix] => Radix, Optional[abs] => Boolean}]'
1096     end
1097 
1098     dispatch :from_args do
1099       param          'Convertible',  :from
1100       optional_param 'Radix',   :radix
1101       optional_param 'Boolean', :abs
1102     end
1103 
1104     dispatch :from_hash do
1105       param          'NamedArgs',  :hash_args
1106     end
1107 
1108     argument_mismatch :on_error_hash do
1109       param          'Hash',  :hash_args
1110     end
1111 
1112     argument_mismatch :on_error do
1113       param          'Any',     :from
1114       optional_param 'Integer', :radix
1115       optional_param 'Boolean', :abs
1116     end
1117 
1118     def from_args(from, radix = :default, abs = false)
1119       result = from_convertible(from, radix)
1120       abs ? result.abs : result
1121     end
1122 
1123     def from_hash(args_hash)
1124       from_args(args_hash['from'], args_hash['radix'] || :default, args_hash['abs'] || false)
1125     end
1126 
1127     def from_convertible(from, radix)
1128       case from
1129       when Float, Time::TimeData
1130         from.to_i
1131       when Integer
1132         from
1133       when TrueClass
1134         1
1135       when FalseClass
1136         0
1137       else
1138         begin
1139           radix == :default ? Integer(from) : Integer(from, radix)
1140         rescue TypeError => e
1141           raise TypeConversionError.new(e.message)
1142         rescue ArgumentError => e
1143           # Test for special case where there is whitespace between sign and number
1144           match = Patterns::WS_BETWEEN_SIGN_AND_NUMBER.match(from)
1145           if match
1146             begin
1147               # Try again, this time with whitespace removed
1148               return from_args(match[1] + match[2], radix)
1149             rescue TypeConversionError
1150               # Ignored to retain original error
1151             end
1152           end
1153           raise TypeConversionError.new(e.message)
1154         end
1155       end
1156     end
1157 
1158     def on_error_hash(args_hash)
1159       if args_hash.include?('from')
1160         from = args_hash['from']
1161         return on_error(from) unless loader.load(:type, 'convertible').instance?(from)
1162       end
1163       radix = args_hash['radix']
1164       assert_radix(radix) unless radix.nil? || radix == :default
1165       TypeAsserter.assert_instance_of('Integer.new', loader.load(:type, 'namedargs'), args_hash)
1166     end
1167 
1168     def on_error(from, radix = :default, abs = nil)
1169       assert_radix(radix) unless radix == :default
1170       if from.is_a?(String)
1171         _("The string '%{str}' cannot be converted to Integer") % { str: from }
1172       else
1173         t = TypeCalculator.singleton.infer(from).generalize
1174         _("Value of type %{type} cannot be converted to Integer") % { type: t }
1175       end
1176     end
1177 
1178     def assert_radix(radix)
1179       case radix
1180       when 2, 8, 10, 16
1181       else
1182         raise ArgumentError.new(_("Illegal radix: %{radix}, expected 2, 8, 10, 16, or default") % { radix: radix })
1183       end
1184       radix
1185     end
1186 
1187   end
1188 end
on_error(from, radix = :default, abs = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1168 def on_error(from, radix = :default, abs = nil)
1169   assert_radix(radix) unless radix == :default
1170   if from.is_a?(String)
1171     _("The string '%{str}' cannot be converted to Integer") % { str: from }
1172   else
1173     t = TypeCalculator.singleton.infer(from).generalize
1174     _("Value of type %{type} cannot be converted to Integer") % { type: t }
1175   end
1176 end
on_error_hash(args_hash) click to toggle source
     # File lib/puppet/pops/types/types.rb
1158 def on_error_hash(args_hash)
1159   if args_hash.include?('from')
1160     from = args_hash['from']
1161     return on_error(from) unless loader.load(:type, 'convertible').instance?(from)
1162   end
1163   radix = args_hash['radix']
1164   assert_radix(radix) unless radix.nil? || radix == :default
1165   TypeAsserter.assert_instance_of('Integer.new', loader.load(:type, 'namedargs'), args_hash)
1166 end
range() click to toggle source

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

     # File lib/puppet/pops/types/types.rb
1072 def range
1073   [@from, @to]
1074 end
size() click to toggle source

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

     # File lib/puppet/pops/types/types.rb
1065 def size
1066   return Float::INFINITY if @from == -Float::INFINITY || @to == Float::INFINITY
1067   1+(to-from).abs
1068 end
to_size() click to toggle source

Returns a range where both to and from are positive numbers. Negative numbers are converted to zero @return [PIntegerType] a positive range

     # File lib/puppet/pops/types/types.rb
1086 def to_size
1087   @from >= 0 ? self : PIntegerType.new(0, @to < 0 ? 0 : @to)
1088 end