monetize 1.1.0 → 1.2.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
  SHA1:
3
- metadata.gz: bbeee1313b44a976a6f7244940b8b8f3b4101054
4
- data.tar.gz: 147a0f56f5fec9af8d00861c233fe1bd607b5d6d
3
+ metadata.gz: 0da3f5e2bea465f09b1722aeedb65d9d0ff40b65
4
+ data.tar.gz: 1dc558e00ac98b0dce9900ff9f4b1d4a1fc25366
5
5
  SHA512:
6
- metadata.gz: 46456db1721dd08e2bad8fb665986ec4acfdeaaf8ea08eabd13909ba421c12a5d0d042ff7d8cfbd9b4a1a942df7608f3573043a6d49f407577df242feac10e78
7
- data.tar.gz: 9c3edb5b6b7d885403dfa1b895d2331f0ab8c4206ce9aae3739e9ec7c2ad1406dcfbfde983fa2c3dd427ca79d93beb0c2bb289c43e9fd0ef8474dfd1498dea35
6
+ metadata.gz: d56c2152a81bea83158da6cc4021b3668ef60a2396058dfdb11f5bce250467d54ee8c8fe193ccc0e114b96c88fa761879f7285072b9255b3b5b45db08b4c1022
7
+ data.tar.gz: 9553d4f65cfad911f9abf46b8d94b4be771981cbdcefd6098b71e06da4b3b143b75cefc52b8ed48e85e5275eab9094b87e5e4b26d86ae3d44349c7b8c8900361
data/CHANGELOG.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # Changelog
2
2
 
3
3
  ## master (next release)
4
+ - Add NilClass extension
4
5
  - Added correct parsing of Brazilian Real $R symbol
5
6
  - Add testing task for Brazilian Real parsing
data/CONTRIBUTING.md CHANGED
@@ -5,13 +5,16 @@
5
5
  1. Fork [the repo](https://github.com/RubyMoney/monetize)
6
6
  2. Grab dependencies: `bundle install`
7
7
  3. Make sure everything is working: `bundle exec rspec spec`
8
- 4. Make your changes
9
- 5. Test your changes
10
- 5. Create a Pull Request
11
- 6. Celebrate!!!!!
8
+ 4. Create your feature branch (`git checkout -b my-new-feature`)
9
+ 5. Make your changes
10
+ 6. Test your changes
11
+ 7. Commit your changes (`git commit -am 'Add some feature'`)
12
+ 8. Push to the branch (`git push origin my-new-feature`)
13
+ 9. Create a Pull Request
14
+ 10. Celebrate!!!!!
12
15
 
13
16
  ## Notes
14
17
 
15
- When contributing, please make sure to update the CHANGELOG when you submit
18
+ When contributing, please make sure to update the [CHANGELOG.md](CHANGELOG.md) when you submit
16
19
  your pull request. Upon merging of your first pull request, you will be
17
20
  given commit access to the repository.
data/README.md CHANGED
@@ -26,25 +26,34 @@ Or install it yourself as:
26
26
  ## Usage
27
27
 
28
28
  ```ruby
29
- Monetize.parse("$100") == Money.new(100_00, "USD")
30
- Monetize.parse("100") == Money.new(100_00, "EUR")
31
- Monetize.parse("£100") == Money.new(100_00, "GBP")
29
+ Monetize.parse("USD 100") == Money.new(100_00, "USD")
30
+ Monetize.parse("EUR 100") == Money.new(100_00, "EUR")
31
+ Monetize.parse("GBP 100") == Money.new(100_00, "GBP")
32
32
 
33
33
  "100".to_money == Money.new(100_00, "USD")
34
34
  ```
35
35
 
36
- Optionally, enable the ability to assume the currency from a passed symbol.
36
+ Optionally, enable the ability to assume the currency from a passed symbol. Otherwise, currency symbols will be ignored, and USD used as the default currency:
37
37
 
38
38
  ```ruby
39
+ Monetize.parse("£100") == Money.new(100_00, "USD")
40
+
39
41
  Monetize.assume_from_symbol = true
40
42
 
41
- "$100".to_money == Money.new(100_00, "USD")
43
+ Monetize.parse("£100") == Money.new(100_00, "GBP")
44
+ "€100".to_money == Money.new(100_00, "EUR")
45
+ ```
46
+
47
+ Monetize can also parse a list of values, returning an array-like object ([Monetize::Collection](lib/collection.rb)):
48
+
49
+ ```ruby
50
+ Monetize.parse_collection("€80/$100") == [Money.new(80_00, "EUR"), Money.new(100_00, "USD")]
51
+ Monetize.parse_collection("€80, $100") == [Money.new(80_00, "EUR"), Money.new(100_00, "USD")]
52
+
53
+ # The #range? method detects the presence of a hyphen
54
+ Monetize.parse_collection("€80-$100").range? == true
42
55
  ```
43
56
 
44
57
  ## Contributing
45
58
 
46
- 1. Fork it
47
- 2. Create your feature branch (`git checkout -b my-new-feature`)
48
- 3. Commit your changes (`git commit -am 'Add some feature'`)
49
- 4. Push to the branch (`git push origin my-new-feature`)
50
- 5. Create new Pull Request
59
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for details.
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ task default: :spec
5
+
6
+ RSpec::Core::RakeTask.new
data/lib/collection.rb ADDED
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+
3
+ require 'monetize'
4
+
5
+ module Monetize
6
+ class Collection
7
+ extend Forwardable
8
+ include Enumerable
9
+ def_delegators :@list, :[], :each, :last
10
+
11
+ attr_reader :input, :currency, :options
12
+
13
+ def self.parse(input, currency = Money.default_currency, options = {})
14
+ new(input, currency, options).parse
15
+ end
16
+
17
+ def initialize(input, currency = Money.default_currency, options = {})
18
+ if input.respond_to? :strip
19
+ @input = input.clone.strip
20
+ else
21
+ raise ArgumentError 'Input must be a string'
22
+ end
23
+
24
+ @currency = currency
25
+ @options = options
26
+ @list = []
27
+ end
28
+
29
+ def parse
30
+ if range?
31
+ @list = split_range.map { |fragment| Monetize.parse(fragment, currency, options) }
32
+ else
33
+ @list = split_list.map { |fragment| Monetize.parse(fragment, currency, options) }
34
+ end
35
+
36
+ self
37
+ end
38
+
39
+ def range?
40
+ RANGE_SPLIT =~ input
41
+ end
42
+
43
+ private
44
+
45
+ LIST_SPLIT = %r{[/,]}
46
+ RANGE_SPLIT = /-/
47
+
48
+ def split_list
49
+ Array @input.split(LIST_SPLIT)
50
+ .map(&:strip)
51
+ end
52
+
53
+ def split_range
54
+ Array @input.split(RANGE_SPLIT)
55
+ .map(&:strip)
56
+ end
57
+ end
58
+ end
data/lib/monetize.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "money"
4
4
  require "monetize/core_extensions"
5
5
  require "monetize/version"
6
+ require "collection"
6
7
 
7
8
  module Monetize
8
9
 
@@ -11,7 +12,9 @@ module Monetize
11
12
  "€" => "EUR",
12
13
  "£" => "GBP",
13
14
  "R$" => "BRL",
14
- "R" => "ZAR"
15
+ "R" => "ZAR",
16
+ "¥" => "JPY",
17
+ "C$" => "CAD"
15
18
  }
