Class: Monies
Defined Under Namespace
Modules: Digits, Serialization Classes: Format, Parser, Symbols
Constant Summary collapse
- BASE =
10
- CurrencyError =
Class.new(ArgumentError)
Class Attribute Summary collapse
-
.currency ⇒ Object
Returns the value of attribute currency.
-
.formats ⇒ Object
Returns the value of attribute formats.
-
.symbols ⇒ Object
Returns the value of attribute symbols.
Class Method Summary collapse
- ._load(string) ⇒ Object
- .dump(value) ⇒ Object
- .format(value, name = :default, symbol: false, code: false) ⇒ Object
- .load(string) ⇒ Object
- .parse(string) ⇒ Object
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(other) ⇒ Object
- #<=>(other) ⇒ Object
- #_dump(_level) ⇒ Object
- #abs ⇒ Object
- #ceil(digits = 0) ⇒ Object
- #coerce(other) ⇒ Object
- #convert(other, currency = nil) ⇒ Object
- #currency ⇒ Object
- #div(other, digits = 16) ⇒ Object
- #floor(digits = 0) ⇒ Object
-
#initialize(value, scale, currency = self.class.currency) ⇒ Monies
constructor
A new instance of Monies.
- #inspect ⇒ Object (also: #to_s)
- #negative? ⇒ Boolean
- #nonzero? ⇒ Boolean
- #positive? ⇒ Boolean
- #precision ⇒ Object
- #round(digits = 0, mode = :default, half: nil) ⇒ Object
- #scale ⇒ Object
- #to_d ⇒ Object
- #to_i ⇒ Object
- #to_r ⇒ Object
- #truncate(digits = 0) ⇒ Object
- #value ⇒ Object
- #zero? ⇒ Boolean
Constructor Details
#initialize(value, scale, currency = self.class.currency) ⇒ Monies
Returns a new instance of Monies.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/monies.rb', line 78 def initialize(value, scale, currency = self.class.currency) unless value.is_a?(Integer) raise ArgumentError, "#{value.inspect} is not a valid value argument" end unless scale.is_a?(Integer) && scale >= 0 raise ArgumentError, "#{scale.inspect} is not a valid scale argument" end unless currency.is_a?(String) raise ArgumentError, "#{currency.inspect} is not a valid currency argument" end @value, @scale, @currency = value, scale, currency freeze end |
Class Attribute Details
.currency ⇒ Object
Returns the value of attribute currency.
15 16 17 |
# File 'lib/monies.rb', line 15 def currency @currency end |
.formats ⇒ Object
Returns the value of attribute formats.
16 17 18 |
# File 'lib/monies.rb', line 16 def formats @formats end |
.symbols ⇒ Object
Returns the value of attribute symbols.
17 18 19 |
# File 'lib/monies.rb', line 17 def symbols @symbols end |
Class Method Details
._load(string) ⇒ Object
72 73 74 75 76 |
# File 'lib/monies.rb', line 72 def self._load(string) value, scale, currency = string.split new(value.to_i, scale.to_i, currency) end |
.dump(value) ⇒ Object
44 45 46 47 48 |
# File 'lib/monies.rb', line 44 def self.dump(value) return value unless value.is_a?(self) "#{Monies::Digits.dump(value)} #{value.currency}" end |
.format(value, name = :default, symbol: false, code: false) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/monies.rb', line 50 def self.format(value, name = :default, symbol: false, code: false) unless formats.key?(name) raise ArgumentError, "#{name.inspect} is not a valid format" end return if value.nil? formats[name].call(value, symbol: symbol, code: code) end |
Instance Method Details
#*(other) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/monies.rb', line 96 def *(other) if other.is_a?(Integer) return reduce(@value * other, @scale) end if other.is_a?(Rational) return self * other.numerator / other.denominator end if other.respond_to?(:to_d) && !other.is_a?(self.class) other = other.to_d sign, significant_digits, base, exponent = other.split value = significant_digits.to_i * sign length = significant_digits.length if exponent.positive? && length < exponent value *= base ** (exponent - length) end scale = other.scale return reduce(@value * value, @scale + scale) end raise TypeError, "#{self.class} can't be multiplied by #{other.class}" end |
#+(other) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/monies.rb', line 126 def +(other) if other.respond_to?(:zero?) && other.zero? return self end unless other.is_a?(self.class) raise TypeError, "can't add #{other.class} to #{self.class}" end unless other.currency == @currency raise CurrencyError, "can't add #{other.currency} to #{@currency}" end add(other) end |
#-(other) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/monies.rb', line 142 def -(other) if other.respond_to?(:zero?) && other.zero? return self end unless other.is_a?(self.class) raise TypeError, "can't subtract #{other.class} from #{self.class}" end unless other.currency == @currency raise CurrencyError, "can't subtract #{other.currency} from #{@currency}" end add(-other) end |
#-@ ⇒ Object
158 159 160 |
# File 'lib/monies.rb', line 158 def -@ self.class.new(-@value, @scale, @currency) end |
#/(other) ⇒ Object
162 163 164 |
# File 'lib/monies.rb', line 162 def /(other) div(other) end |
#<=>(other) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/monies.rb', line 166 def <=>(other) if other.is_a?(self.class) unless other.currency == @currency raise CurrencyError, "can't compare #{other.currency} with #{@currency}" end value, other_value = @value, other.value if other.scale > @scale value *= BASE ** (other.scale - @scale) elsif other.scale < @scale other_value *= BASE ** (@scale - other.scale) end value <=> other_value elsif other.respond_to?(:zero?) && other.zero? @value <=> other end end |
#_dump(_level) ⇒ Object
188 189 190 |
# File 'lib/monies.rb', line 188 def _dump(_level) "#{@value} #{@scale} #{@currency}" end |
#abs ⇒ Object
192 193 194 195 196 |
# File 'lib/monies.rb', line 192 def abs return self unless negative? self.class.new(@value.abs, @scale, @currency) end |
#ceil(digits = 0) ⇒ Object
198 199 200 |
# File 'lib/monies.rb', line 198 def ceil(digits = 0) round(digits, :ceil) end |
#coerce(other) ⇒ Object
202 203 204 205 206 207 208 |
# File 'lib/monies.rb', line 202 def coerce(other) unless other.respond_to?(:zero?) && other.zero? raise TypeError, "#{self.class} can't be coerced into #{other.class}" end return self, other end |
#convert(other, currency = nil) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/monies.rb', line 210 def convert(other, currency = nil) if other.is_a?(self.class) unless currency.nil? raise ArgumentError, "#{self.class} can't be converted with #{other.class} and currency argument" end return self if @currency == other.currency return other * to_r end if other.is_a?(Integer) || other.is_a?(Rational) return Monies(to_r * other, currency || Monies.currency) end if defined?(BigDecimal) && other.is_a?(BigDecimal) return Monies(to_d * other, currency || Monies.currency) end raise TypeError, "#{self.class} can't be converted with #{other.class}" end |
#currency ⇒ Object
232 233 234 |
# File 'lib/monies.rb', line 232 def currency @currency end |
#div(other, digits = 16) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/monies.rb', line 236 def div(other, digits = 16) unless digits.is_a?(Integer) && digits >= 1 raise ArgumentError, 'digits must be greater than or equal to 1' end if other.respond_to?(:zero?) && other.zero? raise ZeroDivisionError, 'divided by 0' end if other.is_a?(self.class) unless other.currency == @currency raise CurrencyError, "can't divide #{@currency} by #{other.currency}" end scale = @scale - other.scale if scale.negative? return divide(@value * BASE ** scale.abs, 0, other.value, digits) else return divide(@value, scale, other.value, digits) end end if other.is_a?(Integer) return divide(@value, @scale, other, digits) end if other.is_a?(Rational) return self * other.denominator / other.numerator end if defined?(BigDecimal) && other.is_a?(BigDecimal) return self / Monies(other, @currency) end raise TypeError, "#{self.class} can't be divided by #{other.class}" end |
#floor(digits = 0) ⇒ Object
274 275 276 |
# File 'lib/monies.rb', line 274 def floor(digits = 0) round(digits, :floor) end |
#inspect ⇒ Object Also known as: to_s
278 279 280 |
# File 'lib/monies.rb', line 278 def inspect "#<#{self.class.name}: #{Monies::Digits.dump(self)} #{@currency}>" end |
#negative? ⇒ Boolean
282 283 284 |
# File 'lib/monies.rb', line 282 def negative? @value.negative? end |
#nonzero? ⇒ Boolean
286 287 288 |
# File 'lib/monies.rb', line 286 def nonzero? !@value.zero? end |
#positive? ⇒ Boolean
290 291 292 |
# File 'lib/monies.rb', line 290 def positive? @value.positive? end |
#precision ⇒ Object
294 295 296 297 298 |
# File 'lib/monies.rb', line 294 def precision return 0 if @value.zero? @value.to_s.length end |
#round(digits = 0, mode = :default, half: nil) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/monies.rb', line 300 def round(digits = 0, mode = :default, half: nil) if half == :up mode = :half_up elsif half == :down mode = :half_down elsif half == :even mode = :half_even elsif !half.nil? raise ArgumentError, "invalid rounding mode: #{half.inspect}" end case mode when :banker, :ceil, :ceiling, :default, :down, :floor, :half_down, :half_even, :half_up, :truncate, :up else raise ArgumentError, "invalid rounding mode: #{mode.inspect}" end if digits >= @scale return self end n = @scale - digits array = @value.abs.digits digit = array[n - 1] case mode when :ceiling, :ceil round_digits!(array, n) if @value.positive? when :floor round_digits!(array, n) if @value.negative? when :half_down round_digits!(array, n) if (digit > 5 || (digit == 5 && n > 1)) when :half_even, :banker round_digits!(array, n) if (digit > 5 || (digit == 5 && n > 1)) || digit == 5 && n == 1 && array[n].odd? when :half_up, :default round_digits!(array, n) if digit >= 5 when :up round_digits!(array, n) end n.times { |i| array[i] = nil } value = array.reverse.join.to_i value = -value if @value.negative? if digits.zero? self.class.new(value, 0, currency) else reduce(value, digits) end end |
#scale ⇒ Object
355 356 357 |
# File 'lib/monies.rb', line 355 def scale @scale end |
#to_d ⇒ Object
359 360 361 |
# File 'lib/monies.rb', line 359 def to_d BigDecimal(Monies::Digits.dump(self)) end |
#to_i ⇒ Object
363 364 365 |
# File 'lib/monies.rb', line 363 def to_i @value / BASE ** @scale end |
#to_r ⇒ Object
367 368 369 |
# File 'lib/monies.rb', line 367 def to_r Rational(@value, BASE ** @scale) end |
#truncate(digits = 0) ⇒ Object
373 374 375 376 377 |
# File 'lib/monies.rb', line 373 def truncate(digits = 0) return self if digits >= @scale reduce(@value / BASE ** (@scale - digits), digits) end |
#value ⇒ Object
379 380 381 |
# File 'lib/monies.rb', line 379 def value @value end |
#zero? ⇒ Boolean
383 384 385 |
# File 'lib/monies.rb', line 383 def zero? @value.zero? end |