bd_money 0.0.6 → 0.0.7
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.
- data/VERSION +1 -1
- data/bd_money.gemspec +2 -2
- data/lib/bd_money/bd_money.rb +25 -1
- data/spec/bd_money_spec.rb +7 -0
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.7
|
data/bd_money.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{bd_money}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.7"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Adrian Madrid"]
|
12
|
-
s.date = %q{2012-03-
|
12
|
+
s.date = %q{2012-03-12}
|
13
13
|
s.description = %q{This library makes it easier to deal with Money values, storing them as BigDecimal to avoid floating-point math errors.}
|
14
14
|
s.email = %q{aemadrid@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/bd_money/bd_money.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'bigdecimal'
|
2
|
+
require 'yaml'
|
2
3
|
|
3
4
|
class Money
|
4
5
|
|
@@ -21,6 +22,10 @@ class Money
|
|
21
22
|
REMOVE_RE = %r{[$,_ ]} unless const_defined?(:REMOVE_RE)
|
22
23
|
VALID_RE = %r{^(-)?(\d)+(\.\d{1,35})?$} unless const_defined?(:VALID_RE)
|
23
24
|
|
25
|
+
YAML_TYPE_CLASS = 'npadv.com,2012-03-12' unless const_defined?(:YAML_TYPE_CLASS)
|
26
|
+
YAML_TYPE_MODE = 'money' unless const_defined?(:YAML_TYPE_MODE)
|
27
|
+
YAML_TYPE_FULL = "#{YAML_TYPE_CLASS}/#{YAML_TYPE_MODE}" unless const_defined?(:YAML_TYPE_FULL)
|
28
|
+
|
24
29
|
include Comparable
|
25
30
|
|
26
31
|
class MoneyError < StandardError # :nodoc:
|
@@ -176,7 +181,7 @@ class Money
|
|
176
181
|
|
177
182
|
def formatted(*args)
|
178
183
|
defaults = args.first.is_a?(::Symbol) ? FORMATS[args.shift] : FORMATS[:default]
|
179
|
-
options
|
184
|
+
options = args.last.is_a?(::Hash) ? args.pop : { }
|
180
185
|
|
181
186
|
unit = options[:unit] || defaults[:unit]
|
182
187
|
spacer = options[:spacer] || defaults[:spacer]
|
@@ -209,6 +214,21 @@ class Money
|
|
209
214
|
end
|
210
215
|
end
|
211
216
|
|
217
|
+
def to_yaml_type
|
218
|
+
"!#{YAML_TYPE_FULL}"
|
219
|
+
end
|
220
|
+
|
221
|
+
def to_yaml(options = { })
|
222
|
+
YAML.quick_emit(self.object_id, options) do |out|
|
223
|
+
out.map(taguri, to_yaml_style) do |map|
|
224
|
+
map.add 'amount', amount.to_s('F')
|
225
|
+
map.add 'precision', @precision unless @precision.nil?
|
226
|
+
map.add 'round_mode', @round_mode unless @round_mode.nil?
|
227
|
+
map.add 'format', @format unless @format.nil?
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
212
232
|
class << self
|
213
233
|
|
214
234
|
def precision=(value)
|
@@ -253,4 +273,8 @@ class Money
|
|
253
273
|
|
254
274
|
end
|
255
275
|
|
276
|
+
end
|
277
|
+
|
278
|
+
YAML::add_domain_type(Money::YAML_TYPE_CLASS, Money::YAML_TYPE_MODE) do |_, map|
|
279
|
+
Money.new map['amount'], map['precision'], map['round_mode'], map['format']
|
256
280
|
end
|
data/spec/bd_money_spec.rb
CHANGED
@@ -383,4 +383,11 @@ describe Money do
|
|
383
383
|
end
|
384
384
|
end
|
385
385
|
|
386
|
+
describe "YAML support" do
|
387
|
+
subject { Money.new amt, 2, :floor, :no_commas }
|
388
|
+
let(:yaml_str) { %{--- !npadv.com,2012-03-12/money \namount: "3.53"\nprecision: 2\nround_mode: :floor\nformat: :no_commas\n} }
|
389
|
+
it { subject.to_yaml.should == yaml_str }
|
390
|
+
it { YAML.load(yaml_str).should == subject }
|
391
|
+
end
|
392
|
+
|
386
393
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bd_money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 7
|
10
|
+
version: 0.0.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adrian Madrid
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-03-
|
18
|
+
date: 2012-03-12 00:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|