money_column 0.1.0 → 0.2.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/Gemfile CHANGED
@@ -3,4 +3,4 @@ source :rubygems
3
3
  # Specify gem dependencies in money_column.gemspec
4
4
  gemspec
5
5
 
6
- gem 'activerecord', '2.3.14'
6
+ gem 'activesupport', '2.3.14'
data/Gemfile.lock CHANGED
@@ -1,15 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- money_column (0.1.0)
5
- activerecord (>= 2.3.14)
4
+ money_column (0.2.0)
5
+ activesupport (>= 2.3.14)
6
6
  money (~> 2.2.0)
7
7
 
8
8
  GEM
9
9
  remote: http://rubygems.org/
10
10
  specs:
11
- activerecord (2.3.14)
12
- activesupport (= 2.3.14)
13
11
  activesupport (2.3.14)
14
12
  diff-lcs (1.1.3)
15
13
  growl (1.0.3)
@@ -28,18 +26,16 @@ GEM
28
26
  rspec-expectations (2.7.0)
29
27
  diff-lcs (~> 1.1.2)
30
28
  rspec-mocks (2.7.0)
31
- sqlite3 (1.3.4)
32
29
  thor (0.14.6)
33
30
 
34
31
  PLATFORMS
35
32
  ruby
36
33
 
37
34
  DEPENDENCIES
38
- activerecord (= 2.3.14)
35
+ activesupport (= 2.3.14)
39
36
  growl (~> 1.0.3)
40
37
  guard-rspec (~> 0.5.0)
41
38
  money_column!
42
39
  rake (~> 0.9.2)
43
40
  rb-fsevent (~> 0.4.2)
44
41
  rspec (~> 2.7.0)
