minting 1.6.2 → 1.6.3

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: 0af1a3786bbef7fd2c9d3614ccd5076e0ad6f77fb35bd0d34e636355bafd4714
4
- data.tar.gz: 520dfe15e4a3ddcd180a898769266d27502af54e3ad70503ed734ad351564639
3
+ metadata.gz: 66e576bf9b9ddb1ff390e5e5ecda0f73800b3ab8577baec9b03cc6f9962915d3
4
+ data.tar.gz: f43875174cd1ab47a0da044e4f877dd6618f5850e363d1f505484a6874346f7e
5
5
  SHA512:
6
- metadata.gz: bd835b1ff970861a98dff3f056feb8903e2cb5145f0b60e52372d754daa65d60461e5200cc07de954963c0f31d954f8a8f54faf0a4f16746119e1195dc3c206e
7
- data.tar.gz: 5f4e58dea85c7129e00aeef897810053136ba8d30cd49a4f199376b0e4f45885680bf1225b518dd479174e473da6e538efe30831e834e2ce41b3fdaf0a81235c
6
+ metadata.gz: 586c212f24ddfac8b7fb2a619aac93d02af67588c05d2ff222aca680970b80b3aa65998e175d68250d0352843794feee751dcd5da1edb7965318079bbf21d9ab
7
+ data.tar.gz: b289c844b36dc08099f1ae3ea963ef689e6cd876060d372e185a7f10cd1f3a0f6ec59ae943aacc03598becd869eb00a0402ecebad02d175e1070d3817d13b770
data/README.md CHANGED
@@ -185,6 +185,49 @@ Option 3: Install it yourself with:
185
185
  gem install minting
186
186
  ```
187
187
 
188
+ ## Configuration
189
+
190
+ ### Optional top‑level `Money` and `Currency`
191
+
192
+ By default, Minting keeps everything namespaced under `Mint`:
193
+
194
+ ```ruby
195
+ price = Mint::Money.create(10, "USD")
196
+ currency = Mint::Currency.new(code: "USD", symbol: "$", subunit: 2, priority: 0)
197
+ ```
198
+
199
+ To avoid polluting the global namespace (and to coexist nicely with other gems), **Minting dont automatically defines `Money` or `Currency` at the top level automatically**.
200
+
201
+ If you prefer the shorter `Money` / `Currency` constants in your application code, you can opt in explicitly.
202
+
203
+ There are two ways to enable shorter constants:
204
+
205
+ 1. Require dsl in your app
206
+
207
+ ```ruby
208
+ require "minting"
209
+ require "minting/dsl" # opt‑in top‑level Money / Currency
210
+ ```
211
+
212
+ 2. Call a configuration method
213
+
214
+ ```ruby
215
+ Minting.use_top_level_constants!
216
+ ```
217
+
218
+ After this, you can use:
219
+
220
+ ```ruby
221
+ price = Money.create(10, "USD") # equivalent to Mint::Money.create
222
+ tax = Money.money(2.50, "USD") # via Mint.money, still available
223
+ cur = Currency.new(code: "EUR", symbol: "€", subunit: 2, priority: 0)
224
+ ```
225
+
226
+ #### When to use this
227
+
228
+ - **Good fit:** application code, especially in Rails apps, where `Money` reads nicely in models and views.
229
+ - **Not recommended:** reusable gems/libraries. In that case, stick to the namespaced API (`Mint::Money`, `Mint::Currency`) to avoid conflicts with other libraries.
230
+
188
231
  ## Parsing strings
189
232
 
190
233
  ```ruby
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Optional top‑level aliases for application use.
4
+ # Not required automatically.
5
+
6
+ Money = Mint::Money
7
+ Currency = Mint::Currency
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Mint refinements
4
+ module Mint
5
+ refine Numeric do
6
+ def reais = Mint.money(self, 'BRL')
7
+
8
+ def dollars = Mint.money(self, 'USD')
9
+
10
+ def euros = Mint.money(self, 'EUR')
11
+
12
+ def to_money(currency) = Mint.money(self, currency)
13
+
14
+ alias_method :dollar, :dollars
15
+ alias_method :euro, :euros
16
+ alias_method :mint, :to_money
17
+ end
18
+
19
+ refine String do
20
+ def to_money(currency) = Mint.money(to_r, currency)
21
+ end
22
+
23
+ def self.use_top_level_constants!
24
+ if !defined?(::Money) && !defined?(::Currency)
25
+ require 'minting/mint/aliases'
26
+ elsif ::Money == Mint::Money && ::Currency == Mint::Currency
27
+ warn 'Warning: Money and Currency already defined as Mint aliases, skipping'
28
+ else
29
+ raise NameError, 'Cannot define top-level Money or Currency constants: already defined'
30
+ end
31
+ end
32
+ end
data/lib/minting/mint.rb CHANGED
@@ -3,9 +3,9 @@
3
3
  require 'minting/mint/currency/currency'
4
4
  require 'minting/mint/currency/currency_registry'
5
5
  require 'minting/mint/currency/world_currencies'
6
+ require 'minting/mint/dsl'
6
7
  require 'minting/mint/mint'
7
8
  require 'minting/mint/parser'
8
- require 'minting/mint/refinements'
9
9
  require 'minting/money/allocation'
10
10
  require 'minting/money/arithmetics'
11
11
  require 'minting/money/coercion'
@@ -3,5 +3,5 @@
3
3
  # Root namespace for the Minting library.
4
4
  module Minting
5
5
  # Current version of the Minting gem.
6
- VERSION = '1.6.2'
6
+ VERSION = '1.6.3'
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minting
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.2
4
+ version: 1.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gilson Ferraz
@@ -43,12 +43,13 @@ files:
43
43
  - lib/minting.rb
44
44
  - lib/minting/data/world-currencies.yaml
45
45
  - lib/minting/mint.rb
46
+ - lib/minting/mint/aliases.rb
46
47
  - lib/minting/mint/currency/currency.rb
47
48
  - lib/minting/mint/currency/currency_registry.rb
48
49
  - lib/minting/mint/currency/world_currencies.rb
50
+ - lib/minting/mint/dsl.rb
49
51
  - lib/minting/mint/mint.rb
50
52
  - lib/minting/mint/parser.rb
51
- - lib/minting/mint/refinements.rb
52
53
  - lib/minting/money/allocation.rb
53
54
  - lib/minting/money/arithmetics.rb
54
55
  - lib/minting/money/coercion.rb
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Mint refinements
4
- module Mint
5
- refine Numeric do
6
- def reais
7
- Mint.money(self, 'BRL')
8
- end
9
-
10
- def dollars
11
- Mint.money(self, 'USD')
12
- end
13
-
14
- def euros
15
- Mint.money(self, 'EUR')
16
- end
17
-
18
- def to_money(currency)
19
- Mint.money(self, currency)
20
- end
21
-
22
- alias_method :dollar, :dollars
23
- alias_method :euro, :euros
24
- alias_method :mint, :to_money
25
- end
26
-
27
- refine String do
28
- def to_money(currency) = Mint.money(to_r, currency)
29
- end
30
- end