big_decimal_helper 2.0.4
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 +15 -0
- data/.gitignore +24 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +52 -0
- data/Rakefile +12 -0
- data/big_decimal_helper.gemspec +20 -0
- data/lib/big_decimal_helper.rb +15 -0
- data/lib/big_decimal_helper/comparisons.rb +21 -0
- data/lib/big_decimal_helper/conversion_protocol.rb +26 -0
- data/lib/big_decimal_helper/has_big_decimal_field.rb +20 -0
- data/lib/big_decimal_helper/pretty_inspect.rb +39 -0
- data/lib/big_decimal_helper/version.rb +3 -0
- data/spec/active_record_macro_spec.rb +25 -0
- data/spec/comparisons_spec.rb +99 -0
- data/spec/conversion_protocol_spec.rb +50 -0
- data/spec/pretty_inspect_spec.rb +49 -0
- data/spec/spec_helper.rb +7 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MjM0YjdjYzcxYTM0YjdhYmM1NjIxNGRhYmQ5ODc0YjZlOTVhZjI5ZA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZTUyZmU2Y2FhMzNjOGYwZTZlYmNkZjBkYTk5ZjUxMTAzNmFlNGY2YQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YjliY2M0NjJhMjRmNmE5ODEyZGM3YTdlMzI5ZTFmNjk5ZjdmNjQzMjc1N2I0
|
10
|
+
MWJhYmE5YWRjOWEyMDBiMjVlYThiZWRkNTQ0NzRmMmQ2YWMzOTU5NzczYjcw
|
11
|
+
YzUzNTFiNDFhNTY5MDVmYjIwMjliOTFiNDY2NDEwYWFiMmQyM2Y=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
OTkwOGU0NTc5NDQ0N2MwNDcyZDA0MmNiMTU3ZDExMDU4ZGU1NTAxMjYyMjVj
|
14
|
+
MTQwOGE0N2M5ZGZlMzlkNWI2MzhkYmQyOTZmZDZjMzUzMjhhZWVhYTNlY2Q2
|
15
|
+
NGFlNWFiMDk1MzRiNzRhYTFlMTRkZmU1YjczYjJiOGJiZGViOGU=
|
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
|
19
|
+
# YARD artifacts
|
20
|
+
.yardoc
|
21
|
+
_yardoc
|
22
|
+
doc/
|
23
|
+
.ruby-version
|
24
|
+
.ruby-gemset
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --order random
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 livingsocial
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
big_decimal_helper
|
2
|
+
==================
|
3
|
+
|
4
|
+
Helpers for working with BigDecimals in Rails apps.
|
5
|
+
|
6
|
+
* Defines a <code>#to_bd</code> conversion protocol on Float, Fixnum, String, and NilClass to ease finger typing.
|
7
|
+
* Overrides <code>BigDecimal#inspect</code> to provide a more human-readable format:
|
8
|
+
<br />1234.56.to_bd # => #<BigDecimal: 1,234.56></code>
|
9
|
+
* Provides an <code>ActiveRecord::Base.has_big_decimal_field</code> macro to automatically convert various fields to BigDecimal.
|
10
|
+
* Provides some convenience methods for finding the min and max of a list that might possibly include NaN values. (This can cause some bugs that are extremely annoying to track down.)
|
11
|
+
|
12
|
+
## A Note About Versions
|
13
|
+
|
14
|
+
As of 2.0.4, this gem will be published to rubygems.org. Earlier
|
15
|
+
versions were published internally to LivingSocial. You're
|
16
|
+
welcome to build them locally, if you're into that sort of thing.
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
gem 'big_decimal_helper'
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install big_decimal_helper
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
You'll be able to call <code>#to_bd</code> on any Fixnum, Float, String, or BigDecimal to ensure that you have a BigDecimal. <code>nil.to_bd</code> is also defined, and works similarly to <code>nil.to_f</code> or <code>nil.to_i</code>.
|
35
|
+
|
36
|
+
Also, if you have a field whose underlying type you can't change, but you want it to be automatically coerced to a BigDecimal, do this:
|
37
|
+
|
38
|
+
class Deal < ActiveRecord::Base
|
39
|
+
has_big_decimal_field :final_revenue
|
40
|
+
end
|
41
|
+
|
42
|
+
This just calls <code>.composed_of</code> behind the scenes, but since I can never remember how to use that API, I wrapped it in a more intention-revealing macro.
|
43
|
+
|
44
|
+
(*NOTE:* This gem does not declare a dependency on ActiveRecord. If this gem loads before ActiveRecord does, that macro won't automatically appear. Should this happen to you, just add <code>BigDecimalHelper.add_active_record_macro!</code> to <code>config/initializers/big_decimal_helper.rb</code> in your Rails project.)
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
|
5
|
+
desc 'Default: run specs.'
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
desc 'Run specs'
|
9
|
+
RSpec::Core::RakeTask.new do |t|
|
10
|
+
t.pattern = './spec/**/*_spec.rb' # don't need this, it's default.
|
11
|
+
# Put spec opts in a file named .rspec in root
|
12
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/big_decimal_helper/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Sam Livingston-Gray"]
|
6
|
+
gem.email = ["sam.livingstongray@livingsocial.com"]
|
7
|
+
gem.description = %q{Helpers for working with BigDecimals in Rails apps}
|
8
|
+
gem.summary = %q{Defines a #to_bd conversion protocol to ease typing, and provides an ActiveRecord macro to facilitate presentation of float fields as BigDecimal.}
|
9
|
+
gem.homepage = "https://github.com/livingsocial/big_decimal_helper"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "big_decimal_helper"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = BigDecimalHelper::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rake', '~> 0.9.2.2'
|
19
|
+
gem.add_development_dependency 'rspec', '~> 2.14.1'
|
20
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
module BigDecimalHelper
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'pathname'
|
7
|
+
lib_path = File.expand_path(File.join(File.dirname(__FILE__), 'big_decimal_helper' ))
|
8
|
+
lib_path = Pathname.new( lib_path )
|
9
|
+
|
10
|
+
require lib_path.join( 'version' )
|
11
|
+
require lib_path.join( 'conversion_protocol' )
|
12
|
+
require lib_path.join( 'pretty_inspect' )
|
13
|
+
require lib_path.join( 'has_big_decimal_field' )
|
14
|
+
require lib_path.join( 'comparisons' )
|
15
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module BigDecimalHelper
|
2
|
+
module Comparisons
|
3
|
+
def max_of(list)
|
4
|
+
sanitize_list_for_comparison(list).max
|
5
|
+
end
|
6
|
+
|
7
|
+
def min_of(list)
|
8
|
+
sanitize_list_for_comparison(list).min
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def sanitize_list_for_comparison(list)
|
14
|
+
list \
|
15
|
+
.map(&:to_bd) \
|
16
|
+
.reject(&:nan?)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
extend Comparisons
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module BigDecimalHelper
|
2
|
+
# Defines a conversion protocol for BigDecimal that is
|
3
|
+
# (a) easier to type than { BigDecimal(something.to_s) }
|
4
|
+
# (b) idempotent (by virtue of being lazy^H^H returning self when called on BigDecimal)
|
5
|
+
# (c) restricted to a few known types
|
6
|
+
module ConversionProtocol
|
7
|
+
module Self
|
8
|
+
def to_bd
|
9
|
+
self
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module ViaStringRepresentation
|
14
|
+
def to_bd
|
15
|
+
string_representation = to_s.gsub(/[^\-\d\.]/, '')
|
16
|
+
BigDecimal.new(string_representation)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
BigDecimal.send :include, BigDecimalHelper::ConversionProtocol::Self
|
23
|
+
Fixnum .send :include, BigDecimalHelper::ConversionProtocol::ViaStringRepresentation
|
24
|
+
Float .send :include, BigDecimalHelper::ConversionProtocol::ViaStringRepresentation
|
25
|
+
String .send :include, BigDecimalHelper::ConversionProtocol::ViaStringRepresentation
|
26
|
+
NilClass .send :include, BigDecimalHelper::ConversionProtocol::ViaStringRepresentation
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module BigDecimalHelper
|
2
|
+
# Provides an ActiveRecord macro to coerce fields to BigDecimal
|
3
|
+
module HasBigDecimalField
|
4
|
+
def has_big_decimal_field(attr_name)
|
5
|
+
composed_of attr_name,
|
6
|
+
:class_name => 'BigDecimal',
|
7
|
+
:mapping => [ [attr_name, :to_f] ],
|
8
|
+
:constructor => Proc.new {|x| x.to_bd },
|
9
|
+
:converter => Proc.new {|x| x.to_bd }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.add_active_record_macro!
|
14
|
+
return unless defined?( ActiveRecord::Base )
|
15
|
+
return if ActiveRecord::Base.kind_of?( BigDecimalHelper::HasBigDecimalField )
|
16
|
+
ActiveRecord::Base.extend BigDecimalHelper::HasBigDecimalField
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
BigDecimalHelper.add_active_record_macro!
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# Provide a notation that confirms that the proper class is being used,
|
2
|
+
# but which a normal human can actually read.
|
3
|
+
|
4
|
+
class BigDecimal
|
5
|
+
def inspect
|
6
|
+
BigDecimalHelper.pretty_inspect(self)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
module BigDecimalHelper
|
12
|
+
def self.pretty_inspect(bd)
|
13
|
+
"#<BigDecimal: #{string_for_pretty_inspect(bd)}>"
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def self.string_for_pretty_inspect(bd)
|
19
|
+
# Just use to_s for exceptional values
|
20
|
+
return bd.to_s if bd.nan? || bd.infinite?
|
21
|
+
|
22
|
+
# Lean on the Float class for basic formatting, but drop the trailing '.0'
|
23
|
+
s = bd.to_f.to_s.gsub(/\.0$/, '')
|
24
|
+
|
25
|
+
# Add commas to the integer part
|
26
|
+
int_part, dec_part = s.split('.')
|
27
|
+
int_part = int_part \
|
28
|
+
.to_s \
|
29
|
+
.reverse \
|
30
|
+
.scan(/(?:\.)?\d{1,3}-?/) \
|
31
|
+
.join(',') \
|
32
|
+
.reverse
|
33
|
+
|
34
|
+
# Put it all back together
|
35
|
+
dec_part = '.' + dec_part unless dec_part.nil?
|
36
|
+
[ int_part, dec_part ].join
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
unless defined?(ActiveRecord::Base)
|
4
|
+
module ActiveRecord
|
5
|
+
class Base
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'ActiveRecord mixin' do
|
11
|
+
before(:all) do
|
12
|
+
BigDecimalHelper.add_active_record_macro!
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'gets mixed in to ActiveRecord::Base' do
|
16
|
+
expect(ActiveRecord::Base).to be_kind_of( BigDecimalHelper::HasBigDecimalField )
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'adds a .has_big_decimal_field method that invokes .composed_of' do
|
20
|
+
expect(ActiveRecord::Base).to receive( :composed_of )
|
21
|
+
ActiveRecord::Base.class_eval do
|
22
|
+
has_big_decimal_field :foo
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "comparison helper(s, eventually)" do
|
4
|
+
it "checks sanity" do
|
5
|
+
expect( nan ).to be_nan
|
6
|
+
expect( nan ).to_not be_infinite
|
7
|
+
|
8
|
+
expect( pos_inf ).to_not be_nan
|
9
|
+
expect( pos_inf ).to be_infinite
|
10
|
+
|
11
|
+
expect( neg_inf ).to_not be_nan
|
12
|
+
expect( neg_inf ).to be_infinite
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:nan) { 0.to_bd / 0.to_bd }
|
16
|
+
let(:pos_inf) { 1.to_bd / 0.to_bd }
|
17
|
+
let(:neg_inf) { -1.to_bd / 0.to_bd }
|
18
|
+
let(:batman) { -1.to_bd }
|
19
|
+
|
20
|
+
describe BigDecimalHelper::Comparisons do
|
21
|
+
describe ".max_of" do
|
22
|
+
def max_of(list)
|
23
|
+
BigDecimalHelper.max_of(list)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "returns nil for an empty list" do
|
27
|
+
expect( max_of([]) ).to be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it "returns nil for a list with nan in it" do
|
31
|
+
expect( max_of([nan]) ).to be_nil
|
32
|
+
end
|
33
|
+
|
34
|
+
it "returns 23 for a list containing just 23" do
|
35
|
+
expect( max_of([23]) ).to eq( 23 )
|
36
|
+
end
|
37
|
+
|
38
|
+
it "returns 42 for a list containing 23 and 42" do
|
39
|
+
expect( max_of([42, 23]) ).to eq( 42 )
|
40
|
+
expect( max_of([23, 42]) ).to eq( 42 )
|
41
|
+
end
|
42
|
+
|
43
|
+
it "returns numeric output as BigDecimal" do
|
44
|
+
expect( max_of([23]) ).to be_kind_of(BigDecimal)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "returns 42 for a list with nan and 42 in it" do
|
48
|
+
expect( max_of([42, nan]) ).to eq( 42 )
|
49
|
+
end
|
50
|
+
|
51
|
+
it "returns +infinity for a list with a +infinity" do
|
52
|
+
expect( max_of([42, pos_inf]) ).to eq( pos_inf )
|
53
|
+
end
|
54
|
+
|
55
|
+
it "returns any other number for a list with a -infinity and any other number" do
|
56
|
+
expect( max_of([42, neg_inf]) ).to eq( 42 )
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe ".min_of" do
|
61
|
+
def min_of(list)
|
62
|
+
BigDecimalHelper.min_of(list)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "returns nil for an empty list" do
|
66
|
+
expect( min_of([]) ).to be_nil
|
67
|
+
end
|
68
|
+
|
69
|
+
it "returns nil for a list with nan in it" do
|
70
|
+
expect( min_of([nan]) ).to be_nil
|
71
|
+
end
|
72
|
+
|
73
|
+
it "returns 23 for a list containing just 23" do
|
74
|
+
expect( min_of([23]) ).to eq( 23 )
|
75
|
+
end
|
76
|
+
|
77
|
+
it "returns 23 for a list containing 23 and 42" do
|
78
|
+
expect( min_of([42, 23]) ).to eq( 23 )
|
79
|
+
expect( min_of([23, 42]) ).to eq( 23 )
|
80
|
+
end
|
81
|
+
|
82
|
+
it "returns numeric output as BigDecimal" do
|
83
|
+
expect( min_of([23]) ).to be_kind_of(BigDecimal)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "returns 42 for a list with nan and 42 in it" do
|
87
|
+
expect( min_of([42, nan]) ).to eq( 42 )
|
88
|
+
end
|
89
|
+
|
90
|
+
it "returns 42 for a list with a +infinity" do
|
91
|
+
expect( min_of([42, pos_inf]) ).to eq( 42 )
|
92
|
+
end
|
93
|
+
|
94
|
+
it "returns -infinity for a list with a -infinity and any other number" do
|
95
|
+
expect( min_of([42, neg_inf]) ).to eq( neg_inf )
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Conversion protocol' do
|
4
|
+
describe "for numeric types" do
|
5
|
+
it "converts a BigDecimal to a BigDecimal" do
|
6
|
+
foo = BigDecimal('42')
|
7
|
+
expect( foo.to_bd ).to eq( foo )
|
8
|
+
end
|
9
|
+
|
10
|
+
it "converts an integer to a BigDecimal" do
|
11
|
+
expect( 42.to_bd ).to eq( BigDecimal('42') )
|
12
|
+
end
|
13
|
+
|
14
|
+
it "converts a float to a BigDecimal" do
|
15
|
+
expect( 1.23.to_bd ).to eq( BigDecimal('1.23') )
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "for strings" do
|
20
|
+
it "converts a string to a BigDecimal" do
|
21
|
+
expect( '42'.to_bd ).to eq( BigDecimal('42') )
|
22
|
+
end
|
23
|
+
|
24
|
+
it "converts a string with commas to a BigDecimal" do
|
25
|
+
expect( '42,000'.to_bd ).to eq( BigDecimal('42000') )
|
26
|
+
end
|
27
|
+
|
28
|
+
it "converts a string with dollar signs to a BigDecimal" do
|
29
|
+
expect( '$42,000'.to_bd ).to eq( BigDecimal('42000') )
|
30
|
+
end
|
31
|
+
|
32
|
+
it "doesn't drop decimals either" do
|
33
|
+
expect( '$42,000.42'.to_bd ).to eq( BigDecimal('42000.42') )
|
34
|
+
end
|
35
|
+
|
36
|
+
it "groks negative numbers" do # oops
|
37
|
+
expect( '-42,000.42'.to_bd ).to eq( BigDecimal('-42000.42') )
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "for other types" do
|
42
|
+
it "converts nil to zero, a la #to_i and #to_f" do
|
43
|
+
expect( nil.to_bd ).to eq( BigDecimal('0') )
|
44
|
+
end
|
45
|
+
|
46
|
+
it "won't even try to convert an Array to a BigDecimal" do
|
47
|
+
expect { %w[ 1 2 3 go ].to_bd }.to raise_error(NoMethodError)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Pretty #inspect' do
|
4
|
+
it "handles NaN" do
|
5
|
+
expect( format(0 / 0.to_bd) ).to eq( '#<BigDecimal: NaN>' )
|
6
|
+
expect( format(0.to_bd / 0 ) ).to eq( '#<BigDecimal: NaN>' )
|
7
|
+
expect( format(0.to_bd / 0.to_bd) ).to eq( '#<BigDecimal: NaN>' )
|
8
|
+
end
|
9
|
+
|
10
|
+
it "handles Infinity" do
|
11
|
+
expect( format(1 / 0.to_bd) ).to eq( '#<BigDecimal: Infinity>' )
|
12
|
+
expect( format(1.to_bd / 0 ) ).to eq( '#<BigDecimal: Infinity>' )
|
13
|
+
expect( format(1.to_bd / 0.to_bd) ).to eq( '#<BigDecimal: Infinity>' )
|
14
|
+
end
|
15
|
+
|
16
|
+
it "handles -Infinity" do
|
17
|
+
expect( format(-1 / 0.to_bd) ).to eq( '#<BigDecimal: -Infinity>' )
|
18
|
+
expect( format(-1.to_bd / 0 ) ).to eq( '#<BigDecimal: -Infinity>' )
|
19
|
+
expect( format(-1.to_bd / 0.to_bd) ).to eq( '#<BigDecimal: -Infinity>' )
|
20
|
+
end
|
21
|
+
|
22
|
+
it "doesn't use pseudoscientific notation" do
|
23
|
+
expect( format(1.23) ).to eq( '#<BigDecimal: 1.23>' )
|
24
|
+
end
|
25
|
+
|
26
|
+
it "groks negative numbers" do
|
27
|
+
expect( format(-1.23) ).to eq( '#<BigDecimal: -1.23>' )
|
28
|
+
end
|
29
|
+
|
30
|
+
it "groks negative numbers, even small ones" do
|
31
|
+
expect( format(-0.5) ).to eq('#<BigDecimal: -0.5>' )
|
32
|
+
end
|
33
|
+
|
34
|
+
it "inserts commas left of the decimal point" do
|
35
|
+
expect( format(1234) ).to eq('#<BigDecimal: 1,234>' )
|
36
|
+
end
|
37
|
+
|
38
|
+
it "inserts commas left of the decimal point in negative numbers" do
|
39
|
+
expect( format(-1234) ).to eq('#<BigDecimal: -1,234>' )
|
40
|
+
end
|
41
|
+
|
42
|
+
it "does all of the above at once" do
|
43
|
+
expect( format(-1234.56) ).to eq('#<BigDecimal: -1,234.56>' )
|
44
|
+
end
|
45
|
+
|
46
|
+
def format(x)
|
47
|
+
x.to_bd.inspect
|
48
|
+
end
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: big_decimal_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sam Livingston-Gray
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.9.2.2
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.9.2.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.14.1
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.14.1
|
41
|
+
description: Helpers for working with BigDecimals in Rails apps
|
42
|
+
email:
|
43
|
+
- sam.livingstongray@livingsocial.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .rspec
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- big_decimal_helper.gemspec
|
55
|
+
- lib/big_decimal_helper.rb
|
56
|
+
- lib/big_decimal_helper/comparisons.rb
|
57
|
+
- lib/big_decimal_helper/conversion_protocol.rb
|
58
|
+
- lib/big_decimal_helper/has_big_decimal_field.rb
|
59
|
+
- lib/big_decimal_helper/pretty_inspect.rb
|
60
|
+
- lib/big_decimal_helper/version.rb
|
61
|
+
- spec/active_record_macro_spec.rb
|
62
|
+
- spec/comparisons_spec.rb
|
63
|
+
- spec/conversion_protocol_spec.rb
|
64
|
+
- spec/pretty_inspect_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
homepage: https://github.com/livingsocial/big_decimal_helper
|
67
|
+
licenses: []
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 2.1.11
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: ! 'Defines a #to_bd conversion protocol to ease typing, and provides an ActiveRecord
|
89
|
+
macro to facilitate presentation of float fields as BigDecimal.'
|
90
|
+
test_files:
|
91
|
+
- spec/active_record_macro_spec.rb
|
92
|
+
- spec/comparisons_spec.rb
|
93
|
+
- spec/conversion_protocol_spec.rb
|
94
|
+
- spec/pretty_inspect_spec.rb
|
95
|
+
- spec/spec_helper.rb
|