Class: Trader::Currency
- Inherits:
-
Object
- Object
- Trader::Currency
- Defined in:
- lib/trade-o-matic/structs/currency.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
Class Method Summary collapse
- .converter_for(_from, _to) ⇒ Object
- .converter_for!(_from, _to) ⇒ Object
- .for_code(_code) ⇒ Object
- .isolate_conversions ⇒ Object
- .register_conversion(_from, _to, _converter = nil, _options = {}, &_block) ⇒ Object
- .register_resolution(*_chain) ⇒ Object
- .reset_converters ⇒ Object
Instance Method Summary collapse
- #compatible_with?(_currency) ⇒ Boolean
- #convert(_value, _to) ⇒ Object
- #convertible_to?(_currency) ⇒ Boolean
-
#initialize(_code) ⇒ Currency
constructor
A new instance of Currency.
- #pack(_value) ⇒ Object
- #to_s ⇒ Object
- #to_sym ⇒ Object
- #unpack(_value, _options = {}) ⇒ Object
Constructor Details
#initialize(_code) ⇒ Currency
Returns a new instance of Currency.
68 69 70 |
# File 'lib/trade-o-matic/structs/currency.rb', line 68 def initialize(_code) @code = _code end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
66 67 68 |
# File 'lib/trade-o-matic/structs/currency.rb', line 66 def code @code end |
Class Method Details
.converter_for(_from, _to) ⇒ Object
40 41 42 |
# File 'lib/trade-o-matic/structs/currency.rb', line 40 def self.converter_for(_from, _to) converter_for!(_from, _to) rescue nil end |
.converter_for!(_from, _to) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/trade-o-matic/structs/currency.rb', line 44 def self.converter_for!(_from, _to) _from = _from.code if _from.is_a? Currency _to = _to.code if _to.is_a? Currency cv = converters[_from][_to] || comp_cache[_from][_to] if cv.nil? chains = [] resolve_converter(_from, _to, [_from], chains) raise ArgumentError, "No converter registered for #{_from}/#{_to}" if chains.count == 0 raise ArgumentError, "Ambiguous conversion path for #{_from}/#{_to}" if chains.count > 1 comp_cache[_from][_to] = cv = build_compound_converter chains.first end cv end |
.for_code(_code) ⇒ Object
61 62 63 64 |
# File 'lib/trade-o-matic/structs/currency.rb', line 61 def self.for_code(_code) return _code if _code.is_a? Currency registry[_code.to_sym] end |
.isolate_conversions ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/trade-o-matic/structs/currency.rb', line 26 def self.isolate_conversions old_converters = @@converters old_cache = @@comp_cache begin @@converters = clone_converter_hash @@converters @@comp_cache = clone_converter_hash @@comp_cache yield ensure @@converters = old_converters @@comp_cache = old_cache end end |
.register_conversion(_from, _to, _converter = nil, _options = {}, &_block) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/trade-o-matic/structs/currency.rb', line 3 def self.register_conversion(_from, _to, _converter=nil, ={}, &_block) if !_block.nil? = _converter || {} _converter = SyncConverter.new(.fetch(:interval, 60.0), &_block) elsif _converter.is_a? Numeric _converter = FixedConverter.new(_converter) end _converter = inverse(_converter) if [:inverse] converters[_from.to_sym][_to.to_sym] = _converter reset_comp_cache end |
.register_resolution(*_chain) ⇒ Object
17 18 19 |
# File 'lib/trade-o-matic/structs/currency.rb', line 17 def self.register_resolution(*_chain) converters[_chain.first][_chain.last] = build_compound_converter _chain end |
.reset_converters ⇒ Object
21 22 23 24 |
# File 'lib/trade-o-matic/structs/currency.rb', line 21 def self.reset_converters converters.clear reset_comp_cache end |
Instance Method Details
#compatible_with?(_currency) ⇒ Boolean
78 79 80 81 |
# File 'lib/trade-o-matic/structs/currency.rb', line 78 def compatible_with?(_currency) _currency = klass.for_code _currency convertible_to? _currency and _currency.convertible_to? self end |
#convert(_value, _to) ⇒ Object
83 84 85 86 |
# File 'lib/trade-o-matic/structs/currency.rb', line 83 def convert(_value, _to) converter = klass.converter_for!(code, _to.to_sym) converter.apply _value end |
#convertible_to?(_currency) ⇒ Boolean
72 73 74 75 76 |
# File 'lib/trade-o-matic/structs/currency.rb', line 72 def convertible_to?(_currency) _currency = klass.for_code _currency return true if self == _currency !klass.converter_for(code, _currency.to_sym).nil? end |
#pack(_value) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/trade-o-matic/structs/currency.rb', line 88 def pack(_value) if _value.is_a? Price raise StandardError, "Invalid value currency" if _value.currency != self return _value end Price.new self, _value end |
#to_s ⇒ Object
115 116 117 |
# File 'lib/trade-o-matic/structs/currency.rb', line 115 def to_s code.to_s end |
#to_sym ⇒ Object
119 120 121 |
# File 'lib/trade-o-matic/structs/currency.rb', line 119 def to_sym code end |
#unpack(_value, _options = {}) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/trade-o-matic/structs/currency.rb', line 97 def unpack(_value, ={}) = { default: } unless .is_a? Hash if _value.is_a? Price if _value.currency != self _value.currency.convert _value.amount, self else _value.amount end elsif _value.nil? Standard.amount [:default] elsif .fetch(:strict, true) raise ArgumentError, "Must provide a currency bound price" else Standard.amount _value end end |