cute_exchange 1.0.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.
- checksums.yaml +7 -0
- data/lib/cute_exchange.rb +11 -0
- data/lib/cute_exchange/money.rb +68 -0
- metadata +67 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: ca480fd77083a5de5b65460286316b121320c189
|
|
4
|
+
data.tar.gz: 6f566e60cf7e206cd1ae89112dcaa35afffec62f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 83128c3d37485c264284779c02848347db9903d3bf2e797f27e2a038e3e70c6eec9a39df627672caeda03b4e0b15d9a7075aaa738e3b143bc58fbde529d2986e
|
|
7
|
+
data.tar.gz: 1e492db1c2640463ee245ff6790cfc7f42cc4f4b3387fac2dff197418a9a46a5448acd95af291dc82a879ceac7f4b9d8179d6dc24c246382a0b33dd651356884
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Dir["#{File.dirname(__FILE__)}/cute_exchange/**/*.rb"].each {|f| require f}
|
|
2
|
+
|
|
3
|
+
module CuteExchange
|
|
4
|
+
def self.create_money(amount, currency)
|
|
5
|
+
Money.new(amount, currency)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def self.conversion_rates(base, rates = {})
|
|
9
|
+
Money.conversion_rates(base, rates)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require 'bigdecimal'
|
|
2
|
+
|
|
3
|
+
class Money
|
|
4
|
+
include Comparable
|
|
5
|
+
|
|
6
|
+
class << self
|
|
7
|
+
attr_reader :base, :rates
|
|
8
|
+
|
|
9
|
+
def conversion_rates(base, rates = {})
|
|
10
|
+
@base = base
|
|
11
|
+
@rates = rates
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
attr_reader :amount, :currency
|
|
16
|
+
|
|
17
|
+
def initialize(amount, currency)
|
|
18
|
+
@amount = amount
|
|
19
|
+
@currency = currency
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def convert_to(target)
|
|
23
|
+
return self if target == currency
|
|
24
|
+
result = target == Money.base ? convert_to_base : convert_to_base * rate_by(target)
|
|
25
|
+
self.class.new(result.to_s('F'), target)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def inspect(options={})
|
|
29
|
+
precision = options[:precision] || 2
|
|
30
|
+
"#{"%.#{precision}f" % amount} #{currency}"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def + arg
|
|
34
|
+
perform_arithmetic(__method__, arg)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def - arg
|
|
38
|
+
perform_arithmetic(__method__, arg)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def / arg
|
|
42
|
+
perform_arithmetic(__method__, arg)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def * arg
|
|
46
|
+
perform_arithmetic(__method__, arg)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def <=>(arg)
|
|
50
|
+
BigDecimal(amount.to_s) <=> BigDecimal(arg.convert_to(currency).amount.to_s)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
def convert_to_base
|
|
55
|
+
return BigDecimal(amount.to_s) if currency == Money.base
|
|
56
|
+
BigDecimal(amount.to_s) / Money.rates.fetch(currency)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def rate_by(currency)
|
|
60
|
+
Money.rates.fetch(currency)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def perform_arithmetic(operation, arg)
|
|
64
|
+
converted_amount = arg.is_a?(Numeric) ? arg : arg.convert_to(currency).amount
|
|
65
|
+
new_amount = BigDecimal(amount.to_s).send(operation, BigDecimal(converted_amount.to_s))
|
|
66
|
+
self.class.new(new_amount.to_s('F'), currency)
|
|
67
|
+
end
|
|
68
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: cute_exchange
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Timo Moss
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-01-20 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bigdecimal
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.2'
|
|
20
|
+
- - ">="
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: 1.2.7
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - "~>"
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '1.2'
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 1.2.7
|
|
33
|
+
description: implement the money conversion in your Ruby project
|
|
34
|
+
email: rails.moss@gmail.com
|
|
35
|
+
executables: []
|
|
36
|
+
extensions: []
|
|
37
|
+
extra_rdoc_files: []
|
|
38
|
+
files:
|
|
39
|
+
- lib/cute_exchange.rb
|
|
40
|
+
- lib/cute_exchange/money.rb
|
|
41
|
+
homepage: http://rubygems.org/gem/cute_exchange
|
|
42
|
+
licenses:
|
|
43
|
+
- MIT
|
|
44
|
+
metadata: {}
|
|
45
|
+
post_install_message:
|
|
46
|
+
rdoc_options: []
|
|
47
|
+
require_paths:
|
|
48
|
+
- lib
|
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: '0'
|
|
59
|
+
requirements:
|
|
60
|
+
- Good mood
|
|
61
|
+
- A little courage
|
|
62
|
+
rubyforge_project:
|
|
63
|
+
rubygems_version: 2.6.14
|
|
64
|
+
signing_key:
|
|
65
|
+
specification_version: 4
|
|
66
|
+
summary: Gem for the money conversion
|
|
67
|
+
test_files: []
|