Class: Puppet::Pops::Types::PIntegerType
- Inherits:
-
PNumericType
- Object
- TypedModelObject
- PAnyType
- PScalarType
- PScalarDataType
- PNumericType
- Puppet::Pops::Types::PIntegerType
- Defined in:
- lib/puppet/pops/types/types.rb
Constant Summary collapse
- DEFAULT =
PIntegerType.new(-Float::INFINITY)
Class Method Summary collapse
Instance Method Summary collapse
-
#adjacent?(o) ⇒ Boolean
Checks if this range is adjacent to the given range.
-
#each(&block) ⇒ Object
Returns Enumerator if no block is given Returns nil if size is infinity (does not yield).
-
#finite_range? ⇒ Boolean
Will respond ‘true` for any range that is bounded at both ends.
- #generalize ⇒ Object
- #instance?(o, guard = nil) ⇒ Boolean
- #iterable?(guard = nil) ⇒ Boolean
- #iterable_type(guard = nil) ⇒ Object
-
#merge(o) ⇒ PIntegerType?
Concatenates this range with another range provided that the ranges intersect or are adjacent.
- #new_function ⇒ Object
-
#range ⇒ Object
Returns the range as an array ordered so the smaller number is always first.
-
#size ⇒ Object
Returns Float.Infinity if one end of the range is unbound.
-
#to_size ⇒ PIntegerType
Returns a range where both to and from are positive numbers.
Methods inherited from PNumericType
#eql?, #from, #hash, #initialize, #intersect?, new_function, #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, create, #eql?, #hash, #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, #to_s
Constructor Details
This class inherits a constructor from Puppet::Pops::Types::PNumericType
Class Method Details
.register_ptype(loader, ir) ⇒ Object
1009 1010 1011 |
# File 'lib/puppet/pops/types/types.rb', line 1009 def self.register_ptype(loader, ir) create_ptype(loader, ir, 'NumericType') end |
Instance Method Details
#adjacent?(o) ⇒ Boolean
Checks if this range is adjacent to the given range
1033 1034 1035 |
# File 'lib/puppet/pops/types/types.rb', line 1033 def adjacent?(o) o.is_a?(PIntegerType) && (@to + 1 == o.from || o.to + 1 == @from) end |
#each(&block) ⇒ Object
Returns Enumerator if no block is given Returns nil if size is infinity (does not yield)
1076 1077 1078 1079 |
# File 'lib/puppet/pops/types/types.rb', line 1076 def each(&block) r = Iterable.on(self) block_given? ? r.each(&block) : r end |
#finite_range? ⇒ Boolean
Will respond ‘true` for any range that is bounded at both ends.
1016 1017 1018 |
# File 'lib/puppet/pops/types/types.rb', line 1016 def finite_range? @from != -Float::INFINITY && @to != Float::INFINITY end |
#generalize ⇒ Object
1020 1021 1022 |
# File 'lib/puppet/pops/types/types.rb', line 1020 def generalize DEFAULT end |
#instance?(o, guard = nil) ⇒ Boolean
1024 1025 1026 |
# File 'lib/puppet/pops/types/types.rb', line 1024 def instance?(o, guard = nil) o.is_a?(Integer) && o >= numeric_from && o <= numeric_to end |
#iterable?(guard = nil) ⇒ Boolean
1053 1054 1055 |
# File 'lib/puppet/pops/types/types.rb', line 1053 def iterable?(guard = nil) true end |
#iterable_type(guard = nil) ⇒ Object
1057 1058 1059 1060 |
# File 'lib/puppet/pops/types/types.rb', line 1057 def iterable_type(guard = nil) # It's unknown if the iterable will be a range (min, max) or a "times" (0, max) PIterableType.new(PIntegerType::DEFAULT) end |
#merge(o) ⇒ PIntegerType?
Concatenates this range with another range provided that the ranges intersect or are adjacent. When that’s not the case, this method will return ‘nil`
1043 1044 1045 1046 1047 1048 1049 1050 1051 |
# File 'lib/puppet/pops/types/types.rb', line 1043 def merge(o) if intersect?(o) || adjacent?(o) min = @from <= o.numeric_from ? @from : o.numeric_from max = @to >= o.numeric_to ? @to : o.numeric_to PIntegerType.new(min, max) else nil end end |
#new_function ⇒ Object
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 |
# File 'lib/puppet/pops/types/types.rb', line 1088 def new_function @@new_function ||= Puppet::Functions.create_loaded_function(:new, loader) do local_types do type 'Radix = Variant[Default, Integer[2,2], Integer[8,8], Integer[10,10], Integer[16,16]]' type "Convertible = Variant[Numeric, Boolean, Pattern[/#{INTEGER_PATTERN}/], Timespan, Timestamp]" type 'NamedArgs = Struct[{from => Convertible, Optional[radix] => Radix, Optional[abs] => Boolean}]' end dispatch :from_args do param 'Convertible', :from optional_param 'Radix', :radix optional_param 'Boolean', :abs end dispatch :from_hash do param 'NamedArgs', :hash_args end argument_mismatch :on_error_hash do param 'Hash', :hash_args end argument_mismatch :on_error do param 'Any', :from optional_param 'Integer', :radix optional_param 'Boolean', :abs end def from_args(from, radix = :default, abs = false) result = from_convertible(from, radix) abs ? result.abs : result end def from_hash(args_hash) from_args(args_hash['from'], args_hash['radix'] || :default, args_hash['abs'] || false) end def from_convertible(from, radix) case from when Float, Time::TimeData from.to_i when Integer from when TrueClass 1 when FalseClass 0 else begin radix == :default ? Integer(from) : Integer(from, radix) rescue TypeError => e raise TypeConversionError.new(e.) rescue ArgumentError => e # Test for special case where there is whitespace between sign and number match = Patterns::WS_BETWEEN_SIGN_AND_NUMBER.match(from) if match begin # Try again, this time with whitespace removed return from_args(match[1] + match[2], radix) rescue TypeConversionError # Ignored to retain original error end end raise TypeConversionError.new(e.) end end end def on_error_hash(args_hash) if args_hash.include?('from') from = args_hash['from'] return on_error(from) unless loader.load(:type, 'convertible').instance?(from) end radix = args_hash['radix'] assert_radix(radix) unless radix.nil? || radix == :default TypeAsserter.assert_instance_of('Integer.new', loader.load(:type, 'namedargs'), args_hash) end def on_error(from, radix = :default, abs = nil) assert_radix(radix) unless radix == :default if from.is_a?(String) _("The string '%{str}' cannot be converted to Integer") % { str: from } else t = TypeCalculator.singleton.infer(from).generalize _("Value of type %{type} cannot be converted to Integer") % { type: t } end end def assert_radix(radix) case radix when 2, 8, 10, 16 else raise ArgumentError.new(_("Illegal radix: %{radix}, expected 2, 8, 10, 16, or default") % { radix: radix }) end radix end end end |
#range ⇒ Object
Returns the range as an array ordered so the smaller number is always first. The number may be Infinity or -Infinity.
1070 1071 1072 |
# File 'lib/puppet/pops/types/types.rb', line 1070 def range [@from, @to] end |
#size ⇒ Object
Returns Float.Infinity if one end of the range is unbound
1063 1064 1065 1066 |
# File 'lib/puppet/pops/types/types.rb', line 1063 def size return Float::INFINITY if @from == -Float::INFINITY || @to == Float::INFINITY 1+(to-from).abs end |
#to_size ⇒ PIntegerType
Returns a range where both to and from are positive numbers. Negative numbers are converted to zero
1084 1085 1086 |
# File 'lib/puppet/pops/types/types.rb', line 1084 def to_size @from >= 0 ? self : PIntegerType.new(0, @to < 0 ? 0 : @to) end |