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

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

Direct Known Subclasses

PFloatType, PIntegerType

Class Method Summary collapse

Methods inherited from PAbstractRangeType

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

Methods inherited from PScalarType

#instance?

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #check_self_recursion, #eql?, #generalize, #hash, #instance?, #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



815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/puppet/pops/types/types.rb', line 815

def self.new_function(_, loader)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_numeric, loader) do
    local_types do
      type 'Convertible = Variant[Undef, Integer, Float, 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
        from
      when Time::TimeData
        from.to_f
      when TrueClass
        1
      when FalseClass
        0
      when String
        begin
          if from[0] == '0' && (from[1].downcase == 'b' || from[1].downcase == 'x')
            Integer(from)
          else
            Puppet::Pops::Utils.to_n(from)
          end
        rescue TypeError => e
          raise TypeConversionError.new(e.message)
        rescue ArgumentError => e
          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 Numeric")
      end
    end
  end
end

.register_ptype(loader, ir) ⇒ Object



808
809
810
811
812
813
# File 'lib/puppet/pops/types/types.rb', line 808

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'ScalarType',
    'from' => { KEY_TYPE => PNumericType::DEFAULT, KEY_VALUE => :default },
    'to' => { KEY_TYPE => PNumericType::DEFAULT, KEY_VALUE => :default }
  )
end