45
- sqlite3 (~> 1.3.4)
data/README.markdown CHANGED
@@ -1,29 +1,56 @@
1
1
  MoneyColumn [![build status](https://secure.travis-ci.org/chargify/money_column.png)](http://travis-ci.org/chargify/money_column)
2
2
  ===========
3
3
 
4
- A set of helper methods for working with money-based database columns.
4
+ A set of helper methods for working with money-based attributes.
5
5
 
6
6
  ## How it works
7
7
 
8
- The money values are stored in the database in cents. The currency is not (necessarily) stored in the same table - instead, the currency is defined by a +currency+ method on the object instance.
8
+ Let's say you have the following Car class:
9
9
 
10
- Reading a money column returns a Money object (or nil, if :allow_nil is true), in the currency defined by the object instance.
10
+ ``` ruby
11
+ class Car
12
+ include MoneyColumn::StoresMoney
11
13
 
12
- Writing a money column assigns the cents-based database-backed attribute, and is assumed to be in the
13
- currency of the target instance.
14
+ attr_accessor :price_in_cents
14
15
 
15
- ## Example
16
+ stores_money :price
16
17
 
17
- ``` ruby
18
- class Subscription < ActiveRecord::Base
19
- belongs_to :site
20
-
21
- stores_money :balance
22
-
23
18
  def currency
24
- site.currency
19
+ "USD"
25
20
  end
26
21
  end
22
+
23
+ So let's go ahead and store a price_in_cents for the car:
24
+
25
+ ``` ruby
26
+ car = Car.new
27
+ car.price_in_cents = 5000
27
28
  ```
28
29
 
29
- In this example, +balance+ provides a Money-based interface to the underlying +balance_in_cents+ database column. The currency is delegated to the site.
30
+ Since we want to show this in a view as `$ 50.00`, we can now do the following:
31
+
32
+ ``` ruby
33
+ > car.price.format
34
+ => "$50.00"
35
+
36
+ > car.price
37
+ => #<Money:0x1016e9fb8 @cents=5000, @bank=#<Money::VariableExchangeBank:0x10143f6f0 @mutex=#<Mutex:0x10143f678>, @rates={}>, @currency="USD">
38
+ ```
39
+
40
+ If the `currency` instance method is defined on your class, MoneyColumn will use that. Otherwise, it will use the default currency specified in the Money gem.
41
+
42
+ ## Under the covers
43
+
44
+ When you define `stores_money :price`, MoneyColumn looks for an attribute named `price_in_cents` to use for its conversion.
45
+
46
+ If you want to explicity tell MoneyColumn where to look for the cents_attribute, do the following:
47
+
48
+ ``` ruby
49
+ class CarWithSpecifiedAttribute
50
+ include MoneyColumn::StoresMoney
51
+
52
+ attr_accessor :amount_in_cents
53
+
54
+ stores_money :price, :cents_attribute => "amount_in_cents"
55
+ end
56
+ ```
data/lib/money_column.rb CHANGED
@@ -1,5 +1,6 @@
1
- require 'active_record'
1
+ require 'rubygems'
2
2
  require 'money'
3
+ require 'active_support'
3
4
  require 'money_column/stores_money'
4
5
 
5
6
  # FREEDOM PATCH - Since a money column might validly return nil,
@@ -7,13 +7,13 @@ module MoneyColumn
7
7
  module ClassMethods
8
8
  def stores_money(money_name, options = {})
9
9
  options.reverse_merge!({
10
- :cents_column => "#{money_name}_in_cents",
10
+ :cents_attribute => "#{money_name}_in_cents",
11
11
  :allow_nil => true
12
12
  })
13
13
 
14
14
  class_eval <<-EOV
15
15
  def #{money_name}
16
- cents = send('#{options[:cents_column]}')
16
+ cents = send('#{options[:cents_attribute]}')
17
17
 
18
18
  if !#{options[:allow_nil]}
19
19
  cents ||= 0
@@ -28,7 +28,7 @@ module MoneyColumn
28
28
  end
29
29
 
30
30
  def #{money_name}=(amount)
31
- self.#{options[:cents_column]} = if amount.blank?
31
+ self.#{options[:cents_attribute]} = if amount.blank?
32
32
  nil
33
33
  else
34
34
  amount.to_money.cents
@@ -39,5 +39,3 @@ module MoneyColumn
39
39
  end
40
40
  end
41
41
  end
42
-
43
- ActiveRecord::Base.send :include, MoneyColumn::StoresMoney
data/money_column.gemspec CHANGED
@@ -4,11 +4,11 @@ Gem::Specification.new do |s|
4
4
  s.rubygems_version = '1.3.7'
5
5
 
6
6
  s.name = 'money_column'
7
- s.version = '0.1.0'
7
+ s.version = '0.2.0'
8
8
  s.date = '2011-11-30'
9
- s.summary = 'A set of helper methods for working with money-based database columns.'
10
- s.description = 'A set of helper methods for working with money-based database columns.'
11
- s.authors = ["Michael Klett"]
9
+ s.summary = 'A set of helper methods for working with money-based attributes.'
10
+ s.description = 'A set of helper methods for working with money-based attributes.'
11
+ s.authors = ["Michael Klett", "Shay Frendt"]
12
12
  s.email = 'support@chargify.com'
13
13
  s.homepage = 'http://github.com/chargify/money_column'
14
14
 
@@ -17,11 +17,10 @@ Gem::Specification.new do |s|
17
17
  s.require_paths = %w[lib]
18
18
 
19
19
  # Runtime Dependencies
20
- s.add_runtime_dependency('activerecord', '>= 2.3.14')
20
+ s.add_runtime_dependency('activesupport', '>= 2.3.14')
21
21
  s.add_runtime_dependency('money', '~> 2.2.0')
22
22
 
23
23
  # Development Dependencies
24
- s.add_development_dependency('sqlite3', '~> 1.3.4')
25
24
  s.add_development_dependency('rake', '~> 0.9.2')
26
25
  s.add_development_dependency('rspec', '~> 2.7.0')
27
26
  s.add_development_dependency('guard-rspec', '~> 0.5.0')
@@ -1,49 +1,114 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class Car
4
+ include MoneyColumn::StoresMoney
5
+ attr_accessor :price_in_cents
6
+ stores_money :price
7
+ end
8
+
9
+ class CarWithCurrency
10
+ include MoneyColumn::StoresMoney
11
+ attr_accessor :price_in_cents
12
+ stores_money :price
13
+
14
+ def currency
15
+ ::Money.default_currency
16
+ end
17
+ end
18
+
19
+ class CarWithNonDefaultCurrency
20
+ include MoneyColumn::StoresMoney
21
+ attr_accessor :price_in_cents
22
+ stores_money :price
23
+
24
+ def currency
25
+ "GBP"
26
+ end
27
+ end
28
+
29
+ class CarNilNotAllowed
30
+ include MoneyColumn::StoresMoney
31
+ attr_accessor :price_in_cents
32
+ stores_money :price, :allow_nil => false
33
+ end
34
+
35
+ class CarWithSpecifiedAttribute
36
+ include MoneyColumn::StoresMoney
37
+ attr_accessor :amount_in_cents
38
+ stores_money :price, :cents_attribute => "amount_in_cents"
39
+ end
40
+
3
41
  describe "MoneyColumn" do
42
+ describe "getter method" do
43
+ it "should return a money object based on the inferred cents_attribute" do
44
+ car = Car.new
45
+ car.price_in_cents = 1000
46
+ car.price.should == 10.to_money
47
+ end
48
+
49
+ it "should return a money object based on the stated cents_attribute" do
50
+ car = CarWithSpecifiedAttribute.new
51
+ car.amount_in_cents = 5000
52
+ car.price.should == 50.to_money
53
+ end
54
+ end
55
+
4
56
  describe "setter method" do
5
57
  it "should pass on money values" do
6
- MoneyExample.new(:amount => 1.to_money).amount.should == 1.to_money
58
+ car = Car.new
59
+ car.price = 1.to_money
60
+ car.price.should == 1.to_money
7
61
  end
8
62
 
9
63
  it "should convert string values to money objects" do
10
- MoneyExample.new(:amount => '2').amount.should == 2.to_money
64
+ car = CarWithCurrency.new
65
+ car.price = '2'
66
+ car.price.should == 2.to_money
11
67
  end
12
68
 
13
69
  it "should convert to money objects in the correct currency automatically" do
14
- MoneyExampleWithNonDefaultCurrency.new(:amount => '4').amount.should == Money.new(400, 'GBP')
70
+ car = CarWithNonDefaultCurrency.new
71
+ car.price = '4'
72
+ car.price.should == Money.new(400, 'GBP')
15
73
  end
16
74
 
17
75
  it "should convert numeric values to money objects" do
18
- MoneyExample.new(:amount => 3).amount.should == 3.to_money
76
+ car = Car.new
77
+ car.price = 3
78
+ car.price.should == 3.to_money
19
79
  end
20
80
 
21
81
  describe "when nil is allowed" do
22
82
  it "should treat blank values as nil" do
23
- MoneyExample.new(:amount => '').amount.should be_nil
83
+ car = Car.new
84
+ car.price = ''
85
+ car.price.should be_nil
24
86
  end
25
87
  end
26
88
 
27
89
  describe "when nil isn't allowed" do
28
90
  it "should treat blank values as $0" do
29
- MoneyExampleThatDoesNotAllowNil.new(:amount => '').amount.should == Money.new(0, 'USD')
91
+ car = CarNilNotAllowed.new
92
+ car.price = ''
93
+ car.price.should == Money.new(0, 'USD')
30
94
  end
31
95
  end
32
96
 
33
- it "should allow existing amounts to be set to nil with a blank value" do
34
- me = MoneyExample.new(:amount => 500.to_money)
35
- me.update_attribute :amount, ''
36
- me.reload
37
- me.amount.should be_nil
97
+ it "should allow existing prices to be set to nil with a blank value" do
98
+ car = Car.new
99
+ car.price = 500.to_money
100
+ car.price.should_not be_nil
101
+ car.price = ''
102
+ car.price.should be_nil
38
103
  end
39
104
  end
40
105
 
41
106
  describe "declaring a money field" do
42
107
  it "should allow the field to be declared with a different cents field" do
43
- me = MoneyExampleWithSpecifiedColumnName.create!(:amt => 5.to_money)
44
- me.amt.should == 5.to_money
45
- me.reload.amt.should == 5.to_money
46
- me.amount_in_cents.should == 5.to_money.cents
108
+ car = CarWithSpecifiedAttribute.new
109
+ car.price = 5.to_money
110
+ car.price.should == 5.to_money
111
+ car.amount_in_cents.should == 5.to_money.cents
47
112
  end
48
113
  end
49
114
 
data/spec/spec_helper.rb CHANGED
@@ -9,12 +9,3 @@ RSpec.configure do |config|
9
9
  config.alias_example_to :fit, :focused => true
10
10
  config.color_enabled = true
11
11
  end
12
-
13
- ActiveRecord::Base.configurations = {'sqlite3' => {:adapter => 'sqlite3', :database => ':memory:'}}
14
- ActiveRecord::Base.establish_connection('sqlite3')
15
-
16
- ActiveRecord::Base.logger = Logger.new(STDERR)
17
- ActiveRecord::Base.logger.level = Logger::WARN
18
-
19
- # Load test database schema
20
- load File.dirname(__FILE__) + "/schema.rb"
metadata CHANGED
@@ -1,16 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money_column
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Klett
14
+ - Shay Frendt
14
15
  autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
@@ -19,7 +20,7 @@ date: 2011-11-30 00:00:00 -05:00
19
20
  default_executable:
20
21
  dependencies:
21
22
  - !ruby/object:Gem::Dependency
22
- name: activerecord
23
+ name: activesupport
23
24
  prerelease: false
24
25
  requirement: &id001 !ruby/object:Gem::Requirement
25
26
  none: false
@@ -50,26 +51,10 @@ dependencies:
50
51
  version: 2.2.0
51
52
  type: :runtime
52
53
  version_requirements: *id002
53
- - !ruby/object:Gem::Dependency
54
- name: sqlite3
55
- prerelease: false
56
- requirement: &id003 !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- hash: 19
62
- segments:
63
- - 1
64
- - 3
65
- - 4
66
- version: 1.3.4
67
- type: :development
68
- version_requirements: *id003
69
54
  - !ruby/object:Gem::Dependency
70
55
  name: rake
71
56
  prerelease: false
72
- requirement: &id004 !ruby/object:Gem::Requirement
57
+ requirement: &id003 !ruby/object:Gem::Requirement
73
58
  none: false
74
59
  requirements:
75
60
  - - ~>
@@ -81,11 +66,11 @@ dependencies:
81
66
  - 2
82
67
  version: 0.9.2
83
68
  type: :development
84
- version_requirements: *id004
69
+ version_requirements: *id003
85
70
  - !ruby/object:Gem::Dependency
86
71
  name: rspec
87
72
  prerelease: false
88
- requirement: &id005 !ruby/object:Gem::Requirement
73
+ requirement: &id004 !ruby/object:Gem::Requirement
89
74
  none: false
90
75
  requirements:
91
76
  - - ~>
@@ -97,11 +82,11 @@ dependencies:
97
82
  - 0
98
83
  version: 2.7.0
99
84
  type: :development
100
- version_requirements: *id005
85
+ version_requirements: *id004
101
86
  - !ruby/object:Gem::Dependency
102
87
  name: guard-rspec
103
88
  prerelease: false
104
- requirement: &id006 !ruby/object:Gem::Requirement
89
+ requirement: &id005 !ruby/object:Gem::Requirement
105
90
  none: false
106
91
  requirements:
107
92
  - - ~>
@@ -113,11 +98,11 @@ dependencies:
113
98
  - 0
114
99
  version: 0.5.0
115
100
  type: :development
116
- version_requirements: *id006
101
+ version_requirements: *id005
117
102
  - !ruby/object:Gem::Dependency
118
103
  name: growl
119
104
  prerelease: false
120
- requirement: &id007 !ruby/object:Gem::Requirement
105
+ requirement: &id006 !ruby/object:Gem::Requirement
121
106
  none: false
122
107
  requirements:
123
108
  - - ~>
@@ -129,11 +114,11 @@ dependencies:
129
114
  - 3
130
115
  version: 1.0.3
131
116
  type: :development
132
- version_requirements: *id007
117
+ version_requirements: *id006
133
118
  - !ruby/object:Gem::Dependency
134
119
  name: rb-fsevent
135
120
  prerelease: false
136
- requirement: &id008 !ruby/object:Gem::Requirement
121
+ requirement: &id007 !ruby/object:Gem::Requirement
137
122
  none: false
138
123
  requirements:
139
124
  - - ~>
@@ -145,8 +130,8 @@ dependencies:
145
130
  - 2
146
131
  version: 0.4.2
147
132
  type: :development
148
- version_requirements: *id008
149
- description: A set of helper methods for working with money-based database columns.
133
+ version_requirements: *id007
134
+ description: A set of helper methods for working with money-based attributes.
150
135
  email: support@chargify.com
151
136
  executables: []
152
137
 
@@ -166,7 +151,6 @@ files:
166
151
  - lib/money_column/stores_money.rb
167
152
  - money_column.gemspec
168
153
  - spec/money_column/money_column_spec.rb
169
- - spec/schema.rb
170
154
  - spec/spec.opts
171
155
  - spec/spec_helper.rb
172
156
  has_rdoc: true
@@ -202,9 +186,8 @@ rubyforge_project:
202
186
  rubygems_version: 1.6.2
203
187
  signing_key:
204
188
  specification_version: 3
205
- summary: A set of helper methods for working with money-based database columns.
189
+ summary: A set of helper methods for working with money-based attributes.
206
190
  test_files:
207
191
  - spec/money_column/money_column_spec.rb
208
- - spec/schema.rb
209
192
  - spec/spec.opts
210
193
  - spec/spec_helper.rb
data/spec/schema.rb DELETED
@@ -1,41 +0,0 @@
1
- ActiveRecord::Schema.define(:version => 0) do
2
- create_table :money_examples, :force => true do |t|
3
- t.integer :amount_in_cents
4
- end
5
- end
6
-
7
- class MoneyExample < ActiveRecord::Base
8
- stores_money :amount
9
- end
10
-
11
- class MoneyExampleWithCurrency < ActiveRecord::Base
12
- set_table_name "money_examples"
13
-
14
- stores_money :amount
15
-
16
- def currency
17
- ::Money.default_currency
18
- end
19
- end
20
-
21
- class MoneyExampleWithNonDefaultCurrency < ActiveRecord::Base
22
- set_table_name "money_examples"
23
-
24
- stores_money :amount
25
-
26
- def currency
27
- "GBP"
28
- end
29
- end
30
-
31
- class MoneyExampleWithSpecifiedColumnName < ActiveRecord::Base
32
- set_table_name "money_examples"
33
-
34
- stores_money :amt, :cents_column => "amount_in_cents"
35
- end
36
-
37
- class MoneyExampleThatDoesNotAllowNil < ActiveRecord::Base
38
- set_table_name "money_examples"
39
-
40
- stores_money :amount, :allow_nil => false
41
- end