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?, #check_self_recursion, #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



1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
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
# File 'lib/puppet/pops/types/types.rb', line 1079

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



1049
1050
1051
# File 'lib/puppet/pops/types/types.rb', line 1049

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

Instance Method Details

#generalizeObject



1053
1054
1055
# File 'lib/puppet/pops/types/types.rb', line 1053

def generalize
  DEFAULT
end

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

Returns:

  • (Boolean)


1057
1058
1059
# File 'lib/puppet/pops/types/types.rb', line 1057

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



1067
1068
1069
1070
1071
1072
1073
1074
1075
# File 'lib/puppet/pops/types/types.rb', line 1067

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