Class: Monies

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/monies.rb

Defined Under Namespace

Modules: Digits, Serialization Classes: Format, Parser, Symbols

Constant Summary collapse

BASE =
10
CurrencyError =
Class.new(ArgumentError)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

.currencyObject

Returns the value of attribute currency.



15
16
17
# File 'lib/monies.rb', line 15

def currency
  @currency
end

.formatsObject

Returns the value of attribute formats.



16
17
18
# File 'lib/monies.rb', line 16

def formats
  @formats
end

.symbolsObject

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

.load(string) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/monies.rb', line 60

def self.load(string)
  return if string.nil?

  digits, currency = string.split

  Monies::Digits.load(digits, currency)
end

.parse(string) ⇒ Object



68
69
70
# File 'lib/monies.rb', line 68

def self.parse(string)
  Parser.new(string).parse
end

Instance Method Details

#*(other) ⇒ Object

Raises:

  • (TypeError)


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
125
# 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
    scale = length - exponent if length < scale

    return reduce(@value * value, @scale + scale)
  end

  raise TypeError, "#{self.class} can't be multiplied by #{other.class}"
end

#+(other) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/monies.rb', line 127

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/monies.rb', line 143

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



159
160
161
# File 'lib/monies.rb', line 159

def -@
  self.class.new(-@value, @scale, @currency)
end

#/(other) ⇒ Object



163
164
165
# File 'lib/monies.rb', line 163

def /(other)
  div(other)
end

#<=>(other) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/monies.rb', line 167

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



189
190
191
# File 'lib/monies.rb', line 189

def _dump(_level)
  "#{@value} #{@scale} #{@currency}"
end

#absObject



193
194
195
196
197
# File 'lib/monies.rb', line 193

def abs
  return self unless negative?

  self.class.new(@value.abs, @scale, @currency)
end

#allocate(n, digits) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/monies.rb', line 199

def allocate(n, digits)
  unless n.is_a?(Integer) && n >= 1
    raise ArgumentError, 'n must be greater than or equal to 1'
  end

  quotient = (self / n).truncate(digits)

  remainder = self - quotient * n

  array = Array.new(n) { quotient }

  array[-1] += remainder unless remainder.zero?

  array
end

#ceil(digits = 0) ⇒ Object



215
216
217
# File 'lib/monies.rb', line 215

def ceil(digits = 0)
  round(digits, :ceil)
end

#coerce(other) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/monies.rb', line 219

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

Raises:

  • (TypeError)


227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/monies.rb', line 227

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

#currencyObject



249
250
251
# File 'lib/monies.rb', line 249

def currency
  @currency
end

#div(other, digits = 16) ⇒ Object

Raises:

  • (TypeError)


253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/monies.rb', line 253

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

#fixObject



291
292
293
# File 'lib/monies.rb', line 291

def fix
  self.class.new(@value / (BASE ** @scale), 0, @currency)
end

#floor(digits = 0) ⇒ Object



295
296
297
# File 'lib/monies.rb', line 295

def floor(digits = 0)
  round(digits, :floor)
end

#fracObject



299
300
301
# File 'lib/monies.rb', line 299

def frac
  self - fix
end

#inspectObject Also known as: to_s



303
304
305
# File 'lib/monies.rb', line 303

def inspect
  "#<#{self.class.name}: #{Monies::Digits.dump(self)} #{@currency}>"
end

#negative?Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/monies.rb', line 307

def negative?
  @value.negative?
end

#nonzero?Boolean

Returns:

  • (Boolean)


311
312
313
# File 'lib/monies.rb', line 311

def nonzero?
  !@value.zero?
end

#positive?Boolean

Returns:

  • (Boolean)


315
316
317
# File 'lib/monies.rb', line 315

def positive?
  @value.positive?
end

#precisionObject



319
320
321
322
323
# File 'lib/monies.rb', line 319

def precision
  return 0 if @value.zero?

  @value.to_s.length
end

#round(digits = 0, mode = :default, half: nil) ⇒ Object



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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/monies.rb', line 325

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

#scaleObject



380
381
382
# File 'lib/monies.rb', line 380

def scale
  @scale
end

#to_dObject



384
385
386
# File 'lib/monies.rb', line 384

def to_d
  BigDecimal(Monies::Digits.dump(self))
end

#to_iObject



388
389
390
# File 'lib/monies.rb', line 388

def to_i
  @value / BASE ** @scale
end

#to_rObject



392
393
394
# File 'lib/monies.rb', line 392

def to_r
  Rational(@value, BASE ** @scale)
end

#truncate(digits = 0) ⇒ Object



398
399
400
401
402
# File 'lib/monies.rb', line 398

def truncate(digits = 0)
  return self if digits >= @scale

  reduce(@value / BASE ** (@scale - digits), digits)
end

#valueObject



404
405
406
# File 'lib/monies.rb', line 404

def value
  @value
end

#zero?Boolean

Returns:

  • (Boolean)


408
409
410
# File 'lib/monies.rb', line 408

def zero?
  @value.zero?
end