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
# 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

#absObject



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

#allocate(n, digits) ⇒ Object



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

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



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

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

#coerce(other) ⇒ Object



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

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)


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

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



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

def currency
  @currency
end

#div(other, digits = 16) ⇒ Object

Raises:

  • (TypeError)


252
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
# File 'lib/monies.rb', line 252

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



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

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

#floor(digits = 0) ⇒ Object



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

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

#fracObject



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

def frac
  self - fix
end

#inspectObject Also known as: to_s



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

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

#negative?Boolean

Returns:

  • (Boolean)


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

def negative?
  @value.negative?
end

#nonzero?Boolean

Returns:

  • (Boolean)


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

def nonzero?
  !@value.zero?
end

#positive?Boolean

Returns:

  • (Boolean)


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

def positive?
  @value.positive?
end

#precisionObject



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

def precision
  return 0 if @value.zero?

  @value.to_s.length
end

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



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

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



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

def scale
  @scale
end

#to_dObject



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

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

#to_iObject



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

def to_i
  @value / BASE ** @scale
end

#to_rObject



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

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

#truncate(digits = 0) ⇒ Object



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

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

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

#valueObject



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

def value
  @value
end

#zero?Boolean

Returns:

  • (Boolean)


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

def zero?
  @value.zero?
end