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?, #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.
849 850 851 852 853 854 855 |
# File 'lib/puppet/pops/types/types.rb', line 849 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(_, loader) ⇒ Object
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 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 |
# File 'lib/puppet/pops/types/types.rb', line 790 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
783 784 785 786 787 788 |
# File 'lib/puppet/pops/types/types.rb', line 783 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
894 895 896 |
# File 'lib/puppet/pops/types/types.rb', line 894 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.
868 869 870 |
# File 'lib/puppet/pops/types/types.rb', line 868 def from @from == -Float::INFINITY ? nil : @from end |
#hash ⇒ Object
890 891 892 |
# File 'lib/puppet/pops/types/types.rb', line 890 def hash @from.hash ^ @to.hash end |
#instance?(o, guard = nil) ⇒ Boolean
898 899 900 |
# File 'lib/puppet/pops/types/types.rb', line 898 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
862 863 864 |
# File 'lib/puppet/pops/types/types.rb', line 862 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.
880 881 882 |
# File 'lib/puppet/pops/types/types.rb', line 880 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.
886 887 888 |
# File 'lib/puppet/pops/types/types.rb', line 886 def numeric_to @to end |
#to ⇒ Float, Integer
Returns the upper bound of the numeric range or ‘nil` if no upper bound is set.
874 875 876 |
# File 'lib/puppet/pops/types/types.rb', line 874 def to @to == Float::INFINITY ? nil : @to end |
#unbounded? ⇒ Boolean
902 903 904 |
# File 'lib/puppet/pops/types/types.rb', line 902 def unbounded? @from == -Float::INFINITY && @to == Float::INFINITY end |