Class: Puppet::Pops::Types::PNumericType
- Inherits:
-
PScalarDataType
- Object
- TypedModelObject
- PAnyType
- PScalarType
- PScalarDataType
- Puppet::Pops::Types::PNumericType
- Defined in:
- lib/puppet/pops/types/types.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
- #eql?(o) ⇒ Boolean
-
#from ⇒ Float, Integer
Returns the lower bound of the numeric range or ‘nil` if no lower bound is set.
- #hash ⇒ Object
-
#initialize(from, to = Float::INFINITY) ⇒ PNumericType
constructor
A new instance of PNumericType.
- #instance?(o, guard = nil) ⇒ Boolean
-
#intersect?(o) ⇒ Boolean
Checks if this numeric range intersects with another.
-
#numeric_from ⇒ Float, Integer
Same as #from but will return ‘-Float::Infinity` instead of `nil` if no lower bound is set.
-
#numeric_to ⇒ Float, Integer
Same as #to but will return ‘Float::Infinity` instead of `nil` if no lower bound is set.
-
#to ⇒ Float, Integer
Returns the upper bound of the numeric range or ‘nil` if no upper bound is set.
- #unbounded? ⇒ Boolean
Methods inherited from PScalarType
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
Methods included from PuppetObject
#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type
Constructor Details
#initialize(from, to = Float::INFINITY) ⇒ PNumericType
Returns a new instance of PNumericType.
892 893 894 895 896 897 898 |
# File 'lib/puppet/pops/types/types.rb', line 892 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
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 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 |
# File 'lib/puppet/pops/types/types.rb', line 833 def self.new_function(type) @new_function ||= Puppet::Functions.create_loaded_function(:new_numeric, type.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
826 827 828 829 830 831 |
# File 'lib/puppet/pops/types/types.rb', line 826 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
937 938 939 |
# File 'lib/puppet/pops/types/types.rb', line 937 def eql?(o) self.class == o.class && @from == o.numeric_from && @to == o.numeric_to end |
#from ⇒ Float, Integer
Returns the lower bound of the numeric range or ‘nil` if no lower bound is set.
911 912 913 |
# File 'lib/puppet/pops/types/types.rb', line 911 def from @from == -Float::INFINITY ? nil : @from end |
#hash ⇒ Object
933 934 935 |
# File 'lib/puppet/pops/types/types.rb', line 933 def hash @from.hash ^ @to.hash end |
#instance?(o, guard = nil) ⇒ Boolean
941 942 943 |
# File 'lib/puppet/pops/types/types.rb', line 941 def instance?(o, guard = nil) o.is_a?(Numeric) && o >= @from && o <= @to end |
#intersect?(o) ⇒ Boolean
Checks if this numeric range intersects with another
905 906 907 |
# File 'lib/puppet/pops/types/types.rb', line 905 def intersect?(o) self.class == o.class && !(@to < o.numeric_from || o.numeric_to < @from) end |
#numeric_from ⇒ Float, Integer
Same as #from but will return ‘-Float::Infinity` instead of `nil` if no lower bound is set.
923 924 925 |
# File 'lib/puppet/pops/types/types.rb', line 923 def numeric_from @from end |
#numeric_to ⇒ Float, Integer
Same as #to but will return ‘Float::Infinity` instead of `nil` if no lower bound is set.
929 930 931 |
# File 'lib/puppet/pops/types/types.rb', line 929 def numeric_to @to end |
#to ⇒ Float, Integer
Returns the upper bound of the numeric range or ‘nil` if no upper bound is set.
917 918 919 |
# File 'lib/puppet/pops/types/types.rb', line 917 def to @to == Float::INFINITY ? nil : @to end |
#unbounded? ⇒ Boolean
945 946 947 |
# File 'lib/puppet/pops/types/types.rb', line 945 def unbounded? @from == -Float::INFINITY && @to == Float::INFINITY end |