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

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



1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
# File 'lib/puppet/pops/types/types.rb', line 1154

def self.new_function(type)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_float, type.loader) do
    local_types do
      type 'Convertible = Variant[Undef, Numeric, Boolean, String, 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

    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 NilClass
        throw :undefined_value
      when Float
        from
      when Integer
        Float(from)
      when Time::TimeData
        from.to_f
      when TrueClass
        1.0
      when FalseClass
        0.0
      when String
        begin
          # support a binary as float
          if from[0] == '0' && from[1].downcase == 'b'
            from = Integer(from)
          end
          Float(from)
        rescue TypeError => e
          raise TypeConversionError.new(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.new(e.message)
        end
      else
        t = Puppet::Pops::Types::TypeCalculator.singleton.infer(from).generalize
        raise TypeConversionError.new("Value of type '#{t}' cannot be converted to Float")
      end
    end
  end
end

.register_ptype(loader, ir) ⇒ Object



1124
1125
1126
# File 'lib/puppet/pops/types/types.rb', line 1124

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

Instance Method Details

#generalizeObject



1128
1129
1130
# File 'lib/puppet/pops/types/types.rb', line 1128

def generalize
  DEFAULT
end

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

Returns:

  • (Boolean)


1132
1133
1134
# File 'lib/puppet/pops/types/types.rb', line 1132

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



1142
1143
1144
1145
1146
1147
1148
1149
1150
# File 'lib/puppet/pops/types/types.rb', line 1142

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