crazy_money 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/.rubocop.yml +39 -0
- data/.travis.yml +9 -0
- data/LICENSE.txt +1 -1
- data/Rakefile +5 -2
- data/bench/div.rb +0 -1
- data/crazy_money.gemspec +6 -4
- data/lib/crazy_money.rb +23 -13
- data/spec/crazy_money_spec.rb +17 -7
- data/spec/spec_helper.rb +71 -0
- metadata +37 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d624827a45e617ad361779b9b350c48094aefa5d
|
4
|
+
data.tar.gz: 447ce0a7248a10b6d40c908dfcda7d520423d10e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e912a41ad5d4d0799eee8ae48207ecb84b626dab88795477b7e5dc2b79303c8163b33653376acc092fab020536cbb7dc57df11ea21615c2edc412f2b8320d8d3
|
7
|
+
data.tar.gz: ae320ed84d7b709806bd456823c678ec8f86ee635756b27b4a33058ec63ce7729632f2c8d188a195780e6427ae5b2a8f35c9b3175aa462c70a96a51921172e2b
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
Metrics/AbcSize:
|
2
|
+
Enabled: false
|
3
|
+
|
4
|
+
Metrics/MethodLength:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Metrics/LineLength:
|
8
|
+
Max: 120
|
9
|
+
Exclude: [spec/**/*]
|
10
|
+
|
11
|
+
Lint/AssignmentInCondition:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Style/AccessModifierIndentation:
|
15
|
+
EnforcedStyle: outdent
|
16
|
+
|
17
|
+
Style/AndOr:
|
18
|
+
EnforcedStyle: conditionals
|
19
|
+
|
20
|
+
Style/SignalException:
|
21
|
+
EnforcedStyle: only_raise
|
22
|
+
|
23
|
+
Style/SpecialGlobalVars:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Style/StringLiterals:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Style/TrailingComma:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Style/SingleLineMethods:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
Style/Documentation:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
Style/FormatString:
|
39
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/LICENSE.txt
CHANGED
data/Rakefile
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
-
require "rspec/core/rake_task"
|
3
2
|
|
3
|
+
require "rspec/core/rake_task"
|
4
4
|
RSpec::Core::RakeTask.new(:spec) do |t|
|
5
5
|
t.ruby_opts = "-w"
|
6
6
|
end
|
7
7
|
|
8
|
-
|
8
|
+
require "rubocop/rake_task"
|
9
|
+
RuboCop::RakeTask.new
|
10
|
+
|
11
|
+
task default: [:rubocop, :spec]
|
data/bench/div.rb
CHANGED
data/crazy_money.gemspec
CHANGED
@@ -4,11 +4,11 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "crazy_money"
|
7
|
-
spec.version = "1.
|
7
|
+
spec.version = "1.3.0"
|
8
8
|
spec.authors = ["Cédric Félizard"]
|
9
9
|
spec.email = ["cedric@felizard.fr"]
|
10
|
-
spec.description =
|
11
|
-
spec.summary =
|
10
|
+
spec.description = 'Simple money wrapper'
|
11
|
+
spec.summary = 'Crazy Money'
|
12
12
|
spec.homepage = ""
|
13
13
|
spec.license = "MIT"
|
14
14
|
|
@@ -17,10 +17,12 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_runtime_dependency "i18n", ">= 0.
|
20
|
+
spec.add_runtime_dependency "i18n", ">= 0.7"
|
21
21
|
spec.add_runtime_dependency "currency_data", ">= 1.1.0"
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler", ">= 1.3"
|
24
24
|
spec.add_development_dependency "rake"
|
25
25
|
spec.add_development_dependency "rspec", ">= 3"
|
26
|
+
spec.add_development_dependency "rubocop"
|
27
|
+
spec.add_development_dependency "simplecov"
|
26
28
|
end
|
data/lib/crazy_money.rb
CHANGED
@@ -15,7 +15,7 @@ class CrazyMoney
|
|
15
15
|
new 0
|
16
16
|
end
|
17
17
|
|
18
|
-
def initialize
|
18
|
+
def initialize(amount)
|
19
19
|
@amount = BigDecimal.new(amount.to_s)
|
20
20
|
end
|
21
21
|
|
@@ -28,15 +28,15 @@ class CrazyMoney
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def inspect
|
31
|
-
"#<CrazyMoney amount=#{
|
31
|
+
"#<CrazyMoney amount=#{self}>"
|
32
32
|
end
|
33
33
|
|
34
|
-
def ==
|
34
|
+
def ==(other)
|
35
35
|
@amount == BigDecimal.new(other.to_s)
|
36
36
|
end
|
37
37
|
alias_method :eql?, :==
|
38
38
|
|
39
|
-
def <=>
|
39
|
+
def <=>(other)
|
40
40
|
@amount <=> BigDecimal.new(other.to_s)
|
41
41
|
end
|
42
42
|
|
@@ -56,25 +56,32 @@ class CrazyMoney
|
|
56
56
|
self.class.new(self * -1)
|
57
57
|
end
|
58
58
|
|
59
|
+
def round
|
60
|
+
self.class.new(@amount.round)
|
61
|
+
end
|
62
|
+
|
59
63
|
def cents(ratio = 100)
|
60
64
|
@amount * BigDecimal.new(ratio.to_s)
|
61
65
|
end
|
62
66
|
|
63
|
-
def +
|
64
|
-
|
65
|
-
def
|
66
|
-
|
67
|
+
def +(other); self.class.new(@amount + BigDecimal.new(other.to_s)); end
|
68
|
+
|
69
|
+
def -(other); self.class.new(@amount - BigDecimal.new(other.to_s)); end
|
70
|
+
|
71
|
+
def /(other); self.class.new(@amount / BigDecimal.new(other.to_s)); end
|
72
|
+
|
73
|
+
def *(other); self.class.new(@amount * BigDecimal.new(other.to_s)); end
|
67
74
|
|
68
75
|
# FIXME: needs polishing
|
69
|
-
def with_currency
|
76
|
+
def with_currency(iso_code)
|
70
77
|
currency = currency(iso_code) || raise(ArgumentError, "Unknown currency: #{iso_code.inspect}")
|
71
78
|
|
72
79
|
left, right = to_s(decimal_places: currency.decimal_places).split(".")
|
73
80
|
decimal_mark = right.nil? ? "" : currency.decimal_mark
|
74
81
|
sign = left.slice!("-")
|
75
82
|
|
76
|
-
left = left.reverse.scan(/.{1,3}/).map(&:reverse).reverse
|
77
|
-
|
83
|
+
left = left.reverse.scan(/.{1,3}/).map(&:reverse).reverse # split every 3 digits right-to-left
|
84
|
+
.join(thousands_separator)
|
78
85
|
|
79
86
|
formatted = [sign, left, decimal_mark, right].join
|
80
87
|
|
@@ -88,10 +95,13 @@ class CrazyMoney
|
|
88
95
|
private
|
89
96
|
|
90
97
|
def thousands_separator
|
91
|
-
|
98
|
+
default = " ".freeze
|
99
|
+
I18n.t("number.currency.format.thousands_separator", default: default)
|
100
|
+
rescue I18n::InvalidLocale
|
101
|
+
default
|
92
102
|
end
|
93
103
|
|
94
|
-
def currency
|
104
|
+
def currency(iso_code)
|
95
105
|
::CurrencyData.find(iso_code)
|
96
106
|
end
|
97
107
|
end
|
data/spec/crazy_money_spec.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
require "crazy_money"
|
4
4
|
|
5
|
-
describe CrazyMoney do
|
5
|
+
RSpec.describe CrazyMoney do
|
6
6
|
let(:one_third) { BigDecimal.new(1) / BigDecimal.new(3) }
|
7
7
|
|
8
8
|
describe ".zero" do
|
@@ -66,14 +66,14 @@ describe CrazyMoney do
|
|
66
66
|
|
67
67
|
describe "#positive?" do
|
68
68
|
specify { expect(CrazyMoney.new(-1)).to_not be_positive }
|
69
|
-
specify { expect(CrazyMoney.new(
|
70
|
-
specify { expect(CrazyMoney.new(
|
69
|
+
specify { expect(CrazyMoney.new(0)).to_not be_positive }
|
70
|
+
specify { expect(CrazyMoney.new(1)).to be_positive }
|
71
71
|
end
|
72
72
|
|
73
73
|
describe "#negative?" do
|
74
|
-
specify { expect(CrazyMoney.new(-1)).to
|
75
|
-
specify { expect(CrazyMoney.new(
|
76
|
-
specify { expect(CrazyMoney.new(
|
74
|
+
specify { expect(CrazyMoney.new(-1)).to be_negative }
|
75
|
+
specify { expect(CrazyMoney.new(0)).to_not be_negative }
|
76
|
+
specify { expect(CrazyMoney.new(1)).to_not be_negative }
|
77
77
|
end
|
78
78
|
|
79
79
|
describe "#zero?" do
|
@@ -88,6 +88,17 @@ describe CrazyMoney do
|
|
88
88
|
specify { expect(CrazyMoney.new(0).opposite).to be_a CrazyMoney }
|
89
89
|
end
|
90
90
|
|
91
|
+
describe "#round" do
|
92
|
+
specify { expect(CrazyMoney.new(0).round).to eq(0) }
|
93
|
+
specify { expect(CrazyMoney.new(1.23).round).to eq(1) }
|
94
|
+
specify { expect(CrazyMoney.new(1.5).round).to eq(2) }
|
95
|
+
specify { expect(CrazyMoney.new(1.999999).round).to eq(2) }
|
96
|
+
specify { expect(CrazyMoney.new(-20).round).to eq(-20) }
|
97
|
+
specify { expect(CrazyMoney.new(-20.1).round).to eq(-20) }
|
98
|
+
specify { expect(CrazyMoney.new(-20.5).round).to eq(-21) }
|
99
|
+
specify { expect(CrazyMoney.new(0).round).to be_a CrazyMoney }
|
100
|
+
end
|
101
|
+
|
91
102
|
describe "#cents" do
|
92
103
|
specify { expect(CrazyMoney.new(1.23).cents).to eq(123) }
|
93
104
|
end
|
@@ -121,4 +132,3 @@ describe CrazyMoney do
|
|
121
132
|
specify { expect(CrazyMoney.new("1337").with_currency("XPF")).to eq("1 337 CFP") }
|
122
133
|
end
|
123
134
|
end
|
124
|
-
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require "simplecov"
|
2
|
+
|
3
|
+
SimpleCov.start do
|
4
|
+
minimum_coverage 95
|
5
|
+
add_group "Sources", "lib"
|
6
|
+
add_group "Tests", "spec"
|
7
|
+
end
|
8
|
+
|
9
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
10
|
+
RSpec.configure do |config|
|
11
|
+
# rspec-expectations config goes here. You can use an alternate
|
12
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
13
|
+
# assertions if you prefer.
|
14
|
+
config.expect_with :rspec do |expectations|
|
15
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
16
|
+
# and `failure_message` of custom matchers include text for helper methods
|
17
|
+
# defined using `chain`, e.g.:
|
18
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
19
|
+
# # => "be bigger than 2 and smaller than 4"
|
20
|
+
# ...rather than:
|
21
|
+
# # => "be bigger than 2"
|
22
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
23
|
+
end
|
24
|
+
|
25
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
26
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
27
|
+
config.mock_with :rspec do |mocks|
|
28
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
29
|
+
# a real object. This is generally recommended, and will default to
|
30
|
+
# `true` in RSpec 4.
|
31
|
+
mocks.verify_partial_doubles = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
35
|
+
# recommended. For more details, see:
|
36
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
37
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
38
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
39
|
+
config.disable_monkey_patching!
|
40
|
+
|
41
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
42
|
+
# be too noisy due to issues in dependencies.
|
43
|
+
config.warnings = true
|
44
|
+
|
45
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
46
|
+
# file, and it's useful to allow more verbose output when running an
|
47
|
+
# individual spec file.
|
48
|
+
if config.files_to_run.one?
|
49
|
+
# Use the documentation formatter for detailed output,
|
50
|
+
# unless a formatter has already been configured
|
51
|
+
# (e.g. via a command-line flag).
|
52
|
+
config.default_formatter = 'doc'
|
53
|
+
end
|
54
|
+
|
55
|
+
# Print the 10 slowest examples and example groups at the
|
56
|
+
# end of the spec run, to help surface which specs are running
|
57
|
+
# particularly slow.
|
58
|
+
config.profile_examples = 10
|
59
|
+
|
60
|
+
# Run specs in random order to surface order dependencies. If you find an
|
61
|
+
# order dependency and want to debug it, you can fix the order by providing
|
62
|
+
# the seed, which is printed after each run.
|
63
|
+
# --seed 1234
|
64
|
+
config.order = :random
|
65
|
+
|
66
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
67
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
68
|
+
# test failures related to randomization by passing the same `--seed` value
|
69
|
+
# as the one that triggered the failure.
|
70
|
+
Kernel.srand config.seed
|
71
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crazy_money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cédric Félizard
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: '0.7'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: '0.7'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: currency_data
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +80,34 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '3'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
83
111
|
description: Simple money wrapper
|
84
112
|
email:
|
85
113
|
- cedric@felizard.fr
|
@@ -88,6 +116,8 @@ extensions: []
|
|
88
116
|
extra_rdoc_files: []
|
89
117
|
files:
|
90
118
|
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".rubocop.yml"
|
91
121
|
- ".travis.yml"
|
92
122
|
- Gemfile
|
93
123
|
- LICENSE.txt
|
@@ -97,6 +127,7 @@ files:
|
|
97
127
|
- crazy_money.gemspec
|
98
128
|
- lib/crazy_money.rb
|
99
129
|
- spec/crazy_money_spec.rb
|
130
|
+
- spec/spec_helper.rb
|
100
131
|
homepage: ''
|
101
132
|
licenses:
|
102
133
|
- MIT
|
@@ -117,9 +148,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
148
|
version: '0'
|
118
149
|
requirements: []
|
119
150
|
rubyforge_project:
|
120
|
-
rubygems_version: 2.
|
151
|
+
rubygems_version: 2.4.6
|
121
152
|
signing_key:
|
122
153
|
specification_version: 4
|
123
154
|
summary: Crazy Money
|
124
155
|
test_files:
|
125
156
|
- spec/crazy_money_spec.rb
|
157
|
+
- spec/spec_helper.rb
|