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