counterfeit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in counterfeit.gemspec
4
+ gemspec
@@ -0,0 +1,64 @@
1
+ Counterfeit
2
+ ===========
3
+
4
+ This gem helps you count somebody's money in your Rails application.
5
+ It is basically a nice rails-wrapper for Money gem.
6
+
7
+ Installation
8
+ ------------
9
+ Gemfile:
10
+
11
+ gem 'counterfeit'
12
+
13
+ Console:
14
+
15
+ bundle install
16
+
17
+ Usage
18
+ -----
19
+
20
+ In your migrations:
21
+
22
+ create_table(:products) do |t|
23
+ t.integer :price_in_cents, :default => 0, :null => false
24
+ t.string :price_currency, :limit => 3, :null => false
25
+ end
26
+
27
+ or
28
+
29
+ create_table(:products) do |t|
30
+ t.money :price
31
+ end
32
+
33
+ In your models:
34
+
35
+ class Product < ActiveRecord::Base
36
+ has_counterfeit :price
37
+ end
38
+
39
+ You can specify default currency like this:
40
+
41
+ has_counterfeit :price, :currency => 'EUR'
42
+
43
+ Now lets play a little:
44
+
45
+ product = Product.new
46
+ product.price
47
+ # => #<Money cents:0 currency:USD>
48
+ product.price = 500
49
+ product.price
50
+ # => #<Money cents:50000 currency:USD>
51
+ product.price.exchange_to('EUR')
52
+ # => #<Money cents:35491 currency:EUR>
53
+ user = User.new
54
+ user.balance = Money.new(100000, 'RUB')
55
+ user.balance > product.price
56
+ # => false
57
+ # need moar moneez
58
+
59
+ You can get more info on the ```money``` gem page here: https://github.com/RubyMoney/money
60
+
61
+
62
+ License
63
+ -------
64
+ Counterfeit is released under the MIT license.
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rake'
10
+ require 'rake/testtask'
11
+
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'lib'
14
+ t.libs << 'test'
15
+ t.pattern = 'test/**/*_test.rb'
16
+ t.verbose = false
17
+ end
18
+
19
+ task :default => :test
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'counterfeit/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'counterfeit'
7
+ s.version = Counterfeit::VERSION
8
+ s.authors = ['Pavel Pravosud']
9
+ s.email = ['rwz@duckroll.ru']
10
+ s.homepage = 'https://github.com/rwz/counterfeit'
11
+ s.summary = %q{Easy Money gem integration for rails 3.}
12
+ s.description = %q{Easy Money gem integration for rails 3.}
13
+
14
+ s.rubyforge_project = 'counterfeit'
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ['lib']
20
+
21
+ s.add_dependency 'rails', '~> 3.0'
22
+ s.add_dependency 'money', '~> 3.7.1'
23
+ s.add_dependency 'google_currency', '~> 1.2.0'
24
+
25
+ s.add_development_dependency 'sqlite3', '~> 1.3.3'
26
+ s.add_development_dependency 'rspec', '~> 2.6.0'
27
+ end
Binary file
@@ -0,0 +1,11 @@
1
+ require 'money'
2
+ require 'money/bank/google_currency'
3
+ require 'counterfeit/version'
4
+ require 'counterfeit/schema'
5
+ require 'counterfeit/active_record'
6
+ require 'counterfeit/money'
7
+
8
+
9
+ ActiveRecord::Base.send :include, Counterfeit::ActiveRecord
10
+ ActiveRecord::ConnectionAdapters::Table.send :include, Counterfeit::Schema
11
+ ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Counterfeit::Schema
@@ -0,0 +1,31 @@
1
+ module Counterfeit
2
+ module ActiveRecord
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+
7
+ def has_counterfeit(attr, options={})
8
+ options = options.with_indifferent_access
9
+
10
+ default_currency = options.delete(:currency).try(:to_s).try(:upcase)
11
+ amount_attr = options.delete(:amount_attribute) || "#{attr}_in_cents"
12
+ currency_attr = options.delete(:currency_attribute) || "#{attr}_currency"
13
+
14
+ mapping = [[ amount_attr, 'cents' ], [ currency_attr, 'currency_as_string' ]]
15
+
16
+ after_initialize do
17
+ self[currency_attr] = default_currency || Money.default_currency.iso_code
18
+ end
19
+
20
+ composed_of attr.to_sym,
21
+ :class_name => 'Money',
22
+ :mapping => mapping,
23
+ :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
24
+ :converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
25
+
26
+ end
27
+
28
+ alias :has_money :has_counterfeit
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,28 @@
1
+ # monkeypatching Money class to catch Money::Bank::UnknownRate error
2
+
3
+ class Money
4
+ mattr_accessor :google_saved_the_day
5
+
6
+ self.google_saved_the_day = false
7
+
8
+ def self.google_to_the_rescue
9
+ !google_saved_the_day && self.google_saved_the_day = true
10
+ end
11
+
12
+ def exchange_to_with_google(*args)
13
+ exchange_to_without_google(*args)
14
+ rescue Money::Bank::UnknownRate => e
15
+
16
+ if @bank == Money::Bank::VariableExchange.instance &&
17
+ self.class.google_to_the_rescue
18
+
19
+ Money.default_bank = @bank = Money::Bank::GoogleCurrency.new
20
+ retry
21
+ else
22
+ raise e
23
+ end
24
+ end
25
+
26
+ alias_method :exchange_to_without_google, :exchange_to
27
+ alias_method :exchange_to, :exchange_to_with_google
28
+ end
@@ -0,0 +1,8 @@
1
+ module Counterfeit
2
+ module Schema
3
+ def money(name)
4
+ column "#{name.to_s}_in_cents", :integer, :default => 0, :null => false
5
+ column "#{name.to_s}_currency", :string, :limit => 3, :null => false
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module Counterfeit
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Item do
4
+ it 'responds to price method' do
5
+ Item.new.should respond_to(:price)
6
+ end
7
+
8
+ it 'responds to counterfeit method with Money object' do
9
+ Item.new.price.should be_kind_of(Money)
10
+ end
11
+
12
+ it 'sets default currency' do
13
+ Money.default_currency = Money::Currency.new('RUB')
14
+ Item.new.price_currency.should == 'RUB'
15
+ end
16
+
17
+ it 'sets specified currency by default' do
18
+ Item.new.balance_currency.should == 'THB'
19
+ end
20
+
21
+ it 'should handle custom attribute names' do
22
+ item = Item.new(:money => 500)
23
+ item.custom_money_amount.should == 500_00
24
+ item.custom_money_currency.should == 'EUR'
25
+ end
26
+
27
+ it 'should work witn has_money alias' do
28
+ Item.new.should respond_to(:another_money_attr)
29
+ end
30
+
31
+ it 'google saved the day' do
32
+ Money.google_to_the_rescue.should be_true
33
+ Money.google_to_the_rescue.should be_false
34
+ end
35
+
36
+ it 'should automatically turn on google exchange on fail' do
37
+ Money.google_saved_the_day = false
38
+ Money.default_bank = Money::Bank::VariableExchange.instance
39
+ money = Money.new(50000, 'USD')
40
+ lambda do
41
+ money.exchange_to('EUR')
42
+ end.should_not raise_error
43
+
44
+ Money.default_bank.should be_kind_of(Money::Bank::GoogleCurrency)
45
+ end
46
+
47
+ it 'should use the google hack only once' do
48
+ Money.default_bank = Money::Bank::VariableExchange.instance
49
+ money = Money.new(50000, 'USD')
50
+ money.bank.should be_kind_of(Money::Bank::VariableExchange)
51
+ lambda do
52
+ money.exchange_to('EUR')
53
+ end.should raise_error(Money::Bank::UnknownRate)
54
+ end
55
+ end
@@ -0,0 +1,63 @@
1
+ # Logfile created on 2011-06-16 20:34:50 +0700 by logger.rb/25413
2
+ SQL (0.2ms)  SELECT name
3
+ FROM sqlite_master
4
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
5
+ 
6
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "money_in_cents", "money_currency") VALUES (0, 'RUB', 0, 'RUB', 0, 'RUB')
7
+ AREL (0.2ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "money_in_cents", "money_currency") VALUES (0, 'RUB', 0, 'RUB', 0, 'RUB')
8
+ SQL (0.2ms)  SELECT name
9
+ FROM sqlite_master
10
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
11
+ 
12
+ AREL (0.4ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "money_in_cents", "money_currency") VALUES (0, 'RUB', 0, 'RUB', 0, 'RUB')
13
+ AREL (0.4ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "money_in_cents", "money_currency") VALUES (0, 'RUB', 0, 'RUB', 0, 'RUB')
14
+ SQL (0.1ms)  SELECT name
15
+ FROM sqlite_master
16
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
17
+ 
18
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "money_in_cents", "money_currency") VALUES (0, 'RUB', 0, 'THB', 0, 'RUB')
19
+ AREL (0.2ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "money_in_cents", "money_currency") VALUES (0, 'RUB', 0, 'THB', 0, 'RUB')
20
+ SQL (0.2ms)  SELECT name
21
+ FROM sqlite_master
22
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
23
+ 
24
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "money_in_cents", "money_currency") VALUES (0, 'RUB', 0, 'THB', 0, 'RUB')
25
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "money_in_cents", "money_currency") VALUES (0, 'RUB', 0, 'THB', 0, 'RUB')
26
+ SQL (0.2ms)  SELECT name
27
+ FROM sqlite_master
28
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
29
+ 
30
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "money_in_cents", "money_currency") VALUES (0, 'RUB', 0, 'THB', 0, 'RUB')
31
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "money_in_cents", "money_currency") VALUES (0, 'RUB', 0, 'THB', 0, 'RUB')
32
+ SQL (0.2ms)  SELECT name
33
+ FROM sqlite_master
34
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
35
+ 
36
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "custom_money_amount", "custom_money_currency") VALUES (0, 'RUB', 0, 'THB', NULL, NULL)
37
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "custom_money_amount", "custom_money_currency") VALUES (0, 'RUB', 0, 'THB', NULL, NULL)
38
+ SQL (0.1ms)  SELECT name
39
+ FROM sqlite_master
40
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
41
+ 
42
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "custom_money_amount", "custom_money_currency") VALUES (0, 'RUB', 0, 'THB', NULL, NULL)
43
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "custom_money_amount", "custom_money_currency") VALUES (0, 'RUB', 0, 'THB', NULL, NULL)
44
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "custom_money_amount", "custom_money_currency") VALUES (0, 'RUB', 0, 'THB', 50000, 'RUB')
45
+ SQL (0.1ms)  SELECT name
46
+ FROM sqlite_master
47
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
48
+ 
49
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "custom_money_amount", "custom_money_currency") VALUES (0, 'RUB', 0, 'THB', NULL, NULL)
50
+ AREL (0.2ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "custom_money_amount", "custom_money_currency") VALUES (0, 'RUB', 0, 'THB', NULL, NULL)
51
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "custom_money_amount", "custom_money_currency") VALUES (0, 'RUB', 0, 'THB', 50000, 'RUB')
52
+ SQL (0.1ms)  SELECT name
53
+ FROM sqlite_master
54
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
55
+ 
56
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "custom_money_amount", "custom_money_currency") VALUES (0, 'RUB', 0, 'THB', NULL, NULL)
57
+ AREL (0.2ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "custom_money_amount", "custom_money_currency") VALUES (0, 'RUB', 0, 'THB', NULL, NULL)
58
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "custom_money_amount", "custom_money_currency") VALUES (0, 'RUB', 0, 'THB', 50000, 'RUB')
59
+ SQL (0.1ms)  SELECT name
60
+ FROM sqlite_master
61
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
62
+ 
63
+ AREL (0.3ms) INSERT INTO "items" ("price_in_cents", "price_currency", "balance_in_cents", "balance_currency", "custom_money_amount", "custom_money_currency") VALUES (0, 'RUB', 0, 'THB', 50000, 'RUB')
@@ -0,0 +1,8 @@
1
+ class Item < ActiveRecord::Base
2
+ has_counterfeit :price
3
+ has_counterfeit :balance, :currency => :thb
4
+ has_money :another_money_attr
5
+ has_counterfeit :money, :currency => 'EUR',
6
+ :currency_attribute => :custom_money_currency,
7
+ :amount_attribute => :custom_money_amount
8
+ end
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Schema.define :version => 0 do
2
+ create_table "items", :force => true do |t|
3
+ t.money :price
4
+ t.money :balance
5
+ t.money :another_money_attr
6
+ t.integer :custom_money_amount
7
+ t.string :custom_money_currency
8
+ end
9
+ end
@@ -0,0 +1,42 @@
1
+ $LOAD_PATH << '.' unless $LOAD_PATH.include?('.')
2
+
3
+ begin
4
+ require 'rubygems'
5
+ require 'bundler'
6
+
7
+ if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new('0.9.5')
8
+ raise RuntimeError, 'Your bundler version is too old.' +
9
+ 'Run `gem install bundler` to upgrade.'
10
+ end
11
+
12
+ # Set up load paths for all bundled gems
13
+ Bundler.setup
14
+ rescue Bundler::GemNotFound
15
+ raise RuntimeError, 'Bundler couldn\'t find some gems.' +
16
+ 'Did you run `bundlee install`?'
17
+ end
18
+
19
+ require 'rails/all'
20
+
21
+ Bundler.require
22
+ require File.expand_path('../../lib/counterfeit', __FILE__)
23
+
24
+ db_filename = 'counterfeit.sqlite3'
25
+
26
+ File.delete(db_filename)
27
+
28
+ active_record_configuration = { :adapter => 'sqlite3', :database => db_filename }
29
+
30
+ ActiveRecord::Base.establish_connection(active_record_configuration)
31
+ ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
32
+
33
+ ActiveRecord::Base.silence do
34
+ ActiveRecord::Migration.verbose = false
35
+
36
+ load(File.dirname(__FILE__) + '/schema.rb')
37
+ load(File.dirname(__FILE__) + '/models.rb')
38
+ end
39
+
40
+ def clean_database!
41
+ ActiveRecord::Base.connection.execute "DELETE FROM #{Item.table_name}"
42
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: counterfeit
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Pavel Pravosud
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-16 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: "3.0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: money
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 3.7.1
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: google_currency
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.2.0
46
+ type: :runtime
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: sqlite3
50
+ prerelease: false
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 1.3.3
57
+ type: :development
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: rspec
61
+ prerelease: false
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 2.6.0
68
+ type: :development
69
+ version_requirements: *id005
70
+ description: Easy Money gem integration for rails 3.
71
+ email:
72
+ - rwz@duckroll.ru
73
+ executables: []
74
+
75
+ extensions: []
76
+
77
+ extra_rdoc_files: []
78
+
79
+ files:
80
+ - .gitignore
81
+ - Gemfile
82
+ - README.md
83
+ - Rakefile
84
+ - counterfeit.gemspec
85
+ - counterfeit.sqlite3
86
+ - lib/counterfeit.rb
87
+ - lib/counterfeit/active_record.rb
88
+ - lib/counterfeit/money.rb
89
+ - lib/counterfeit/schema.rb
90
+ - lib/counterfeit/version.rb
91
+ - spec/counterfeit_spec.rb
92
+ - spec/debug.log
93
+ - spec/models.rb
94
+ - spec/schema.rb
95
+ - spec/spec_helper.rb
96
+ homepage: https://github.com/rwz/counterfeit
97
+ licenses: []
98
+
99
+ post_install_message:
100
+ rdoc_options: []
101
+
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: "0"
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: "0"
116
+ requirements: []
117
+
118
+ rubyforge_project: counterfeit
119
+ rubygems_version: 1.8.5
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Easy Money gem integration for rails 3.
123
+ test_files:
124
+ - spec/counterfeit_spec.rb
125
+ - spec/debug.log
126
+ - spec/models.rb
127
+ - spec/schema.rb
128
+ - spec/spec_helper.rb