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

Public Instance Methods

from_args(from, abs = false) click to toggle source
     # File lib/puppet/pops/types/types.rb
1247 def from_args(from, abs = false)
1248   result = from_convertible(from)
1249   abs ? result.abs : result
1250 end
from_convertible(from) click to toggle source
     # File lib/puppet/pops/types/types.rb
1256 def from_convertible(from)
1257   case from
1258   when Float
1259     from
1260   when Integer
1261     Float(from)
1262   when Time::TimeData
1263     from.to_f
1264   when TrueClass
1265     1.0
1266   when FalseClass
1267     0.0
1268   else
1269     begin
1270       # support a binary as float
1271       if from[0] == '0' && from[1].casecmp('b').zero?
1272         from = Integer(from)
1273       end
1274       Float(from)
1275     rescue TypeError => e
1276       raise TypeConversionError.new(e.message)
1277     rescue ArgumentError => e
1278       # Test for special case where there is whitespace between sign and number
1279       match = Patterns::WS_BETWEEN_SIGN_AND_NUMBER.match(from)
1280       if match
1281         begin
1282           # Try again, this time with whitespace removed
1283           return from_args(match[1] + match[2])
1284         rescue TypeConversionError
1285           # Ignored to retain original error
1286         end
1287       end
1288       raise TypeConversionError.new(e.message)
1289     end
1290   end
1291 end
from_hash(args_hash) click to toggle source
     # File lib/puppet/pops/types/types.rb
1252 def from_hash(args_hash)
1253   from_args(args_hash['from'], args_hash['abs'] || false)
1254 end
generalize() click to toggle source
     # File lib/puppet/pops/types/types.rb
1200 def generalize
1201   DEFAULT
1202 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1204 def instance?(o, guard = nil)
1205   o.is_a?(Float) && o >= numeric_from && o <= numeric_to
1206 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
1214 def merge(o)
1215   if intersect?(o)
1216     min = @from <= o.from ? @from : o.from
1217     max = @to >= o.to ? @to : o.to
1218     PFloatType.new(min, max)
1219   else
1220     nil
1221   end
1222 end
on_error(from, _ = false) click to toggle source
     # File lib/puppet/pops/types/types.rb
1293 def on_error(from, _ = false)
1294   if from.is_a?(String)
1295     _("The string '%{str}' cannot be converted to Float") % { str: from }
1296   else
1297     t = TypeCalculator.singleton.infer(from).generalize
1298     _("Value of type %{type} cannot be converted to Float") % { type: t }
1299   end
1300 end