monies 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ffee4b91733b331056188aef0f3576b9e90fc16847f91174749f7ffc154e7ff
4
- data.tar.gz: bac13063005ebde101cf5faa4405fcfe48c1107bb10e6568ae35fe9c51191ce7
3
+ metadata.gz: 411e7bb7e471b8d8df69c510b07c810984dce43c7a753a1431c0f40c849cb1c8
4
+ data.tar.gz: 8b84016a100c83249bf9845c1592b1a20b405c1bdeb297b887abe8163b98d411
5
5
  SHA512:
6
- metadata.gz: e7a50b0b8c9584a16dc75be18bac0e54f706028cda755d501e8e91f3a2c84487b4b4763ac6ba651c36ccb70b2b275baa445983133982d926efb299653989611b
7
- data.tar.gz: 9a1f89469a73048e45e55cd6db7e3a93b4e2cfc99a9a5dbf6d247e56ee89f68ddea4bd76b66d819e8e844f5967911878a88c7a12996743923416e475d56db57d
6
+ metadata.gz: 7d6080e16f4d8c3fb149149635573d0e2615f4c104aa742623ba1d36e127b19572a59197d2cfc45f06de56f9f85b72db220e737b1448ea1da33a48f4109349fd
7
+ data.tar.gz: a251134b700e1659e6d0ff07162288ef44e3f69bb9fd8605bc407bf5e319d438b1cab818a0cd5144d3759f136cf856f1b73dfef4cf30474af0ef13e44ba0bed4
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2024 TIMCRAFT
1
+ Copyright (c) 2024-2025 TIMCRAFT
2
2
 
3
3
  This is an Open Source project licensed under the terms of the LGPLv3 license.
4
4
  Please see <http://www.gnu.org/licenses/lgpl-3.0.html> for license text.
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # monies
2
2
 
