Class: Puppet::Pops::Types::PFloatType

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

Constant Summary collapse

DEFAULT =
PFloatType.new(-Float::INFINITY)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PNumericType

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

Methods inherited from PScalarType

#roundtrip_with_string?

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #eql?, #hash, #iterable?, #iterable_type, #kind_of_callable?, #loader, #name, #new_function, #normalize, #really_instance?, #resolve, #roundtrip_with_string?, simple_name, #simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_pcore_type, create_ptype, register_ptypes

Methods included from Visitable

#accept

Methods included from PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s

Constructor Details

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

Class Method Details

.new_function(type) ⇒ Object

Returns a new function that produces a Float value



1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
# File 'lib/puppet/pops/types/types.rb', line 1234

def self.new_function(type)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_float, type.loader) do
    local_types do
      type "Convertible = Variant[Numeric, Boolean, Pattern[/#{FLOAT_PATTERN}/], Timespan, Timestamp]"
      type 'NamedArgs   = Struct[{from => Convertible, Optional[abs] => Boolean}]'
    end

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

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

    argument_mismatch :on_error do
      param          'Any',     :from
      optional_param 'Boolean', :abs
    end

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

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

    def from_convertible(from)
      case from
      when Float
        from
      when Integer
        Float(from)
      when Time::TimeData
        from.to_f
      when TrueClass
        1.0
      when FalseClass
        0.0
      else
        begin
          # support a binary as float
          if from[0] == '0' && from[1].casecmp('b').zero?
            from = Integer(from)
          end
          Float(from)
        rescue TypeError => e
          raise TypeConversionError, 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])
            rescue TypeConversionError
              # Ignored to retain original error
            end
          end
          raise TypeConversionError, e.message
        end
      end
    end

    def on_error(from, _ = false)
      if from.is_a?(String)
        _("The string '%{str}' cannot be converted to Float") % { str: from }
      else
        t = TypeCalculator.singleton.infer(from).generalize
        _("Value of type %{type} cannot be converted to Float") % { type: t }
      end
    end
  end
end

.register_ptype(loader, ir) ⇒ Object



1204
1205
1206
# File 'lib/puppet/pops/types/types.rb', line 1204

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

Instance Method Details

#generalizeObject



1208
1209
1210
# File 'lib/puppet/pops/types/types.rb', line 1208

def generalize
  DEFAULT
end

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

Returns:

  • (Boolean)


1212
1213
1214
# File 'lib/puppet/pops/types/types.rb', line 1212

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

#merge(o) ⇒ PFloatType?

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

Parameters:

  • o (PFloatType)

    the range to concatenate with this range

Returns:

  • (PFloatType, nil)

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



1222
1223
1224
1225
1226
1227
1228
1229
1230
# File 'lib/puppet/pops/types/types.rb', line 1222

def merge(o)
  if intersect?(o)
    min = @from <= o.from ? @from : o.from
    max = @to >= o.to ? @to : o.to
    PFloatType.new(min, max)
  else
    nil
  end
end