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 PAbstractRangeType

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

Methods inherited from PAnyType

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

Methods inherited from TypedModelObject

_ptype, create_ptype, register_ptypes

Methods included from Visitable

#accept

Methods included from PuppetObject

#_ptype

Constructor Details

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

Class Method Details

.new_function(_, loader) ⇒ Object

Returns a new function that produces a Float value



1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
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
# File 'lib/puppet/pops/types/types.rb', line 1114

def self.new_function(_, loader)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_float, 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



1084
1085
1086
# File 'lib/puppet/pops/types/types.rb', line 1084

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

Instance Method Details

#generalizeObject



1088
1089
1090
# File 'lib/puppet/pops/types/types.rb', line 1088

def generalize
  DEFAULT
end

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

Returns:

  • (Boolean)


1092
1093
1094
# File 'lib/puppet/pops/types/types.rb', line 1092

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



1102
1103
1104
1105
1106
1107
1108
1109
1110
# File 'lib/puppet/pops/types/types.rb', line 1102

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