dolla_dolla_bill 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jeff Felchner
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ dolla_dolla_bill
2
+ =====================
3
+
4
+ Rails integration for the RubyMoney gem including helpers
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,37 @@
1
+ require "dolla_dolla_bill/version"
2
+ require 'money'
3
+
4
+ module DollaDollaBill
5
+ module ClassMethods
6
+ def money(name, options = {})
7
+ cents_field_reader = options[:cents] || :"#{name}_in_cents"
8
+ currency_field_reader = options[:currency] || :"#{name}_currency"
9
+ cents_field_writer = :"#{cents_field_reader}="
10
+ currency_field_writer = :"#{currency_field_reader}="
11
+
12
+ define_method(name) do
13
+ return nil if send(cents_field_reader).nil? || send(currency_field_reader).nil?
14
+
15
+ ::Money.new(send(cents_field_reader), send(currency_field_reader))
16
+ end
17
+
18
+ define_method("#{name}=") do |value|
19
+ value = value.to_money
20
+
21
+ send(cents_field_writer, value.cents)
22
+ send(currency_field_writer, value.currency_as_string)
23
+ end
24
+
25
+ self.class.send(:define_method, "lowest_#{name}") do
26
+ select("#{cents_field_reader}, #{currency_field_reader}").
27
+ order("#{cents_field_reader} ASC").
28
+ first.
29
+ price
30
+ end
31
+ end
32
+ end
33
+
34
+ def self.included(base)
35
+ base.extend ClassMethods
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module DollaDollaBill
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ class ExoticDancer < ActiveRecord::Base
4
+ include DollaDollaBill
5
+
6
+ money :price
7
+ end
8
+
9
+ describe DollaDollaBill do
10
+ describe '#price' do
11
+ let(:money) do
12
+ ExoticDancer.new(
13
+ price_in_cents: cents,
14
+ price_currency: currency)
15
+ end
16
+
17
+ context 'when the money components are set' do
18
+ let(:cents) { 1234 }
19
+ let(:currency) { 'USD' }
20
+
21
+ it 'creates the proper Money object' do
22
+ money.price.cents.should eql 1234
23
+ money.price.currency_as_string.should eql 'USD'
24
+ end
25
+ end
26
+
27
+ context 'when only the currency is set' do
28
+ let(:cents) { nil }
29
+ let(:currency) { 'USD' }
30
+
31
+ it 'creates the proper Money object' do
32
+ money.price.should be_nil
33
+ end
34
+ end
35
+
36
+ context 'when only the cents are set' do
37
+ let(:cents) { 1234 }
38
+ let(:currency) { nil }
39
+
40
+ it 'creates the proper Money object' do
41
+ money.price.should be_nil
42
+ end
43
+ end
44
+ end
45
+
46
+ describe '#price=' do
47
+ let(:money) { ExoticDancer.new }
48
+
49
+ context 'when set to a Money object' do
50
+ before { money.price = Money.new(5432, 'USD') }
51
+
52
+ it 'sets the attributes to the correct values' do
53
+ money.price_in_cents.should eql 5432
54
+ money.price_currency.should eql 'USD'
55
+ end
56
+ end
57
+
58
+ context 'when set to a string' do
59
+ before { money.price = '54.32' }
60
+
61
+ it 'sets the attributes to the correct values' do
62
+ money.price_in_cents.should eql 5432
63
+ money.price_currency.should eql 'USD'
64
+ end
65
+ end
66
+
67
+ context 'when set to a decimal' do
68
+ before { money.price = 54.32 }
69
+
70
+ it 'sets the attributes to the correct values' do
71
+ money.price_in_cents.should eql 5432
72
+ money.price_currency.should eql 'USD'
73
+ end
74
+ end
75
+ end
76
+
77
+ describe '.lowest_money' do
78
+ context 'when there are two items with different amounts' do
79
+ let!(:higher_money) { ExoticDancer.create( price_in_cents: 5433,
80
+ price_currency: 'GBP') }
81
+
82
+ let!(:lower_money) { ExoticDancer.create( price_in_cents: 5432,
83
+ price_currency: 'GBP') }
84
+
85
+ it 'is the lower amount' do
86
+ ExoticDancer.lowest_price.should eql Money.new(5432, 'GBP')
87
+ end
88
+ end
89
+
90
+ context 'when there are two items with the same amounts' do
91
+ let!(:money_1) { ExoticDancer.create( price_in_cents: 5432,
92
+ price_currency: 'GBP') }
93
+
94
+ let!(:money_2) { ExoticDancer.create( price_in_cents: 5432,
95
+ price_currency: 'GBP') }
96
+
97
+ it 'is the correct amount' do
98
+ ExoticDancer.lowest_price.should eql Money.new(5432, 'GBP')
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,35 @@
1
+ require 'sqlite3'
2
+ require 'active_record'
3
+ require 'dolla_dolla_bill'
4
+
5
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
6
+
7
+ RSpec.configure do |config|
8
+ config.before(:all) do
9
+ SQLite3::Database.new 'tmp/test.db'
10
+
11
+ ActiveRecord::Base.establish_connection(
12
+ :adapter => 'sqlite3',
13
+ :database => 'tmp/test.db'
14
+ )
15
+
16
+ class SetupTests < ActiveRecord::Migration
17
+ def up
18
+ create_table :exotic_dancers do |t|
19
+ t.integer :price_in_cents
20
+ t.string :price_currency
21
+ end
22
+ end
23
+ end
24
+
25
+ SetupTests.new.migrate(:up)
26
+ end
27
+
28
+ config.before(:each) do
29
+ ActiveRecord::Base.connection.execute 'DELETE FROM exotic_dancers'
30
+ end
31
+
32
+ config.after(:all) do
33
+ `rm -f ./tmp/test.db`
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ RSpec.configure do |config|
2
+ # Configure RSpec to run focused specs, and also respect the alias 'fit' for focused specs
3
+ config.treat_symbols_as_metadata_keys_with_true_values = true
4
+ config.filter_run :focused => true
5
+ config.alias_example_to :fit, :focused => true
6
+ config.run_all_when_everything_filtered = true
7
+ end
@@ -0,0 +1,4 @@
1
+ RSpec.configure do |config|
2
+ # Configure RSpec to respect the alias 'pit' for pending specs
3
+ config.alias_example_to :pit, :pending => true
4
+ end
metadata ADDED
@@ -0,0 +1,205 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dolla_dolla_bill
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - jfelchner
9
+ - m5rk
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-10-06 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: money
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 5.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: 5.0.0
31
+ - !ruby/object:Gem::Dependency
32
+ name: rspec
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '2.11'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '2.11'
47
+ - !ruby/object:Gem::Dependency
48
+ name: fuubar
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '1.0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: guard
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: 1.4.0
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: 1.4.0
79
+ - !ruby/object:Gem::Dependency
80
+ name: guard-rspec
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ version: 2.0.0
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: 2.0.0
95
+ - !ruby/object:Gem::Dependency
96
+ name: rb-fsevent
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ~>
101
+ - !ruby/object:Gem::Version
102
+ version: 0.9.1
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: 0.9.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: awesome_print
113
+ requirement: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ~>
117
+ - !ruby/object:Gem::Version
118
+ version: 1.1.0
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ~>
125
+ - !ruby/object:Gem::Version
126
+ version: 1.1.0
127
+ - !ruby/object:Gem::Dependency
128
+ name: activerecord
129
+ requirement: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ~>
133
+ - !ruby/object:Gem::Version
134
+ version: 3.1.8
135
+ type: :development
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ~>
141
+ - !ruby/object:Gem::Version
142
+ version: 3.1.8
143
+ - !ruby/object:Gem::Dependency
144
+ name: sqlite3
145
+ requirement: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ~>
149
+ - !ruby/object:Gem::Version
150
+ version: 1.3.6
151
+ type: :development
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ~>
157
+ - !ruby/object:Gem::Version
158
+ version: 1.3.6
159
+ description: ''
160
+ email: support@chirrpy.com
161
+ executables: []
162
+ extensions: []
163
+ extra_rdoc_files:
164
+ - README.md
165
+ - LICENSE
166
+ files:
167
+ - lib/dolla_dolla_bill/version.rb
168
+ - lib/dolla_dolla_bill.rb
169
+ - Rakefile
170
+ - README.md
171
+ - LICENSE
172
+ - spec/dolla_dolla_bill_spec.rb
173
+ - spec/spec_helper.rb
174
+ - spec/support/focused.rb
175
+ - spec/support/pending.rb
176
+ homepage: https://github.com/chirrpy/dolla_dolla_bill
177
+ licenses: []
178
+ post_install_message:
179
+ rdoc_options:
180
+ - --charset = UTF-8
181
+ require_paths:
182
+ - lib
183
+ required_ruby_version: !ruby/object:Gem::Requirement
184
+ none: false
185
+ requirements:
186
+ - - ! '>='
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ required_rubygems_version: !ruby/object:Gem::Requirement
190
+ none: false
191
+ requirements:
192
+ - - ! '>='
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ requirements: []
196
+ rubyforge_project: dolla_dolla_bill
197
+ rubygems_version: 1.8.24
198
+ signing_key:
199
+ specification_version: 3
200
+ summary: Rails integration for the RubyMoney gem including helpers
201
+ test_files:
202
+ - spec/dolla_dolla_bill_spec.rb
203
+ - spec/spec_helper.rb
204
+ - spec/support/focused.rb
205
+ - spec/support/pending.rb