class Puppet::Pops::Types::PNumericType
@api public
Constants
- DEFAULT
Public Class Methods
new(from, to = Float::INFINITY)
click to toggle source
# File lib/puppet/pops/types/types.rb 939 def initialize(from, to = Float::INFINITY) 940 from = -Float::INFINITY if from.nil? || from == :default 941 to = Float::INFINITY if to.nil? || to == :default 942 raise ArgumentError, "'from' must be less or equal to 'to'. Got (#{from}, #{to}" if from > to 943 @from = from 944 @to = to 945 end
new_function(type)
click to toggle source
# File lib/puppet/pops/types/types.rb 867 def self.new_function(type) 868 @new_function ||= Puppet::Functions.create_loaded_function(:new_numeric, type.loader) do 869 local_types do 870 type "Convertible = Variant[Integer, Float, Boolean, Pattern[/#{FLOAT_PATTERN}/], Timespan, Timestamp]" 871 type 'NamedArgs = Struct[{from => Convertible, Optional[abs] => Boolean}]' 872 end 873 874 dispatch :from_args do 875 param 'Convertible', :from 876 optional_param 'Boolean', :abs 877 end 878 879 dispatch :from_hash do 880 param 'NamedArgs', :hash_args 881 end 882 883 argument_mismatch :on_error do 884 param 'Any', :from 885 optional_param 'Boolean', :abs 886 end 887 888 def from_args(from, abs = false) 889 result = from_convertible(from) 890 abs ? result.abs : result 891 end 892 893 def from_hash(args_hash) 894 from_args(args_hash['from'], args_hash['abs'] || false) 895 end 896 897 def from_convertible(from) 898 case from 899 when Float 900 from 901 when Integer 902 from 903 when Time::TimeData 904 from.to_f 905 when TrueClass 906 1 907 when FalseClass 908 0 909 else 910 begin 911 if from[0] == '0' 912 second_char = (from[1] || '').downcase 913 if second_char == 'b' || second_char == 'x' 914 # use built in conversion 915 return Integer(from) 916 end 917 end 918 919 Puppet::Pops::Utils.to_n(from) 920 rescue TypeError => e 921 raise TypeConversionError.new(e.message) 922 rescue ArgumentError => e 923 raise TypeConversionError.new(e.message) 924 end 925 end 926 end 927 928 def on_error(from, abs = false) 929 if from.is_a?(String) 930 _("The string '%{str}' cannot be converted to Numeric") % { str: from } 931 else 932 t = TypeCalculator.singleton.infer(from).generalize 933 _("Value of type %{type} cannot be converted to Numeric") % { type: t } 934 end 935 end 936 end 937 end
register_ptype(loader, ir)
click to toggle source
# File lib/puppet/pops/types/types.rb 860 def self.register_ptype(loader, ir) 861 create_ptype(loader, ir, 'ScalarDataType', 862 'from' => { KEY_TYPE => POptionalType.new(PNumericType::DEFAULT), KEY_VALUE => nil }, 863 'to' => { KEY_TYPE => POptionalType.new(PNumericType::DEFAULT), KEY_VALUE => nil } 864 ) 865 end
Public Instance Methods
eql?(o)
click to toggle source
# File lib/puppet/pops/types/types.rb 984 def eql?(o) 985 self.class == o.class && @from == o.numeric_from && @to == o.numeric_to 986 end
from()
click to toggle source
Returns the lower bound of the numeric range or `nil` if no lower bound is set. @return [Float,Integer]
# File lib/puppet/pops/types/types.rb 958 def from 959 @from == -Float::INFINITY ? nil : @from 960 end
from_args(from, abs = false)
click to toggle source
# File lib/puppet/pops/types/types.rb 888 def from_args(from, abs = false) 889 result = from_convertible(from) 890 abs ? result.abs : result 891 end
from_convertible(from)
click to toggle source
# File lib/puppet/pops/types/types.rb 897 def from_convertible(from) 898 case from 899 when Float 900 from 901 when Integer 902 from 903 when Time::TimeData 904 from.to_f 905 when TrueClass 906 1 907 when FalseClass 908 0 909 else 910 begin 911 if from[0] == '0' 912 second_char = (from[1] || '').downcase 913 if second_char == 'b' || second_char == 'x' 914 # use built in conversion 915 return Integer(from) 916 end 917 end 918 919 Puppet::Pops::Utils.to_n(from) 920 rescue TypeError => e 921 raise TypeConversionError.new(e.message) 922 rescue ArgumentError => e 923 raise TypeConversionError.new(e.message) 924 end 925 end 926 end
from_hash(args_hash)
click to toggle source
# File lib/puppet/pops/types/types.rb 893 def from_hash(args_hash) 894 from_args(args_hash['from'], args_hash['abs'] || false) 895 end
hash()
click to toggle source
# File lib/puppet/pops/types/types.rb 980 def hash 981 @from.hash ^ @to.hash 982 end
instance?(o, guard = nil)
click to toggle source
# File lib/puppet/pops/types/types.rb 988 def instance?(o, guard = nil) 989 (o.is_a?(Float) || o.is_a?(Integer)) && o >= @from && o <= @to 990 end
intersect?(o)
click to toggle source
Checks if this numeric range intersects with another
@param o [PNumericType] the range to compare with @return [Boolean] `true` if this range intersects with the other range @api public
# File lib/puppet/pops/types/types.rb 952 def intersect?(o) 953 self.class == o.class && !(@to < o.numeric_from || o.numeric_to < @from) 954 end
numeric_from()
click to toggle source
Same as from but will return `-Float::Infinity` instead of `nil` if no lower bound is set. @return [Float,Integer]
# File lib/puppet/pops/types/types.rb 970 def numeric_from 971 @from 972 end
numeric_to()
click to toggle source
Same as to but will return `Float::Infinity` instead of `nil` if no lower bound is set. @return [Float,Integer]
# File lib/puppet/pops/types/types.rb 976 def numeric_to 977 @to 978 end
on_error(from, abs = false)
click to toggle source
# File lib/puppet/pops/types/types.rb 928 def on_error(from, abs = false) 929 if from.is_a?(String) 930 _("The string '%{str}' cannot be converted to Numeric") % { str: from } 931 else 932 t = TypeCalculator.singleton.infer(from).generalize 933 _("Value of type %{type} cannot be converted to Numeric") % { type: t } 934 end 935 end
to()
click to toggle source
Returns the upper bound of the numeric range or `nil` if no upper bound is set. @return [Float,Integer]
# File lib/puppet/pops/types/types.rb 964 def to 965 @to == Float::INFINITY ? nil : @to 966 end
unbounded?()
click to toggle source
# File lib/puppet/pops/types/types.rb 992 def unbounded? 993 @from == -Float::INFINITY && @to == Float::INFINITY 994 end
Protected Instance Methods
_assignable?(o, guard)
click to toggle source
@api_private
# File lib/puppet/pops/types/types.rb 999 def _assignable?(o, guard) 1000 return false unless o.is_a?(self.class) 1001 # If o min and max are within the range of t 1002 @from <= o.numeric_from && @to >= o.numeric_to 1003 end