class Puppet::Pops::Types::PFloatType

@api public

Constants

DEFAULT

Public Class Methods

new_function(type) click to toggle source

Returns a new function that produces a Float value

     # File lib/puppet/pops/types/types.rb
1225 def self.new_function(type)
1226   @new_function ||= Puppet::Functions.create_loaded_function(:new_float, type.loader) do
1227     local_types do
1228       type "Convertible = Variant[Numeric, Boolean, Pattern[/#{FLOAT_PATTERN}/], Timespan, Timestamp]"
1229       type 'NamedArgs   = Struct[{from => Convertible, Optional[abs] => Boolean}]'
1230     end
1231 
1232     dispatch :from_args do
1233       param          'Convertible',  :from
1234       optional_param 'Boolean',      :abs
1235     end
1236 
1237     dispatch :from_hash do
1238       param          'NamedArgs',  :hash_args
1239     end
1240 
1241     argument_mismatch :on_error do
1242       param          'Any',     :from
1243       optional_param 'Boolean', :abs
1244     end
1245 
1246     def from_args(from, abs = false)
1247       result = from_convertible(from)
1248       abs ? result.abs : result
1249     end
1250 
1251     def from_hash(args_hash)
1252       from_args(args_hash['from'], args_hash['abs'] || false)
1253     end
1254 
1255     def from_convertible(from)
1256       case from
1257       when Float
1258         from
1259       when Integer
1260         Float(from)
1261       when Time::TimeData
1262         from.to_f
1263       when TrueClass
1264         1.0
1265       when FalseClass
1266         0.0
1267       else
1268         begin
1269           # support a binary as float
1270           if from[0] == '0' && from[1].casecmp('b').zero?
1271             from = Integer(from)
1272           end
1273           Float(from)
1274         rescue TypeError => e
1275           raise TypeConversionError.new(e.message)
1276         rescue ArgumentError => e
1277           # Test for special case where there is whitespace between sign and number
1278           match = Patterns::WS_BETWEEN_SIGN_AND_NUMBER.match(from)
1279           if match
1280             begin
1281               # Try again, this time with whitespace removed
1282               return from_args(match[1] + match[2])
1283             rescue TypeConversionError
1284               # Ignored to retain original error
1285             end
1286           end
1287           raise TypeConversionError.new(e.message)
1288         end
1289       end
1290     end
1291 
1292     def on_error(from, _ = false)
1293       if from.is_a?(String)
1294         _("The string '%{str}' cannot be converted to Float") % { str: from }
1295       else
1296         t = TypeCalculator.singleton.infer(from).generalize
1297         _("Value of type %{type} cannot be converted to Float") % { type: t }
1298       end
1299     end
1300   end
1301 end
register_ptype(loader, ir) click to toggle source
     # File lib/puppet/pops/types/types.rb
1195 def self.register_ptype(loader, ir)
1196   create_ptype(loader, ir, 'NumericType')
1197 end

Public Instance Methods

from_args(from, abs = false) click to toggle source
     # File lib/puppet/pops/types/types.rb
1246 def from_args(from, abs = false)
1247   result = from_convertible(from)
1248   abs ? result.abs : result
1249 end
from_convertible(from) click to toggle source
     # File lib/puppet/pops/types/types.rb
1255 def from_convertible(from)
1256   case from
1257   when Float
1258     from
1259   when Integer
1260     Float(from)
1261   when Time::TimeData
1262     from.to_f
1263   when TrueClass
1264     1.0
1265   when FalseClass
1266     0.0
1267   else
1268     begin
1269       # support a binary as float
1270       if from[0] == '0' && from[1].casecmp('b').zero?
1271         from = Integer(from)
1272       end
1273       Float(from)
1274     rescue TypeError => e
1275       raise TypeConversionError.new(e.message)
1276     rescue ArgumentError => e
1277       # Test for special case where there is whitespace between sign and number
1278       match = Patterns::WS_BETWEEN_SIGN_AND_NUMBER.match(from)
1279       if match
1280         begin
1281           # Try again, this time with whitespace removed
1282           return from_args(match[1] + match[2])
1283         rescue TypeConversionError
1284           # Ignored to retain original error
1285         end
1286       end
1287       raise TypeConversionError.new(e.message)
1288     end
1289   end
1290 end
from_hash(args_hash) click to toggle source
     # File lib/puppet/pops/types/types.rb
1251 def from_hash(args_hash)
1252   from_args(args_hash['from'], args_hash['abs'] || false)
1253 end
generalize() click to toggle source
     # File lib/puppet/pops/types/types.rb
1199 def generalize
1200   DEFAULT
1201 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1203 def instance?(o, guard = nil)
1204   o.is_a?(Float) && o >= numeric_from && o <= numeric_to
1205 end
merge(o) click to toggle source

Concatenates this range with another range provided that the ranges intersect. When that's not the case, this method will return `nil`

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

     # File lib/puppet/pops/types/types.rb
1213 def merge(o)
1214   if intersect?(o)
1215     min = @from <= o.from ? @from : o.from
1216     max = @to >= o.to ? @to : o.to
1217     PFloatType.new(min, max)
1218   else
1219     nil
1220   end
1221 end
on_error(from, _ = false) click to toggle source
     # File lib/puppet/pops/types/types.rb
1292 def on_error(from, _ = false)
1293   if from.is_a?(String)
1294     _("The string '%{str}' cannot be converted to Float") % { str: from }
1295   else
1296     t = TypeCalculator.singleton.infer(from).generalize
1297     _("Value of type %{type} cannot be converted to Float") % { type: t }
1298   end
1299 end