cashify 0.9.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d31e15d5b41014afa8408f2eb88f2951ceb129c8b1f189c0fb4783d506a9e99
4
- data.tar.gz: 3d88a0eab4b01d2dbfb4ea67cc32c26e255d677f6d71ea34b056fcaca426a3ef
3
+ metadata.gz: bf06dc249f2461c5e6f7f59cdf2208eae3a423007570b12e5a52eaa8ed5ca44c
4
+ data.tar.gz: 625e22c82feb306c2564f5f7e79db25d6d658bfddaa347450b909bb7503b2fae
5
5
  SHA512:
6
- metadata.gz: 4fc2adb94912de958291b7a5aa878a89cde682aa1dad935136576e4df4a9705570b7bfc85fbeadb027e9d0478de0b269e4482fbefbbe664f0914c2d8b83247ac
7
- data.tar.gz: 8dac497d6a56923a1d7dadfe96ef64dba8e5fb7aba33b1278e392b8759b83c4ee0f6515a8c7e55c8a0ac7d948b4298cbc2e210449415bc93b19fecafc7377b14
6
+ metadata.gz: 8f65af840ef4a53e2bf82bff63197526bbbfa22d936321b57ddabce52762fb02f7f55bfd3436306fd6af99bdd0882dfead0b5a323b613ff4b0c6bbae4d5bc032
7
+ data.tar.gz: 84c82bca4217c8a116c24a62cde33d9d0f12c6d2139f357ebb64633f7472f2c56bb566b89ae0e0094c0029bfe233655f25261c22a70ea7ee74776ef406efa3ea
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## [1.0.0] - 2023-03-30
2
+
3
+ - Add coercion so things like `sum` work natively (Johan Halse)
4
+
5
+ ## [0.9.2] - 2022-11-18
6
+
7
+ - Add `to_delimited_s` method (Johan Halse)
8
+
1
9
  ## [0.9.1] - 2021-10-08
2
10
 
3
11
  - Add `cashify` method for Active Record users (Johan Halse)
data/Gemfile.lock CHANGED
@@ -1,42 +1,44 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cashify (0.9.1)
4
+ cashify (0.9.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.2)
10
10
  coderay (1.1.3)
11
+ json (2.6.2)
11
12
  method_source (1.0.0)
12
- minitest (5.14.4)
13
- parallel (1.21.0)
14
- parser (3.0.2.0)
13
+ minitest (5.16.3)
14
+ parallel (1.22.1)
15
+ parser (3.1.2.1)
15
16
  ast (~> 2.4.1)
16
17
  pry (0.14.1)
17
18
  coderay (~> 1.1)
18
19
  method_source (~> 1.0)
19
- rainbow (3.0.0)
20
+ rainbow (3.1.1)
20
21
  rake (13.0.6)
21
- regexp_parser (2.1.1)
22
+ regexp_parser (2.6.1)
22
23
  rexml (3.2.5)
23
- rubocop (1.22.1)
24
+ rubocop (1.39.0)
25
+ json (~> 2.3)
24
26
  parallel (~> 1.10)
25
- parser (>= 3.0.0.0)
27
+ parser (>= 3.1.2.1)
26
28
  rainbow (>= 2.2.2, < 4.0)
27
29
  regexp_parser (>= 1.8, < 3.0)
28
- rexml
29
- rubocop-ast (>= 1.12.0, < 2.0)
30
+ rexml (>= 3.2.5, < 4.0)
31
+ rubocop-ast (>= 1.23.0, < 2.0)
30
32
  ruby-progressbar (~> 1.7)
31
33
  unicode-display_width (>= 1.4.0, < 3.0)
32
- rubocop-ast (1.12.0)
33
- parser (>= 3.0.1.1)
34
- rubocop-minitest (0.15.1)
34
+ rubocop-ast (1.23.0)
35
+ parser (>= 3.1.1.0)
36
+ rubocop-minitest (0.23.2)
35
37
  rubocop (>= 0.90, < 2.0)
36
38
  rubocop-rake (0.6.0)
37
39
  rubocop (~> 1.0)
38
40
  ruby-progressbar (1.11.0)
39
- unicode-display_width (2.1.0)
41
+ unicode-display_width (2.3.0)
40
42
 
