Class: Puppet::Pops::Types::PNumericType
- Inherits:
-
PAbstractRangeType
- Object
- TypedModelObject
- PAnyType
- PScalarType
- PAbstractRangeType
- Puppet::Pops::Types::PNumericType
- Defined in:
- lib/puppet/pops/types/types.rb
Direct Known Subclasses
Class Method Summary collapse
Methods inherited from PAbstractRangeType
#eql?, #from, #hash, #initialize, #instance?, #intersect?, #numeric_from, #numeric_to, #to, #unbounded?
Methods inherited from PScalarType
Methods inherited from PAnyType
#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, #create, #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
Methods included from PuppetObject
Constructor Details
This class inherits a constructor from Puppet::Pops::Types::PAbstractRangeType
Class Method Details
.new_function(_, loader) ⇒ Object
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 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 |
# File 'lib/puppet/pops/types/types.rb', line 850 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.) rescue ArgumentError => e raise TypeConversionError.new(e.) 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
843 844 845 846 847 848 |
# File 'lib/puppet/pops/types/types.rb', line 843 def self.register_ptype(loader, ir) create_ptype(loader, ir, 'ScalarType', 'from' => { KEY_TYPE => POptionalType.new(PNumericType::DEFAULT), KEY_VALUE => nil }, 'to' => { KEY_TYPE => POptionalType.new(PNumericType::DEFAULT), KEY_VALUE => nil } ) end |