spook_and_puff_money 0.5.7 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +1 -1
- data/README.md +2 -2
- data/lib/spook_and_puff/money.rb +33 -16
- metadata +21 -31
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f66498f529e17e83990cb37a1ffb2080750a3e003d8208c03a23d0207eb3f9d4
|
4
|
+
data.tar.gz: f11c467614ae5db0a35c469828813245d8a1ae6a0164712d96f6eaa0102b3df9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 33d11ccff4c6d06fe249ad43ddf49957ee7e9baafaff9c7837a3b8f2f78ed32a6049abafdcad71e71897d59666c79a74483ce39f120cdb87539092a00c6f1208
|
7
|
+
data.tar.gz: 41504d8bca3f596537226e83dd6b899e48595c45c748035f027db09d0b734b8c5fadfffd470886f22513f33b61ba074b612387cd6362fab4ef71f700c2bbaa1b
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# SpookAndPuff::Money
|
2
2
|
|
3
|
-
This is yet another Ruby class made to represent money. This one is dead simple. It doesn't integrate with any frameworks, it simply enforces precison by ensuring all operands -- for methods like +, -, / etc. -- are BigDecimals.
|
3
|
+
This is yet another Ruby class made to represent money. This one is dead simple. It doesn't integrate with any frameworks, it simply enforces precison by ensuring all operands -- for methods like +, -, / etc. -- are BigDecimals.
|
4
4
|
|
5
5
|
It also provides a few other conveniences like formatting.
|
6
6
|
|
@@ -62,7 +62,7 @@ The argument types of each method is documented, so don't forget to have a look
|
|
62
62
|
|
63
63
|
# SpookAndPuff::MoneyAttributes
|
64
64
|
|
65
|
-
This is a module provided as a convenience for working with ActiveRecord models. It is not required by default when using this gem, instead you must require it separately.
|
65
|
+
This is a module provided as a convenience for working with ActiveRecord models. It is not required by default when using this gem, instead you must require it separately.
|
66
66
|
|
67
67
|
Assuming you have the gem installed:
|
68
68
|
|
data/lib/spook_and_puff/money.rb
CHANGED
@@ -2,13 +2,15 @@ require 'bigdecimal'
|
|
2
2
|
|
3
3
|
# The main Spook and Puff module which namespaces our projects. Yay?
|
4
4
|
module SpookAndPuff
|
5
|
-
# The money class represents monetary values with a precision of up to seven
|
5
|
+
# The money class represents monetary values with a precision of up to seven
|
6
6
|
# digits. When used as part of a comparison or mathmatical operation it
|
7
7
|
# always ensures the other operand is coerced into a BigDecimal. This ensures
|
8
8
|
# the precision is maintained.
|
9
9
|
class Money
|
10
10
|
include Comparable
|
11
11
|
|
12
|
+
NUMBER_OF_CENTS = 100.freeze
|
13
|
+
|
12
14
|
# Stores the raw BigDecimal instance that a Money instance wraps.
|
13
15
|
attr_reader :raw
|
14
16
|
|
@@ -23,11 +25,19 @@ module SpookAndPuff
|
|
23
25
|
@raw = case value
|
24
26
|
when SpookAndPuff::Money then value.raw
|
25
27
|
when BigDecimal then value
|
26
|
-
when String then BigDecimal
|
28
|
+
when String then BigDecimal(value.gsub(/\$/, ''))
|
27
29
|
else raise TypeError.new("Money can only be initalized with a BigDecimal or String not #{value.class}.")
|
28
30
|
end
|
29
31
|
end
|
30
32
|
|
33
|
+
# Initialize a money instance by providing an number of cents
|
34
|
+
#
|
35
|
+
# @return SpookAndPuff::Money
|
36
|
+
def self.cents(cents)
|
37
|
+
raise TypeError.new('SpookAndPuff::Money#cents expects a number of cents') unless cents.is_a? Numeric
|
38
|
+
new((cents / NUMBER_OF_CENTS).to_s)
|
39
|
+
end
|
40
|
+
|
31
41
|
# A convenience method which returns an instance initalized to zero.
|
32
42
|
#
|
33
43
|
# @return SpookAndPuff::Money
|
@@ -40,10 +50,11 @@ module SpookAndPuff
|
|
40
50
|
# @param [Money, Numeric, String] other
|
41
51
|
#
|
42
52
|
# @return Money
|
43
|
-
#
|
44
|
-
# @raise ArgumentError
|
45
53
|
def ==(other)
|
46
|
-
|
54
|
+
case other
|
55
|
+
when Money then @raw == other.raw
|
56
|
+
else false
|
57
|
+
end
|
47
58
|
end
|
48
59
|
|
49
60
|
# @param [Money, Numeric, String] other
|
@@ -124,7 +135,7 @@ module SpookAndPuff
|
|
124
135
|
#
|
125
136
|
# @return BigDecimal
|
126
137
|
def cents
|
127
|
-
@raw * BigDecimal
|
138
|
+
@raw * BigDecimal('100')
|
128
139
|
end
|
129
140
|
|
130
141
|
# Returns the raw BigDecimal value.
|
@@ -170,7 +181,7 @@ module SpookAndPuff
|
|
170
181
|
#
|
171
182
|
# @raise TypeError
|
172
183
|
def percent(percentage)
|
173
|
-
Money.new(@raw * (coerce(percentage) / BigDecimal
|
184
|
+
Money.new(@raw * (coerce(percentage) / BigDecimal('100')))
|
174
185
|
end
|
175
186
|
|
176
187
|
# Calculates the proportion of the provided amount as a percentage.
|
@@ -185,7 +196,7 @@ module SpookAndPuff
|
|
185
196
|
end
|
186
197
|
|
187
198
|
# Rounds to the specified places; defaults to two.
|
188
|
-
#
|
199
|
+
#
|
189
200
|
# @param Integer places
|
190
201
|
#
|
191
202
|
# @return Money
|
@@ -193,16 +204,22 @@ module SpookAndPuff
|
|
193
204
|
Money.new(@raw.round(places))
|
194
205
|
end
|
195
206
|
|
196
|
-
# Returns a currency formatted string.
|
207
|
+
# Returns a currency formatted string, set to two decimal places.
|
197
208
|
#
|
209
|
+
# @param Hash opts
|
210
|
+
# @option opts [true, false] :prefix
|
198
211
|
# @return String
|
199
|
-
def to_s
|
200
|
-
|
212
|
+
def to_s(opts = {})
|
213
|
+
rounded = @raw.round(2)
|
214
|
+
prefix = opts[:prefix] == false ? '' : '$'
|
215
|
+
format = (opts[:drop_cents] and rounded == @raw.round) ? "%i" : "%.2f"
|
216
|
+
|
217
|
+
"#{prefix}#{format}" % rounded
|
201
218
|
end
|
202
219
|
|
203
220
|
private
|
204
221
|
|
205
|
-
# Grabs the raw value of a Money instance, erroring with a message
|
222
|
+
# Grabs the raw value of a Money instance, erroring with a message
|
206
223
|
# about comparison if it's the wrong type.
|
207
224
|
#
|
208
225
|
# @param SpookAndPuff::Money other
|
@@ -218,7 +235,7 @@ module SpookAndPuff
|
|
218
235
|
other.raw
|
219
236
|
end
|
220
237
|
|
221
|
-
# Grabs the raw value of a Money instance, erroring with a message
|
238
|
+
# Grabs the raw value of a Money instance, erroring with a message
|
222
239
|
# about comparison if it's the wrong type.
|
223
240
|
#
|
224
241
|
# @param SpookAndPuff::Money other
|
@@ -235,7 +252,7 @@ module SpookAndPuff
|
|
235
252
|
other.raw
|
236
253
|
end
|
237
254
|
|
238
|
-
# Coerces the provided value into a BigDecimal. It will handle any
|
255
|
+
# Coerces the provided value into a BigDecimal. It will handle any
|
239
256
|
# Numeric or string.
|
240
257
|
#
|
241
258
|
# @param [Numeric, String] other
|
@@ -246,8 +263,8 @@ module SpookAndPuff
|
|
246
263
|
def coerce(other)
|
247
264
|
case other
|
248
265
|
when BigDecimal then other
|
249
|
-
when String then BigDecimal
|
250
|
-
when Integer, Fixnum, Bignum then BigDecimal
|
266
|
+
when String then BigDecimal(other)
|
267
|
+
when Integer, Fixnum, Bignum then BigDecimal(other.to_s)
|
251
268
|
else raise TypeError
|
252
269
|
end
|
253
270
|
end
|
metadata
CHANGED
@@ -1,21 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spook_and_puff_money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.3
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
|
-
- Luke Sutton
|
9
7
|
- Ben Hull
|
10
|
-
autorequire:
|
8
|
+
autorequire:
|
11
9
|
bindir: bin
|
12
10
|
cert_chain: []
|
13
|
-
date:
|
11
|
+
date: 2021-12-06 00:00:00.000000000 Z
|
14
12
|
dependencies:
|
15
13
|
- !ruby/object:Gem::Dependency
|
16
14
|
name: rspec
|
17
15
|
requirement: !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
16
|
requirements:
|
20
17
|
- - '='
|
21
18
|
- !ruby/object:Gem::Version
|
@@ -23,7 +20,6 @@ dependencies:
|
|
23
20
|
type: :development
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
|
-
none: false
|
27
23
|
requirements:
|
28
24
|
- - '='
|
29
25
|
- !ruby/object:Gem::Version
|
@@ -31,69 +27,63 @@ dependencies:
|
|
31
27
|
- !ruby/object:Gem::Dependency
|
32
28
|
name: yard
|
33
29
|
requirement: !ruby/object:Gem::Requirement
|
34
|
-
none: false
|
35
30
|
requirements:
|
36
31
|
- - '='
|
37
32
|
- !ruby/object:Gem::Version
|
38
|
-
version: 0.
|
33
|
+
version: 0.9.20
|
39
34
|
type: :development
|
40
35
|
prerelease: false
|
41
36
|
version_requirements: !ruby/object:Gem::Requirement
|
42
|
-
none: false
|
43
37
|
requirements:
|
44
38
|
- - '='
|
45
39
|
- !ruby/object:Gem::Version
|
46
|
-
version: 0.
|
40
|
+
version: 0.9.20
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
42
|
name: redcarpet
|
49
43
|
requirement: !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
44
|
requirements:
|
52
45
|
- - '='
|
53
46
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
47
|
+
version: 3.5.1
|
55
48
|
type: :development
|
56
49
|
prerelease: false
|
57
50
|
version_requirements: !ruby/object:Gem::Requirement
|
58
|
-
none: false
|
59
51
|
requirements:
|
60
52
|
- - '='
|
61
53
|
- !ruby/object:Gem::Version
|
62
|
-
version:
|
63
|
-
description:
|
54
|
+
version: 3.5.1
|
55
|
+
description:
|
64
56
|
email:
|
65
|
-
-
|
57
|
+
- gems@companionstudio.com.au
|
66
58
|
executables: []
|
67
59
|
extensions: []
|
68
60
|
extra_rdoc_files: []
|
69
61
|
files:
|
70
|
-
- lib/spook_and_puff/money.rb
|
71
|
-
- lib/spook_and_puff/money_attributes.rb
|
72
62
|
- MIT-LICENSE
|
73
63
|
- README.md
|
74
|
-
|
75
|
-
|
76
|
-
|
64
|
+
- lib/spook_and_puff/money.rb
|
65
|
+
- lib/spook_and_puff/money_attributes.rb
|
66
|
+
homepage: https://github.com/companionstudio/money
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
metadata: {}
|
70
|
+
post_install_message:
|
77
71
|
rdoc_options: []
|
78
72
|
require_paths:
|
79
73
|
- lib
|
80
74
|
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
75
|
requirements:
|
83
|
-
- -
|
76
|
+
- - ">="
|
84
77
|
- !ruby/object:Gem::Version
|
85
78
|
version: '0'
|
86
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
-
none: false
|
88
80
|
requirements:
|
89
|
-
- -
|
81
|
+
- - ">="
|
90
82
|
- !ruby/object:Gem::Version
|
91
83
|
version: '0'
|
92
84
|
requirements: []
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
specification_version: 3
|
85
|
+
rubygems_version: 3.0.8
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
97
88
|
summary: A simple money class.
|
98
89
|
test_files: []
|
99
|
-
has_rdoc:
|