41
43
  PLATFORMS
42
44
  x86_64-darwin-19
data/README.md CHANGED
@@ -161,15 +161,6 @@ Cash.new(EUR: 100_00, SEK: 100_00, USD: 100_00).to_s
161
161
  # "100 EUR, 100 SEK, 100 USD"
162
162
  ```
163
163
 
164
- #### `.sum`
165
-
166
- You'll be working with arrays of Cash objects a lot. You should absolutely be using Ruby's excellent array methods on them, but unfortunately `cash_array.sum` won't work. Ruby's `sum` method injects a `0` as the first value, and even though `Cash.new(USD: 100) + 0` is super valid and nice, `0 + Cash.new(USD: 100)` will try to coerce your Cash object into an integer which just won't work. So if you want to sum an array of Cash, you can instead use:
167
-
168
- ```ruby
169
- Cash.sum [Cash.new(EUR: 100_00), Cash.new(SEK: 100_00), Cash.new(EUR: 100_00)]
170
- # 200 EUR, 100 SEK
171
- ```
172
-
173
164
  #### `.empty?`
174
165
 
175
166
  Returns true if there are no currencies in the object.
@@ -1,5 +1,9 @@
1
1
  class Cash
2
2
  module Arithmetic
3
+ def coerce(other)
4
+ [self, other]
5
+ end
6
+
3
7
  def +(other)
4
8
  raise Cash::Errors::AdditionError unless other.respond_to?(:zero?)
5
9
  return self unless present?(other)
@@ -72,12 +76,12 @@ class Cash
72
76
  return currencies if other.zero?
73
77
  return other.currencies.transform_values { |v| 0 - v } if zero?
74
78
 
75
- (@currencies.keys + other.currencies.keys).uniq.map do |k|
79
+ (@currencies.keys + other.currencies.keys).uniq.to_h do |k|
76
80
  minuend = @currencies[k] || 0
77
81
  subtrahend = other.currencies[k] || 0
78
82
 
79
83
  [k, minuend - subtrahend]
80
- end.to_h
84
+ end
81
85
  end
82
86
  end
83
87
  end
@@ -1,3 +1,3 @@
1
1
  module Cashify
2
- VERSION = "0.9.1".freeze
2
+ VERSION = "1.0.0".freeze
3
3
  end
data/lib/cashify.rb CHANGED
@@ -25,10 +25,6 @@ class Cash
25
25
  new(USD: 0)
26
26
  end
27
27
 
28
- def self.sum(cashes)
29
- cashes.inject(Cash.zero, :+)
30
- end
31
-
32
28
  def initialize(**currencies)
33
29
  @currencies = currencies
34
30
  .transform_keys(&:to_sym)
@@ -69,6 +65,10 @@ class Cash
69
65
  currencies.map { |k, v| "#{(v * 0.01).round} #{k}" }.join(", ")
70
66
  end
71
67
 
68
+ def to_delimited_s
69
+ currencies.map { |k, v| "#{(v * 0.01).round.to_formatted_s(:delimited, delimiter: " ")} #{k}" }.join(", ")
70
+ end
71
+
72
72
  def currency
73
73
  currencies.keys.first
74
74
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cashify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johan Halse
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-08 00:00:00.000000000 Z
11
+ date: 2023-03-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Money trouble no more! Add, subtract, and have fun with money in different
14
14
  currencies.
@@ -41,7 +41,8 @@ metadata:
41
41
  homepage_uri: https://github.com/johanhalse/cashify
42
42
  source_code_uri: https://github.com/johanhalse/cashify
43
43
  changelog_uri: https://github.com/johanhalse/cashify/CHANGELOG.md
44
- post_install_message:
44
+ rubygems_mfa_required: 'true'
45
+ post_install_message:
45
46
  rdoc_options: []
46
47
  require_paths:
47
48
  - lib
@@ -56,8 +57,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
57
  - !ruby/object:Gem::Version
57
58
  version: '0'
58
59
  requirements: []
59
- rubygems_version: 3.2.22
60
- signing_key:
60
+ rubygems_version: 3.4.3
61
+ signing_key:
61
62
  specification_version: 4
62
63
  summary: A sensible way to handle money
63
64
  test_files: []