3
+ ![Gem Version](https://badge.fury.io/rb/monies.svg)
4
+ ![Test Status](https://github.com/readysteady/monies/actions/workflows/test.yml/badge.svg)
5
+
6
+
3
7
  Ruby gem for representing monetary values.
4
8
 
5
9
  Pure Ruby—compatible with MRI/CRuby, JRuby, TruffleRuby, and Natalie.
@@ -76,6 +80,12 @@ the maximum number of decimal places you want:
76
80
  Monies(1, 'USD').div(9, 100)
77
81
  ```
78
82
 
83
+ Use the #allocate method to split an amount into a number of smaller amounts:
84
+
85
+ ```ruby
86
+ installments = Monies(1, 'USD').allocate(3, 2)
87
+ ```
88
+
79
89
 
80
90
  ## Currency conversion
81
91
 
data/lib/monies/digits.rb CHANGED
@@ -60,4 +60,10 @@ module Monies::Digits
60
60
 
61
61
  Monies.new(value, scale, currency)
62
62
  end
63
+
64
+ REGEXP = /\A\-?\d+(\.\d+)?\z/
65
+
66
+ def self.match?(string)
67
+ REGEXP.match?(string)
68
+ end
63
69
  end
data/lib/monies.rb CHANGED
@@ -116,6 +116,7 @@ class Monies
116
116
  end
117
117
 
118
118
  scale = other.scale
119
+ scale = length - exponent if length < scale
119
120
 
120
121
  return reduce(@value * value, @scale + scale)
121
122
  end
@@ -195,6 +196,22 @@ class Monies
195
196
  self.class.new(@value.abs, @scale, @currency)
196
197
  end
197
198
 
199
+ def allocate(n, digits)
200
+ unless n.is_a?(Integer) && n >= 1
201
+ raise ArgumentError, 'n must be greater than or equal to 1'
202
+ end
203
+
204
+ quotient = (self / n).truncate(digits)
205
+
206
+ remainder = self - quotient * n
207
+
208
+ array = Array.new(n) { quotient }
209
+
210
+ array[-1] += remainder unless remainder.zero?
211
+
212
+ array
213
+ end
214
+
198
215
  def ceil(digits = 0)
199
216
  round(digits, :ceil)
200
217
  end
@@ -271,10 +288,18 @@ class Monies
271
288
  raise TypeError, "#{self.class} can't be divided by #{other.class}"
272
289
  end
273
290
 
291
+ def fix
292
+ self.class.new(@value / (BASE ** @scale), 0, @currency)
293
+ end
294
+
274
295
  def floor(digits = 0)
275
296
  round(digits, :floor)
276
297
  end
277
298
 
299
+ def frac
300
+ self - fix
301
+ end
302
+
278
303
  def inspect
279
304
  "#<#{self.class.name}: #{Monies::Digits.dump(self)} #{@currency}>"
280
305
  end
@@ -463,7 +488,7 @@ class Monies
463
488
  end
464
489
  end
465
490
 
466
- def Monies(object, currency = Monies.currency)
491
+ def Monies(object, currency = Monies.currency, exception: true)
467
492
  case object
468
493
  when Monies
469
494
  object
@@ -472,7 +497,11 @@ def Monies(object, currency = Monies.currency)
472
497
  when Rational
473
498
  Monies.new(object.numerator, 0, currency) / object.denominator
474
499
  when String
475
- Monies::Digits.load(object, currency)
500
+ if Monies::Digits.match?(object)
501
+ Monies::Digits.load(object, currency)
502
+ elsif exception
503
+ raise ArgumentError, "invalid value for Monies(): #{object.inspect}"
504
+ end
476
505
  else
477
506
  if defined?(BigDecimal) && object.is_a?(BigDecimal)
478
507
  sign, significant_digits, base, exponent = object.split
@@ -486,10 +515,11 @@ def Monies(object, currency = Monies.currency)
486
515
  end
487
516
 
488
517
  scale = object.scale
518
+ scale = length - exponent if length < scale
489
519
 
490
520
  return Monies.new(value, scale, currency)
491
521
  end
492
522
 
493
- raise TypeError, "can't convert #{object.inspect} into #{Monies}"
523
+ raise TypeError, "can't convert #{object.inspect} into #{Monies}" if exception
494
524
  end
495
525
  end
data/monies.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'monies'
3
- s.version = '1.0.1'
3
+ s.version = '1.2.0'
4
4
  s.license = 'LGPL-3.0'
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.authors = ['Tim Craft']
@@ -9,11 +9,12 @@ Gem::Specification.new do |s|
9
9
  s.description = 'Ruby gem for representing monetary values'
10
10
  s.summary = 'See description'
11
11
  s.files = Dir.glob('lib/**/*.rb') + %w[LICENSE.txt README.md monies.gemspec]
12
- s.required_ruby_version = '>= 3.1.0'
12
+ s.required_ruby_version = '>= 3.2.0'
13
13
  s.require_path = 'lib'
14
14
  s.metadata = {
15
15
  'homepage' => 'https://github.com/readysteady/monies',
16
16
  'source_code_uri' => 'https://github.com/readysteady/monies',
17
17
  'bug_tracker_uri' => 'https://github.com/readysteady/monies/issues',
18
+ 'changelog_uri' => 'https://github.com/readysteady/monies/blob/main/CHANGES.md'
18
19
  }
19
20
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monies
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Craft
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-09-05 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Ruby gem for representing monetary values
14
13
  email:
@@ -35,7 +34,7 @@ metadata:
35
34
  homepage: https://github.com/readysteady/monies
36
35
  source_code_uri: https://github.com/readysteady/monies
37
36
  bug_tracker_uri: https://github.com/readysteady/monies/issues
38
- post_install_message:
37
+ changelog_uri: https://github.com/readysteady/monies/blob/main/CHANGES.md
39
38
  rdoc_options: []
40
39
  require_paths:
41
40
  - lib
@@ -43,15 +42,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
42
  requirements:
44
43
  - - ">="
45
44
  - !ruby/object:Gem::Version
46
- version: 3.1.0
45
+ version: 3.2.0
47
46
  required_rubygems_version: !ruby/object:Gem::Requirement
48
47
  requirements:
49
48
  - - ">="
50
49
  - !ruby/object:Gem::Version
51
50
  version: '0'
52
51
  requirements: []
53
- rubygems_version: 3.5.11
54
- signing_key:
52
+ rubygems_version: 3.6.7
55
53
  specification_version: 4
56
54
  summary: See description
57
55
  test_files: []