minting-rails 0.5.0 → 0.7.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: bebeb146f003c49cd0f1c667648090a35f96d5d22edfec3c6616f5f45d812988
4
- data.tar.gz: f6084d3f41dce76e7b437505566a1a606f54a92419cac641e017d33a9a2d0400
3
+ metadata.gz: 205d9eb9b1d27048756b4f65f2e65781ae71f131d1d838a1834ea733349d3bf4
4
+ data.tar.gz: ea2dfd05351ae59717737a0770480c1eeb6cf484264c9daea064c87ac80dd4a0
5
5
  SHA512:
6
- metadata.gz: 46539fbf075c057d2fb2727320975b13dd20f5efbe5a0ad2955d51f78f1720ecb4e9a3e7f52c9428f798dabf13a04a258403df6fe0735fff901a0c8e6e2ccf08
7
- data.tar.gz: 66528813c392c867719ea9dcecf24fbc78a8bdd9fe9b234d6ca82c201b6d6da533ebd3058b0610223def05344e10843505a3c30fe13d65acd5593717b2d8fce2
6
+ metadata.gz: a3e434d199296b8d9a22db937b2b40af35eec147a7fdc1d4a1c1a387a3a0300b806381f2e98cb3a3dfc40371b1087f1529a3a81b399b91dff4d4f454034f39c4
7
+ data.tar.gz: 8552a6d4d6c8f6567a4300c6540b04c00ca220b52b3deb0f0ba00af1fb40d0c974110eb97f04d3d0acb8d037f32852d61cd4a55a6b8d2dc17cbc398c9e58c6d4
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Minting::Rails brings [Minting](https://github.com/gferraz/minting) money objects to Active Record models.
4
4
 
5
- It adds a `money_attribute` model helper, registers a `:mint_money` Active Record type, and includes small convenience methods such as `12.to_money(:USD)`, `12.dollars`, and `'12.00'.mint(:BRL)`.
5
+ It adds a `money_attribute` model helper, registers a `:mint_money` Active Record type, and includes small convenience methods such as `12.to_money('USD')`, `12.dollars`, and `'12.00'.mint('BRL')`.
6
6
 
7
7
  ## What it does
8
8
 
@@ -15,7 +15,7 @@ It adds a `money_attribute` model helper, registers a `:mint_money` Active Recor
15
15
 
16
16
  - Ruby 3.3 or newer.
17
17
  - Rails 7.1.3.2 or newer.
18
- - Minting 1.0.0 or newer.
18
+ - Minting 1.6.0 or newer.
19
19
 
20
20
  ## Installation
21
21
 
@@ -45,6 +45,7 @@ Configure Minting in `config/initializers/minting.rb`:
45
45
  Mint.configure do |config|
46
46
  config.enabled_currencies = :all
47
47
  config.default_currency = 'USD'
48
+ config.default_format = '%<symbol>s%<amount>f'
48
49
  end
49
50
  ```
50
51
 
@@ -63,7 +64,7 @@ You can also register custom currencies before enabling or using them:
63
64
  Mint.configure do |config|
64
65
  config.added_currencies = [
65
66
  { currency: 'CRC', subunit: 2, symbol: 'CRC' },
66
- { currency: 'NGN', subunit: 2, symbol: 'NGN' }
67
+ { currency: 'NGN', subunit: 2, symbol: 'NGN' } # subunit is the number of digits after the decimal; USD has 2, JPY has 0, BHD has 3
67
68
  ]
68
69
 
69
70
  config.enabled_currencies = :all
@@ -120,7 +121,7 @@ product.discount
120
121
  Assigning a `Mint::Money` with a different currency raises `ArgumentError`:
121
122
 
122
123
  ```ruby
123
- Product.new(price: 12.to_money(:EUR))
124
+ Product.new(price: 12.to_money('EUR'))
124
125
  # raises ArgumentError because the attribute only accepts USD
125
126
  ```
126
127
 
@@ -154,7 +155,7 @@ end
154
155
  The attribute is composed from `price_amount` and `price_currency`:
155
156
 
156
157
  ```ruby
157
- offer = Offer.new(price: 15.to_money(:EUR))
158
+ offer = Offer.new(price: 15.to_money('EUR'))
158
159
 
159
160
  offer.price
160
161
  # => #<Mint::Money ... EUR 15.00>
@@ -224,7 +225,7 @@ t.bigint :price
224
225
 
225
226
  No model change is needed. The column type drives the behavior.
226
227
 
227
- > **Note:** Integer storage avoids decimal rounding issues and is more efficient for large tables.
228
+ > **Note:** Integer storage is more efficient for large tables. Use Decimal when you need to stay close to SQL standards for interoperability with other systems
228
229
 
229
230
  ### Default Currency
230
231
 
@@ -244,26 +245,26 @@ If your amount and currency columns do not follow the `<name>_amount` and `<name
244
245
  ```ruby
245
246
  class Invoice < ApplicationRecord
246
247
  money_attribute :total, mapping: {
247
- total_amount: :amount,
248
- currency_code: :currency
248
+ amount: :total_amount,
249
+ currency: :currency_code
249
250
  }
250
251
  end
251
252
  ```
252
253
 
253
- The mapping keys are your database columns. The values must identify which column stores the `:amount` and which stores the `:currency`.
254
+ The mapping keys are `:amount` and `:currency`. The values are your database columns.
254
255
 
255
256
  ## Querying
256
257
 
257
258
  Fixed-currency attributes can be queried with `Mint::Money` values:
258
259
 
259
260
  ```ruby
260
- Product.where(price: 15.to_money(:USD))
261
+ Product.where(price: 15.to_money('USD'))
261
262
  ```
262
263
 
263
264
  Composed attributes can also be queried with a money object:
264
265
 
265
266
  ```ruby
266
- Offer.where(price: 15.to_money(:EUR))
267
+ Offer.where(price: 15.to_money('EUR'))
267
268
  ```
268
269
 
269
270
  You can still query the backing columns directly when that is clearer:
@@ -277,12 +278,14 @@ Offer.where(price_amount: 15, price_currency: 'EUR')
277
278
  Minting::Rails adds a few small helpers:
278
279
 
279
280
  ```ruby
280
- 12.to_money(:USD)
281
- 12.mint(:BRL)
281
+ 12.to_money('USD')
282
+ 12.mint('BRL')
282
283
  12.dollars
284
+ 1.dollar
285
+ 1.euro
283
286
  12.euros
284
- '12.50'.to_money(:USD)
285
- '12.50'.mint(:BRL)
287
+ '12.50'.to_money('USD')
288
+ '12.50'.mint('BRL')
286
289
  ```
287
290
 
288
291
  These return `Mint::Money` instances.
@@ -303,16 +306,6 @@ bundle exec rake test
303
306
 
304
307
  The repository includes a dummy Rails application under `test/dummy` for exercising the engine in a Rails environment.
305
308
 
306
- ## Releasing
307
-
308
- Update the version in `lib/minting/money_attribute/version.rb`, update release notes, and build the gem:
309
-
310
- ```sh
311
- gem build minting-rails.gemspec
312
- ```
313
-
314
- Publishing is configured for RubyGems.org.
315
-
316
309
  ## Contributing
317
310
 
318
311
  Bug reports and pull requests are welcome on GitHub at [gferraz/minting-rails](https://github.com/gferraz/minting-rails).
@@ -27,22 +27,6 @@ Mint.configure do |config|
27
27
  #
28
28
  config.default_currency = 'BRL'
29
29
 
30
- # Specify a rounding mode (not yet implemented)
31
- # Any one of:
32
- #
33
- # BigDecimal::ROUND_UP,
34
- # BigDecimal::ROUND_DOWN,
35
- # BigDecimal::ROUND_HALF_UP,
36
- # BigDecimal::ROUND_HALF_DOWN,
37
- # BigDecimal::ROUND_HALF_EVEN,
38
- # BigDecimal::ROUND_CEILING,
39
- # BigDecimal::ROUND_FLOOR
40
- #
41
- # set to BigDecimal::ROUND_HALF_EVEN by default
42
- #
43
- # config.rounding_mode = BigDecimal::ROUND_HALF_UP
44
-
45
- # Set default money format globally.
46
30
  # Default value is nil meaning "ignore this option".
47
31
  # Example:
48
32
  #
@@ -42,7 +42,7 @@ module Mint
42
42
 
43
43
  def find_money_attributes(name, mapping:)
44
44
  composite = if mapping.present?
45
- { amount: mapping.key(:amount).to_s, currency: mapping.key(:currency).to_s }
45
+ { amount: mapping[:amount].to_s, currency: mapping[:currency].to_s }
46
46
  else
47
47
  { amount: "#{name}_amount", currency: "#{name}_currency" }
48
48
  end
@@ -4,25 +4,24 @@ module Mint
4
4
  # MoneyAttribute
5
5
  module MoneyAttribute
6
6
  class Parser
7
- def initialize(currency)
7
+ def initialize(currency = Mint.default_currency)
8
8
  @default_currency = currency
9
9
  end
10
10
 
11
- def call(amount, currency = @default_currency)
11
+ def parse(amount, currency = @default_currency)
12
12
  currency = Mint.assert_valid_currency!(currency)
13
13
  case amount
14
14
  when NilClass then nil
15
- when Mint::Money then amount
16
15
  when Numeric then Mint::Money.create(amount, currency)
17
16
  when String then Mint::Money.create(amount.to_r, currency)
17
+ when Mint::Money
18
+ return amount if amount.currency == currency
19
+ raise TypeError, "Cannot automatically convert #{amount} to #{currency.code}"
18
20
  else
19
- if amount.respond_to? :to_money
20
- amount.to_money(currency)
21
- else
22
- Mint.parse(amount, currency)
23
- end
21
+ Mint.parse(amount, currency)
24
22
  end
25
23
  end
24
+ alias_method :call, :parse
26
25
  end
27
26
  end
28
27
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Mint
4
4
  module MoneyAttribute
5
- VERSION = '0.5.0'
5
+ VERSION = '0.7.0'
6
6
  end
7
7
  end
@@ -15,7 +15,7 @@ module Mint
15
15
  else
16
16
  code, subunit, symbol = *currency_data
17
17
  end
18
- Mint.register_currency(code:, subunit:, symbol:)
18
+ Mint.register_currency(code:, subunit:, symbol:)
19
19
  end
20
20
  end
21
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minting-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gilson Ferraz
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: 1.0.0
18
+ version: 1.6.0
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
- version: 1.0.0
25
+ version: 1.6.0
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rails
28
28
  requirement: !ruby/object:Gem::Requirement