16
19
 
17
20
  # Class methods
@@ -37,6 +40,10 @@ module Monetize
37
40
  Money.new(fractional, currency)
38
41
  end
39
42
 
43
+ def self.parse_collection(input, currency = Money.default_currency, options = {})
44
+ Collection.parse(input, currency, options)
45
+ end
46
+
40
47
  def self.from_string(value, currency = Money.default_currency)
41
48
  value = BigDecimal.new(value.to_s)
42
49
  from_bigdecimal(value, currency)
@@ -1,5 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require "monetize/core_extensions/nil_class"
3
4
  require "monetize/core_extensions/numeric"
4
5
  require "monetize/core_extensions/string"
5
6
  require "monetize/core_extensions/symbol"
7
+ require "monetize/core_extensions/hash"
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ class Hash
4
+ def to_money(currency = nil)
5
+ Money.new(self[:cents], self[:currency] || currency || Money.default_currency)
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ class NilClass
2
+ def to_money(currency = nil)
3
+ Money.new(nil, currency || Money.default_currency)
4
+ end
5
+ end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Monetize
4
- VERSION = "1.1.0"
4
+ VERSION = "1.2.0"
5
5
  end
@@ -5,6 +5,21 @@ require "monetize"
5
5
  require "monetize/core_extensions"
6
6
 
7
7
  describe Monetize, "core extensions" do
8
+ describe NilClass do
9
+ describe "#to_money" do
10
+ it "work as documented" do
11
+ money = nil.to_money
12
+ expect(money.cents).to eq 0
13
+ expect(money.currency).to eq Money.default_currency
14
+ end
15
+
16
+ it "accepts optional currency" do
17
+ expect(nil.to_money('USD')).to eq Money.new(nil, 'USD')
18
+ expect(nil.to_money('EUR')).to eq Money.new(nil, 'EUR')
19
+ end
20
+ end
21
+ end
22
+
8
23
  describe Numeric do
9
24
  describe "#to_money" do
10
25
  it "work as documented" do
@@ -142,6 +157,38 @@ describe Monetize, "core extensions" do
142
157
  end
143
158
  end
144
159
 
