has_money 0.0.0 → 0.1.0
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/README.rdoc +10 -1
- data/VERSION +1 -1
- data/has_money.gemspec +53 -0
- data/lib/has_money.rb +8 -4
- data/spec/has_money_spec.rb +19 -3
- metadata +4 -3
data/README.rdoc
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
= has_money
|
2
|
+
|
3
|
+
== Usage
|
2
4
|
In your ActiveRecord model:
|
3
5
|
class Order < ActiveRecord::Base
|
4
6
|
has_money :default_price
|
@@ -7,4 +9,11 @@ In your ActiveRecord model:
|
|
7
9
|
Defines methods :default_price_in_dollars and :default_price_in_dollars= which make life easier when building forms.
|
8
10
|
|
9
11
|
> Order.new :default_price_in_dollars => '10.00'
|
10
|
-
=> #<Order default_price: 1000>
|
12
|
+
=> #<Order default_price: 1000>
|
13
|
+
|
14
|
+
== Install
|
15
|
+
As a gem:
|
16
|
+
$ gem install has_money
|
17
|
+
|
18
|
+
As a plugin:
|
19
|
+
script/plugin install git://github.com/samvincent/has_money.git
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.1.0
|
data/has_money.gemspec
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{has_money}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Greg Bell", "Sam Vincent", "Howard Yeh"]
|
12
|
+
s.date = %q{2010-10-28}
|
13
|
+
s.description = %q{Parsing the various ways people enter dollar amounts so you don't have to. Store values in cents as integer.}
|
14
|
+
s.email = %q{sam.vincent@mac.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"has_money.gemspec",
|
27
|
+
"init.rb",
|
28
|
+
"lib/has_money.rb",
|
29
|
+
"spec/has_money_spec.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/samvincent/has_money}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.6}
|
35
|
+
s.summary = %q{Parsing the various ways people enter dollar amounts so you don't have to.}
|
36
|
+
s.test_files = [
|
37
|
+
"spec/has_money_spec.rb"
|
38
|
+
]
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
42
|
+
s.specification_version = 3
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
46
|
+
else
|
47
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
48
|
+
end
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
data/lib/has_money.rb
CHANGED
@@ -28,10 +28,14 @@ module HasMoney
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def calculate_cents_from_dollars(dollars)
|
31
|
-
return nil if dollars.nil?
|
32
|
-
|
33
|
-
|
31
|
+
return nil if dollars.nil?
|
32
|
+
|
33
|
+
dollars = dollars.to_s.gsub(/[$,]/, "")
|
34
|
+
dollars[0] = '0.' if dollars =~ /^\.\d{1,2}/
|
35
|
+
return nil unless dollars.match(/^\d+(\.\d+)?$/) # xx.xxx
|
36
|
+
|
37
|
+
(BigDecimal(dollars).round(2) * 100).to_i
|
34
38
|
end
|
35
39
|
|
36
40
|
end
|
37
|
-
end
|
41
|
+
end
|
data/spec/has_money_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
|
1
2
|
require File.expand_path(File.dirname(__FILE__) + '/../lib/has_money')
|
2
3
|
|
3
4
|
describe HasMoney do
|
@@ -39,11 +40,16 @@ describe HasMoney do
|
|
39
40
|
@product.price.should == 3999
|
40
41
|
end
|
41
42
|
|
42
|
-
it "should set the amount in dollars with a string" do
|
43
|
+
it "should set the amount in dollars with a string with decimals" do
|
43
44
|
@product.price_in_dollars = '39.99'
|
44
45
|
@product.price.should == 3999
|
45
46
|
end
|
46
|
-
|
47
|
+
|
48
|
+
it "should set the amount in dollars with a string without decimals" do
|
49
|
+
@product.price_in_dollars = '39'
|
50
|
+
@product.price.should == 3900
|
51
|
+
end
|
52
|
+
|
47
53
|
it "should set the amount in dollars with a dollar sign" do
|
48
54
|
@product.price_in_dollars = "$39.99"
|
49
55
|
@product.price.should == 3999
|
@@ -53,6 +59,11 @@ describe HasMoney do
|
|
53
59
|
@product.price_in_dollars = "2,500.99"
|
54
60
|
@product.price.should == 250099
|
55
61
|
end
|
62
|
+
|
63
|
+
it "should set the amount in dollars if only cents with a period" do
|
64
|
+
@product.price_in_dollars = ".59"
|
65
|
+
@product.price.should == 59
|
66
|
+
end
|
56
67
|
|
57
68
|
it "should round the amount up to the nearest cent" do
|
58
69
|
@product.price_in_dollars = '39.998'
|
@@ -71,6 +82,11 @@ describe HasMoney do
|
|
71
82
|
|
72
83
|
it "should set nil if an empty string is passed in as dollars" do
|
73
84
|
@product.price_in_dollars = ''
|
85
|
+
@product.price.should == nil
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should set nil if a non numerical string is passed in as dollars" do
|
89
|
+
@product.price_in_dollars = 'two dollars'
|
74
90
|
@product.price.should == nil
|
75
91
|
end
|
76
92
|
|
@@ -80,4 +96,4 @@ describe HasMoney do
|
|
80
96
|
end
|
81
97
|
end
|
82
98
|
|
83
|
-
end
|
99
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.0
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Greg Bell
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-
|
19
|
+
date: 2010-10-28 00:00:00 -07:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- README.rdoc
|
50
50
|
- Rakefile
|
51
51
|
- VERSION
|
52
|
+
- has_money.gemspec
|
52
53
|
- init.rb
|
53
54
|
- lib/has_money.rb
|
54
55
|
- spec/has_money_spec.rb
|