multi_currency 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 64485780a95deef7cb922f32aeb87c496b983a61
4
+ data.tar.gz: 682ca0b6ad9fe8853240c30e54600789457bc569
5
+ SHA512:
6
+ metadata.gz: a0a92123f74df001010f570680bb645aef6d9ba9c1fb5d67d984ceb46d52ba012c548afdc4755666da483576f365fb2636624df547fc1980980dae0a70fbd4e5
7
+ data.tar.gz: 7134b779e413d4950b8ac4712c7c19be6922a25c7bc8c482ab9b70f65188b536034978949598cd42966c2c76cd0eb91d65619e6cabf70db6a4638777b3de7d8e
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in multi_currency.gemspec
4
+ gemspec
5
+
6
+ gem 'activesupport'
7
+ gem 'money', '~>5.1.1'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Rijaludin Muhsin
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.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # MultiCurrency
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'multi_currency', github: 'xsoulsyndicate/multi_currency'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ NOTE: Still in development
18
+
19
+ For example you already had 'price' attribute in Product model and want to add multi currency support for it.
20
+
21
+ ```ruby
22
+ class Product < ActiveRecord::Base
23
+ attr_accessible :price, :price_rate_date, :price_currency, :price_source_amount, :price_source_currency # For Rails 3
24
+
25
+ multi_currency_for [:price]
26
+
27
+ before_save :do_currency_exchange
28
+ end
29
+ ```
30
+
31
+ Add required migration. Create a migration and add this columns:
32
+
33
+ ```ruby
34
+ add_column :products, :price_rate_date, :date
35
+ add_column :products, :price_currency, :string
36
+ add_column :products, :price_source_amount, :decimal
37
+ add_column :products, :price_source_currency, :string
38
+ ```
39
+
40
+ All columns are prefixed with :price in this case. Adjust the prefix according to the column name.
41
+
42
+ So when you save a product, you can just do it like this.
43
+
44
+ ```ruby
45
+ product = Product.new(price_source_amount: 100, price_source_currency: 'USD')
46
+ product.save
47
+ ```
48
+
49
+ The required attributes to set is only _source_currency and _source_amount. You can set _rate_date if you want to specify in which date the currency exchange rate will be fetched.
50
+
51
+ After that you can get price in any currency:
52
+
53
+ ```ruby
54
+ product = Product.first
55
+ product.price_in('EUR')
56
+ ```
57
+
58
+ Default exchange rate is rate from _rate_date column. If you want to get price with currency exchange rate in other date, just pass a date:
59
+
60
+ ```ruby
61
+ product.price_in('EUR', Date.yesterday)
62
+ ```
63
+
64
+ You can also get total of price in other currency:
65
+
66
+ ```ruby
67
+ Product.sum_price('EUR', Date.today)
68
+ ```
69
+
70
+ ## Configuration
71
+
72
+ Add an initializer e.g config/initializers/multi_currency.rb
73
+
74
+ ```ruby
75
+ MultiCurrency.configure do |config|
76
+ config.default_currency = 'eur'
77
+ end
78
+ ```
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it ( http://github.com/xsoulsyndicate/multi_currency/fork )
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ module MultiCurrency
2
+ module Converter
3
+ module GrandTrunk
4
+
5
+ def self.get_rate_and_cache(source_currency, to_currency, date)
6
+ if source_currency.downcase == to_currency.downcase
7
+ rate = 1.0
8
+ else
9
+ exchange_rate = ExchangeRate.find_by_from_code_and_to_code_and_date(source_currency.downcase, to_currency.downcase, date)
10
+ rate = if exchange_rate.present?
11
+ exchange_rate.rate
12
+ else
13
+ response = Net::HTTP.get_response(URI("http://currencies.apps.grandtrunk.net/getrate/#{(date - 1).strftime("%Y-%m-%d")}/#{source_currency.downcase}/#{to_currency.downcase}"))
14
+ if response.is_a? Net::HTTPOK
15
+ ExchangeRate.create!(from_code: source_currency, to_code: to_currency, date: date, rate: response.body.to_f)
16
+ response.body.to_f
17
+ else
18
+ raise "#{response.code}: #{response.body}"
19
+ end
20
+ end
21
+ end
22
+ rate
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,42 @@
1
+ module MultiCurrency
2
+ class Configuration
3
+ attr_accessor :default_currency, :default_converter
4
+
5
+ def initialize
6
+ @default_currency = 'usd'
7
+ @default_converter = 'GrandTrunk'
8
+ end
9
+
10
+ def default_converter=(converter)
11
+ if ['GrandTrunk'].include? converter
12
+ @default_converter = converter
13
+ else
14
+ raise "Undefined converter"
15
+ end
16
+ end
17
+
18
+ def default_converter
19
+ "MultiCurrency::Converter::#{@default_converter}".safe_constantize
20
+ end
21
+ end
22
+
23
+ class << self
24
+ attr_accessor :configuration
25
+ end
26
+
27
+ # Configure MultiCurrency someplace sensible,
28
+ # like config/initializers/multi_currency.rb
29
+ #
30
+ # @example
31
+ # MultiCurrency.configure do |config|
32
+ # config.default_currency = 'usd'
33
+ # end
34
+
35
+ def self.configuration
36
+ @configuration ||= Configuration.new
37
+ end
38
+
39
+ def self.configure
40
+ yield(configuration)
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module MultiCurrency
2
+ VERSION = "0.1.3"
3
+ end
@@ -0,0 +1,71 @@
1
+ require "multi_currency/version"
2
+ require "multi_currency/configuration"
3
+ require "converter/grand_trunk"
4
+
5
+ module MultiCurrency
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def multi_currency_columns
10
+ @@multi_currency_columns ||= []
11
+ end
12
+
13
+ def multi_currency_columns=(columns)
14
+ if columns.is_a? Array
15
+ @@multi_currency_columns = columns
16
+ else
17
+ raise "Multi currency columns should be an array"
18
+ end
19
+ end
20
+
21
+ def multi_currency_for(columns)
22
+ multi_currency_columns = columns
23
+ multi_currency_columns.each do |column|
24
+ define_singleton_method "sum_#{column}" do |currency, date = nil|
25
+ formatted_date = date.nil? ? "#{column}_rate_date" : "'#{date.strftime("%Y-%m-%d")}'"
26
+ self.where(
27
+ "#{column}_currency IS NOT NULL AND #{column} IS NOT NULL").
28
+ sum("
29
+ CASE #{column}_currency
30
+ WHEN '#{currency.downcase}' THEN #{column}
31
+ ELSE #{column} * (SELECT exchange_rates.rate FROM exchange_rates WHERE (exchange_rates.from_code = #{column}_currency AND to_code = '#{currency.downcase}' AND date = #{formatted_date}) )
32
+ END").to_f
33
+ end
34
+
35
+ define_method "#{column}_in" do |currency_code, date = nil|
36
+ if self.send("#{column}_source_amount").present? && self.send("#{column}_source_currency").present?
37
+ date = self.send("#{column}_rate_date") if date.nil?
38
+ default_currency = self.send("#{column}_currency") rescue MultiCurrency.configuration.default_currency
39
+ rate = MultiCurrency.configuration.default_converter.get_rate_and_cache(default_currency, currency_code, date)
40
+ return self.send(column) * rate
41
+ else
42
+ return nil
43
+ end
44
+ end
45
+ end
46
+
47
+ define_method "do_currency_exchange" do
48
+ multi_currency_columns.each do |column|
49
+ if self.send("#{column}_source_amount").present? && self.send("#{column}_source_currency").present?
50
+ eval("self.#{column}_currency = '#{MultiCurrency.configuration.default_currency.downcase}'")
51
+ date = self.send("#{column}_rate_date") || Date.today
52
+ eval("self.#{column}_rate_date = date")
53
+ rate = MultiCurrency.configuration.default_converter.
54
+ get_rate_and_cache(self.send("#{column}_source_currency"), self.send("#{column}_currency"), date)
55
+ eval("self.#{column} = self.#{column}_source_amount * rate")
56
+ else
57
+ eval("self.#{column}_source_amount = nil")
58
+ eval("self.#{column}_source_currency = nil")
59
+ eval("self.#{column}_rate_date = nil")
60
+ eval("self.#{column}_currency = nil")
61
+ eval("self.#{column} = nil")
62
+ end
63
+ end
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+ end
70
+
71
+ ActiveRecord::Base.send(:include, MultiCurrency)
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multi_currency/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "multi_currency"
8
+ spec.version = MultiCurrency::VERSION
9
+ spec.authors = ["Rijaludin Muhsin"]
10
+ spec.email = ["rijaludinmuhsin@gmail.com"]
11
+ spec.summary = "Library for multi currency support"
12
+ spec.description = "Provide various functions to support multi currency in activerecord object."
13
+ spec.homepage = "https://github.com/xsoulsyndicate/multi_currency"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_currency
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Rijaludin Muhsin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Provide various functions to support multi currency in activerecord object.
42
+ email:
43
+ - rijaludinmuhsin@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/converter/grand_trunk.rb
54
+ - lib/multi_currency.rb
55
+ - lib/multi_currency/configuration.rb
56
+ - lib/multi_currency/version.rb
57
+ - multi_currency.gemspec
58
+ homepage: https://github.com/xsoulsyndicate/multi_currency
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.1
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Library for multi currency support
82
+ test_files: []