economy 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +129 -0
- data/Rakefile +19 -0
- data/lib/economy.rb +43 -0
- data/lib/economy/builder.rb +81 -0
- data/lib/economy/cache.rb +26 -0
- data/lib/economy/configuration.rb +11 -0
- data/lib/economy/currencies.rb +34 -0
- data/lib/economy/currency.rb +28 -0
- data/lib/economy/exchange.rb +17 -0
- data/lib/economy/extensions/active_record/base.rb +18 -0
- data/lib/economy/money.rb +170 -0
- data/lib/economy/railtie.rb +18 -0
- data/lib/economy/rates/base.rb +27 -0
- data/lib/economy/rates/yahoo.rb +38 -0
- data/lib/economy/version.rb +5 -0
- data/lib/generators/economy/install_generator.rb +24 -0
- data/lib/generators/economy/templates/initializer.rb +13 -0
- data/lib/generators/economy/templates/migration.rb +14 -0
- data/lib/tasks/economy.rake +5 -0
- data/test/dummy/Rakefile +5 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/plan.rb +5 -0
- data/test/dummy/app/models/product.rb +5 -0
- data/test/dummy/app/views/layouts/application.html.erb +12 -0
- data/test/dummy/bin/bundle +4 -0
- data/test/dummy/bin/rails +5 -0
- data/test/dummy/bin/rake +5 -0
- data/test/dummy/bin/setup +30 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +25 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +7 -0
- data/test/dummy/config/database.yml.travis +3 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +41 -0
- data/test/dummy/config/environments/production.rb +79 -0
- data/test/dummy/config/environments/test.rb +45 -0
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/economy.rb +19 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/db/migrate/20161115135521_create_exchanges.rb +14 -0
- data/test/dummy/db/migrate/20161115145905_create_products.rb +10 -0
- data/test/dummy/db/migrate/20161115150024_create_plans.rb +11 -0
- data/test/dummy/db/schema.rb +45 -0
- data/test/dummy/log/development.log +172 -0
- data/test/dummy/log/test.log +8574 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/fixtures/yahoo/multiple.json +29 -0
- data/test/fixtures/yahoo/single.json +18 -0
- data/test/fixtures/yahoo/unknown.json +18 -0
- data/test/generator_test.rb +19 -0
- data/test/money_test.rb +142 -0
- data/test/rates_test.rb +60 -0
- data/test/record_test.rb +60 -0
- data/test/support/money_helper.rb +30 -0
- data/test/support/rates_helper.rb +16 -0
- data/test/task_test.rb +22 -0
- data/test/test_helper.rb +14 -0
- metadata +234 -0
data/test/record_test.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class RecordTest < ActiveSupport::TestCase
|
4
|
+
include MoneyHelper
|
5
|
+
|
6
|
+
test 'default currency' do
|
7
|
+
product = Product.new
|
8
|
+
assert_equal 0, product.price
|
9
|
+
assert_equal 'USD', product.price.currency.iso_code
|
10
|
+
assert_nil product.price_currency
|
11
|
+
end
|
12
|
+
|
13
|
+
test 'persistent' do
|
14
|
+
plan = Plan.create(monthly_price: 20, annually_price: 200, currency: 'UYU')
|
15
|
+
assert_equal 20, plan.monthly_price.amount
|
16
|
+
assert_equal 'UYU', plan.monthly_price.currency.iso_code
|
17
|
+
assert_equal 200, plan.annually_price.amount
|
18
|
+
assert_equal 'UYU', plan.annually_price.currency.iso_code
|
19
|
+
assert_equal 'UYU', plan.currency
|
20
|
+
|
21
|
+
plan.update monthly_price: money(5, 'USD')
|
22
|
+
assert_equal 100, plan.monthly_price.amount
|
23
|
+
assert_equal 200, plan.annually_price.amount
|
24
|
+
assert_equal 'UYU', plan.currency
|
25
|
+
|
26
|
+
product = Product.create(price: 15, price_currency: 'UYU')
|
27
|
+
assert_equal 15, product.price.amount
|
28
|
+
assert_equal 'UYU', product.price.currency.iso_code
|
29
|
+
assert_equal 'UYU', product.price_currency
|
30
|
+
|
31
|
+
product.update price: money(5, 'USD')
|
32
|
+
assert_equal 5, product.price.amount
|
33
|
+
assert_equal 'USD', product.price.currency.iso_code
|
34
|
+
assert_equal 'USD', product.price_currency
|
35
|
+
end
|
36
|
+
|
37
|
+
test 'validators' do
|
38
|
+
product = Product.new
|
39
|
+
assert product.valid?
|
40
|
+
|
41
|
+
product.price = 20
|
42
|
+
assert product.invalid?
|
43
|
+
|
44
|
+
product.price_currency = 'IO'
|
45
|
+
assert product.invalid?
|
46
|
+
|
47
|
+
product.price_currency = 'UYU'
|
48
|
+
assert product.valid?
|
49
|
+
|
50
|
+
product.price = 0
|
51
|
+
assert product.valid?
|
52
|
+
end
|
53
|
+
|
54
|
+
test 'helpers' do
|
55
|
+
product = Product.new(price: 10000)
|
56
|
+
assert product.price_came_from_user?
|
57
|
+
assert_equal '10000.00', product.price_before_type_cast
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module MoneyHelper
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
setup do
|
6
|
+
Economy.cache.clear
|
7
|
+
usd_to_uyu = Economy::Exchange.create(
|
8
|
+
service: 'Yahoo',
|
9
|
+
from: 'USD',
|
10
|
+
to: 'UYU',
|
11
|
+
rate: 20
|
12
|
+
)
|
13
|
+
usd_to_uyu.run_callbacks :commit
|
14
|
+
uyu_to_usd = Economy::Exchange.create(
|
15
|
+
service: 'Yahoo',
|
16
|
+
from: 'UYU',
|
17
|
+
to: 'USD',
|
18
|
+
rate: 0.05
|
19
|
+
)
|
20
|
+
uyu_to_usd.run_callbacks :commit
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def money(amount, currency)
|
27
|
+
Economy::Money.new amount, currency
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module RatesHelper
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
def mock_response(service, code, name)
|
7
|
+
response = mock
|
8
|
+
response.expects(:code).returns code
|
9
|
+
if name
|
10
|
+
body = File.read(File.dirname(__FILE__) + "/../fixtures/#{service}/#{name}.json")
|
11
|
+
response.expects(:body).returns body
|
12
|
+
end
|
13
|
+
response
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
data/test/task_test.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TaskTest < ActiveSupport::TestCase
|
4
|
+
include RatesHelper
|
5
|
+
|
6
|
+
setup do
|
7
|
+
Dummy::Application.load_tasks
|
8
|
+
end
|
9
|
+
|
10
|
+
test 'update rates' do
|
11
|
+
response = mock_response(:yahoo, '200', :multiple)
|
12
|
+
Net::HTTP.stubs(:get_response).returns response
|
13
|
+
silence_stream(STDOUT) do
|
14
|
+
Rake::Task['economy:update_rates'].invoke
|
15
|
+
end
|
16
|
+
|
17
|
+
assert_equal 2, Economy::Exchange.count
|
18
|
+
assert Economy::Exchange.exists?(service: 'Yahoo', from: 'USD', to: 'UYU', rate: 29.3200)
|
19
|
+
assert Economy::Exchange.exists?(service: 'Yahoo', from: 'UYU', to: 'USD', rate: 0.0341)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# Configure Rails Environment
|
2
|
+
ENV['RAILS_ENV'] = 'test'
|
3
|
+
|
4
|
+
require File.expand_path('../../test/dummy/config/environment.rb', __FILE__)
|
5
|
+
ActiveRecord::Migrator.migrations_paths = [File.expand_path('../../test/dummy/db/migrate', __FILE__)]
|
6
|
+
require 'rails/test_help'
|
7
|
+
require 'mocha/mini_test'
|
8
|
+
|
9
|
+
# Filter out Minitest backtrace while allowing backtrace from other libraries
|
10
|
+
# to be shown.
|
11
|
+
Minitest.backtrace_filter = Minitest::BacktraceFilter.new
|
12
|
+
|
13
|
+
# Load support files
|
14
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
metadata
ADDED
@@ -0,0 +1,234 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: economy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- mmontossi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.0
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 4.3.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 4.2.0
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 4.3.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: redis
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '3.2'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '3.2'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: pg
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0.18'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0.18'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: mocha
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '1.1'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '1.1'
|
75
|
+
description: High performance multicurrency money for rails.
|
76
|
+
email:
|
77
|
+
- mmontossi@gmail.com
|
78
|
+
executables: []
|
79
|
+
extensions: []
|
80
|
+
extra_rdoc_files: []
|
81
|
+
files:
|
82
|
+
- MIT-LICENSE
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- lib/economy.rb
|
86
|
+
- lib/economy/builder.rb
|
87
|
+
- lib/economy/cache.rb
|
88
|
+
- lib/economy/configuration.rb
|
89
|
+
- lib/economy/currencies.rb
|
90
|
+
- lib/economy/currency.rb
|
91
|
+
- lib/economy/exchange.rb
|
92
|
+
- lib/economy/extensions/active_record/base.rb
|
93
|
+
- lib/economy/money.rb
|
94
|
+
- lib/economy/railtie.rb
|
95
|
+
- lib/economy/rates/base.rb
|
96
|
+
- lib/economy/rates/yahoo.rb
|
97
|
+
- lib/economy/version.rb
|
98
|
+
- lib/generators/economy/install_generator.rb
|
99
|
+
- lib/generators/economy/templates/initializer.rb
|
100
|
+
- lib/generators/economy/templates/migration.rb
|
101
|
+
- lib/tasks/economy.rake
|
102
|
+
- test/dummy/Rakefile
|
103
|
+
- test/dummy/app/assets/javascripts/application.js
|
104
|
+
- test/dummy/app/assets/stylesheets/application.css
|
105
|
+
- test/dummy/app/controllers/application_controller.rb
|
106
|
+
- test/dummy/app/helpers/application_helper.rb
|
107
|
+
- test/dummy/app/models/plan.rb
|
108
|
+
- test/dummy/app/models/product.rb
|
109
|
+
- test/dummy/app/views/layouts/application.html.erb
|
110
|
+
- test/dummy/bin/bundle
|
111
|
+
- test/dummy/bin/rails
|
112
|
+
- test/dummy/bin/rake
|
113
|
+
- test/dummy/bin/setup
|
114
|
+
- test/dummy/config.ru
|
115
|
+
- test/dummy/config/application.rb
|
116
|
+
- test/dummy/config/boot.rb
|
117
|
+
- test/dummy/config/database.yml
|
118
|
+
- test/dummy/config/database.yml.travis
|
119
|
+
- test/dummy/config/environment.rb
|
120
|
+
- test/dummy/config/environments/development.rb
|
121
|
+
- test/dummy/config/environments/production.rb
|
122
|
+
- test/dummy/config/environments/test.rb
|
123
|
+
- test/dummy/config/initializers/assets.rb
|
124
|
+
- test/dummy/config/initializers/backtrace_silencers.rb
|
125
|
+
- test/dummy/config/initializers/cookies_serializer.rb
|
126
|
+
- test/dummy/config/initializers/economy.rb
|
127
|
+
- test/dummy/config/initializers/filter_parameter_logging.rb
|
128
|
+
- test/dummy/config/initializers/inflections.rb
|
129
|
+
- test/dummy/config/initializers/mime_types.rb
|
130
|
+
- test/dummy/config/initializers/session_store.rb
|
131
|
+
- test/dummy/config/initializers/wrap_parameters.rb
|
132
|
+
- test/dummy/config/locales/en.yml
|
133
|
+
- test/dummy/config/routes.rb
|
134
|
+
- test/dummy/config/secrets.yml
|
135
|
+
- test/dummy/db/migrate/20161115135521_create_exchanges.rb
|
136
|
+
- test/dummy/db/migrate/20161115145905_create_products.rb
|
137
|
+
- test/dummy/db/migrate/20161115150024_create_plans.rb
|
138
|
+
- test/dummy/db/schema.rb
|
139
|
+
- test/dummy/log/development.log
|
140
|
+
- test/dummy/log/test.log
|
141
|
+
- test/dummy/public/404.html
|
142
|
+
- test/dummy/public/422.html
|
143
|
+
- test/dummy/public/500.html
|
144
|
+
- test/dummy/public/favicon.ico
|
145
|
+
- test/fixtures/yahoo/multiple.json
|
146
|
+
- test/fixtures/yahoo/single.json
|
147
|
+
- test/fixtures/yahoo/unknown.json
|
148
|
+
- test/generator_test.rb
|
149
|
+
- test/money_test.rb
|
150
|
+
- test/rates_test.rb
|
151
|
+
- test/record_test.rb
|
152
|
+
- test/support/money_helper.rb
|
153
|
+
- test/support/rates_helper.rb
|
154
|
+
- test/task_test.rb
|
155
|
+
- test/test_helper.rb
|
156
|
+
homepage: https://github.com/mmontossi/economy
|
157
|
+
licenses:
|
158
|
+
- MIT
|
159
|
+
metadata: {}
|
160
|
+
post_install_message:
|
161
|
+
rdoc_options: []
|
162
|
+
require_paths:
|
163
|
+
- lib
|
164
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: 2.0.0
|
169
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
requirements: []
|
175
|
+
rubyforge_project:
|
176
|
+
rubygems_version: 2.5.1
|
177
|
+
signing_key:
|
178
|
+
specification_version: 4
|
179
|
+
summary: Economy for Rails
|
180
|
+
test_files:
|
181
|
+
- test/dummy/app/assets/javascripts/application.js
|
182
|
+
- test/dummy/app/assets/stylesheets/application.css
|
183
|
+
- test/dummy/app/controllers/application_controller.rb
|
184
|
+
- test/dummy/app/helpers/application_helper.rb
|
185
|
+
- test/dummy/app/models/plan.rb
|
186
|
+
- test/dummy/app/models/product.rb
|
187
|
+
- test/dummy/app/views/layouts/application.html.erb
|
188
|
+
- test/dummy/bin/bundle
|
189
|
+
- test/dummy/bin/rails
|
190
|
+
- test/dummy/bin/rake
|
191
|
+
- test/dummy/bin/setup
|
192
|
+
- test/dummy/config/application.rb
|
193
|
+
- test/dummy/config/boot.rb
|
194
|
+
- test/dummy/config/database.yml
|
195
|
+
- test/dummy/config/database.yml.travis
|
196
|
+
- test/dummy/config/environment.rb
|
197
|
+
- test/dummy/config/environments/development.rb
|
198
|
+
- test/dummy/config/environments/production.rb
|
199
|
+
- test/dummy/config/environments/test.rb
|
200
|
+
- test/dummy/config/initializers/assets.rb
|
201
|
+
- test/dummy/config/initializers/backtrace_silencers.rb
|
202
|
+
- test/dummy/config/initializers/cookies_serializer.rb
|
203
|
+
- test/dummy/config/initializers/economy.rb
|
204
|
+
- test/dummy/config/initializers/filter_parameter_logging.rb
|
205
|
+
- test/dummy/config/initializers/inflections.rb
|
206
|
+
- test/dummy/config/initializers/mime_types.rb
|
207
|
+
- test/dummy/config/initializers/session_store.rb
|
208
|
+
- test/dummy/config/initializers/wrap_parameters.rb
|
209
|
+
- test/dummy/config/locales/en.yml
|
210
|
+
- test/dummy/config/routes.rb
|
211
|
+
- test/dummy/config/secrets.yml
|
212
|
+
- test/dummy/config.ru
|
213
|
+
- test/dummy/db/migrate/20161115135521_create_exchanges.rb
|
214
|
+
- test/dummy/db/migrate/20161115145905_create_products.rb
|
215
|
+
- test/dummy/db/migrate/20161115150024_create_plans.rb
|
216
|
+
- test/dummy/db/schema.rb
|
217
|
+
- test/dummy/log/development.log
|
218
|
+
- test/dummy/log/test.log
|
219
|
+
- test/dummy/public/404.html
|
220
|
+
- test/dummy/public/422.html
|
221
|
+
- test/dummy/public/500.html
|
222
|
+
- test/dummy/public/favicon.ico
|
223
|
+
- test/dummy/Rakefile
|
224
|
+
- test/fixtures/yahoo/multiple.json
|
225
|
+
- test/fixtures/yahoo/single.json
|
226
|
+
- test/fixtures/yahoo/unknown.json
|
227
|
+
- test/generator_test.rb
|
228
|
+
- test/money_test.rb
|
229
|
+
- test/rates_test.rb
|
230
|
+
- test/record_test.rb
|
231
|
+
- test/support/money_helper.rb
|
232
|
+
- test/support/rates_helper.rb
|
233
|
+
- test/task_test.rb
|
234
|
+
- test/test_helper.rb
|