160
+ describe Hash do
161
+ describe "#to_money" do
162
+
163
+ context "when currency is present in hash" do
164
+ subject(:hash){ { cents: 5, currency: "EUR" } }
165
+
166
+ it "converts Hash to Money using key from hash" do
167
+ expect(hash.to_money).to eq(Money.new(5, "EUR"))
168
+ end
169
+ end
170
+
171
+ context "when currency is passed as argument" do
172
+ subject(:hash){ { cents: 10 } }
173
+
174
+ it "converts Hash to Money using passed currency argument" do
175
+ expect(hash.to_money("JPY")).to eq(Money.new(10, "JPY"))
176
+ end
177
+ end
178
+
179
+ context "when no currency is passed" do
180
+ subject(:hash){ { cents: 123 } }
181
+
182
+ before { allow(Money).to receive(:default_currency).and_return("USD") }
183
+
184
+ it "converts Hash to Money using default value" do
185
+ expect(hash.to_money("USD")).to eq(Money.new(123, "USD"))
186
+ end
187
+ end
188
+
189
+ end
190
+ end
191
+
145
192
  describe Symbol do
146
193
  describe "#to_currency" do
147
194
  it "converts Symbol to Currency" do
@@ -159,5 +206,4 @@ describe Monetize, "core extensions" do
159
206
  end
160
207
  end
161
208
  end
162
-
163
209
  end
@@ -50,6 +50,14 @@ describe Monetize do
50
50
  expect(Monetize.parse("R$R9.99")).to eq Money.new(999, 'BRL')
51
51
  end
52
52
 
53
+ it "parses formatted inputs with Japanese Yen passed as a symbol" do
54
+ expect(Monetize.parse("¥999")).to eq Money.new(999, 'JPY')
55
+ end
56
+
57
+ it "parses formatted inputs with Canadian Dollar passed as a symbol" do
58
+ expect(Monetize.parse("C$9.99")).to eq Money.new(999, 'CAD')
59
+ end
60
+
53
61
  it 'should assume default currency if not a recognised symbol' do
54
62
  expect(Monetize.parse("L9.99")).to eq Money.new(999, 'USD')
55
63
  end
@@ -62,12 +70,17 @@ describe Monetize do
62
70
  expect(Monetize.parse("-€9.99")).to eq Money.new(-999, 'EUR')
63
71
  expect(Monetize.parse("-£9.99")).to eq Money.new(-999, 'GBP')
64
72
  expect(Monetize.parse("-R$R9.99")).to eq Money.new(-999, 'BRL')
73
+ expect(Monetize.parse("-¥999")).to eq Money.new(-999, 'JPY')
74
+ expect(Monetize.parse("-C$9.99")).to eq Money.new(-999, 'CAD')
65
75
  end
66
76
 
67
77
  it 'parses formatted inputs with plus and GBP passed as symbol' do
68
78
  expect(Monetize.parse("+€9.99")).to eq Money.new(999, 'EUR')
69
79
  expect(Monetize.parse("+£9.99")).to eq Money.new(999, 'GBP')
70
80
  expect(Monetize.parse("+R$R9.99")).to eq Money.new(999, 'BRL')
81
+ expect(Monetize.parse("+¥999")).to eq Money.new(999, 'JPY')
82
+ expect(Monetize.parse("+C$9.99")).to eq Money.new(999, 'CAD')
83
+
71
84
  end
72
85
 
73
86
  it 'parses formatted inputs with currency symbol and postfix minus sign' do
@@ -208,6 +221,30 @@ describe Monetize do
208
221
  end
209
222
  end
210
223
 
224
+ describe ".parse_collection" do
225
+ it "parses into a Money::Collection" do
226
+ expect(Monetize.parse_collection("$7")).to be_a Monetize::Collection
227
+ end
228
+
229
+ it "parses comma separated values" do
230
+ collection = Monetize.parse_collection("$5, $7")
231
+ expect(collection.first).to eq Monetize.parse("$5")
232
+ expect(collection.last).to eq Monetize.parse("$7")
233
+ end
234
+
235
+ it "parses slash separated values" do
236
+ collection = Monetize.parse_collection("£4.50/€6")
237
+ expect(collection.first).to eq Monetize.parse("£4.50")
238
+ expect(collection.last).to eq Monetize.parse("€6")
239
+ end
240
+
241
+ it "parses hyphens as ranges" do
242
+ collection = Monetize.parse_collection("$4 - $10")
243
+ expect(collection.first).to eq Monetize.parse("$4")
244
+ expect(collection.last).to eq Monetize.parse("$10")
245
+ end
246
+ end
247
+
211
248
  describe ".from_string" do
212
249
  it "converts given amount to cents" do
213
250
  expect(Monetize.from_string("1")).to eq Money.new(1_00)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monetize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Emmons
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-16 00:00:00.000000000 Z
11
+ date: 2015-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: money
@@ -82,8 +82,11 @@ files:
82
82
  - LICENSE.txt
83
83
  - README.md
84
84
  - Rakefile
85
+ - lib/collection.rb
85
86
  - lib/monetize.rb
86
87
  - lib/monetize/core_extensions.rb
88
+ - lib/monetize/core_extensions/hash.rb
89
+ - lib/monetize/core_extensions/nil_class.rb
87
90
  - lib/monetize/core_extensions/numeric.rb
88
91
  - lib/monetize/core_extensions/string.rb
89
92
  - lib/monetize/core_extensions/symbol.rb