Class: Puppet::Pops::Types::PNumericType

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

Direct Known Subclasses

PFloatType, PIntegerType

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PScalarType

#roundtrip_with_string?

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, #create, create, #generalize, #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

#initialize(from, to = Float::INFINITY) ⇒ PNumericType

Returns a new instance of PNumericType

Raises:

  • (ArgumentError)

939
940
941
942
943
944
945
# File 'lib/puppet/pops/types/types.rb', line 939

def initialize(from, to = Float::INFINITY)
  from = -Float::INFINITY if from.nil? || from == :default
  to = Float::INFINITY if to.nil? || to == :default
  raise ArgumentError, "'from' must be less or equal to 'to'. Got (#{from}, #{to}" if from > to
  @from = from
  @to = to
end

Class Method Details

.new_function(type) ⇒ Object


867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
# File 'lib/puppet/pops/types/types.rb', line 867

def self.new_function(type)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_numeric, type.loader) do
    local_types do
      type "Convertible = Variant[Integer, Float, 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
        from
      when Time::TimeData
        from.to_f
      when TrueClass
        1
      when FalseClass
        0
      else
        begin
          if from[0] == '0'
            second_char = (from[1] || '').downcase
            if second_char == 'b' || second_char == 'x'
              # use built in conversion
              return Integer(from)
            end
          end

          Puppet::Pops::Utils.to_n(from)
        rescue TypeError => e
          raise TypeConversionError.new(e.message)
        rescue ArgumentError => e
          raise TypeConversionError.new(e.message)
        end
      end
    end

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

.register_ptype(loader, ir) ⇒ Object


860
861
862
863
864
865
# File 'lib/puppet/pops/types/types.rb', line 860

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'ScalarDataType',
    'from' => { KEY_TYPE => POptionalType.new(PNumericType::DEFAULT), KEY_VALUE => nil },
    'to' => { KEY_TYPE => POptionalType.new(PNumericType::DEFAULT), KEY_VALUE => nil }
  )
end

Instance Method Details

#eql?(o) ⇒ Boolean


984
985
986
# File 'lib/puppet/pops/types/types.rb', line 984

def eql?(o)
  self.class == o.class && @from == o.numeric_from && @to == o.numeric_to
end

#fromFloat, Integer

Returns the lower bound of the numeric range or `nil` if no lower bound is set.


958
959
960
# File 'lib/puppet/pops/types/types.rb', line 958

def from
  @from == -Float::INFINITY ? nil : @from
end

#hashObject


980
981
982
# File 'lib/puppet/pops/types/types.rb', line 980

def hash
  @from.hash ^ @to.hash
end

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


988
989
990
# File 'lib/puppet/pops/types/types.rb', line 988

def instance?(o, guard = nil)
  (o.is_a?(Float) || o.is_a?(Integer)) && o >= @from && o <= @to
end

#intersect?(o) ⇒ Boolean

Checks if this numeric range intersects with another


952
953
954
# File 'lib/puppet/pops/types/types.rb', line 952

def intersect?(o)
  self.class == o.class && !(@to < o.numeric_from || o.numeric_to < @from)
end

#numeric_fromFloat, Integer

Same as #from but will return `-Float::Infinity` instead of `nil` if no lower bound is set.


970
971
972
# File 'lib/puppet/pops/types/types.rb', line 970

def numeric_from
  @from
end

#numeric_toFloat, Integer

Same as #to but will return `Float::Infinity` instead of `nil` if no lower bound is set.


976
977
978
# File 'lib/puppet/pops/types/types.rb', line 976

def numeric_to
  @to
end

#toFloat, Integer

Returns the upper bound of the numeric range or `nil` if no upper bound is set.


964
965
966
# File 'lib/puppet/pops/types/types.rb', line 964

def to
  @to == Float::INFINITY ? nil : @to
end

#unbounded?Boolean


992
993
994
# File 'lib/puppet/pops/types/types.rb', line 992

def unbounded?
  @from == -Float::INFINITY && @to == Float::INFINITY
end