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

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

     # File lib/puppet/pops/types/types.rb
1064 def size
1065   return Float::INFINITY if @from == -Float::INFINITY || @to == Float::INFINITY
1066   1+(to-from).abs
1067 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
1085 def to_size
1086   @from >= 0 ? self : PIntegerType.new(0, @to < 0 ? 0 : @to)
1087 end