Class: Trader::Currency

Inherits:
Object
  • Object
show all
Defined in:
lib/trade-o-matic/structs/currency.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_code) ⇒ Currency

Returns a new instance of Currency.



65
66
67
# File 'lib/trade-o-matic/structs/currency.rb', line 65

def initialize(_code)
  @code = _code
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



63
64
65
# File 'lib/trade-o-matic/structs/currency.rb', line 63

def code
  @code
end

Class Method Details

.converter_for(_from, _to) ⇒ Object



37
38
39
# File 'lib/trade-o-matic/structs/currency.rb', line 37

def self.converter_for(_from, _to)
  converter_for!(_from, _to) rescue nil
end

.converter_for!(_from, _to) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/trade-o-matic/structs/currency.rb', line 41

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



58
59
60
61
# File 'lib/trade-o-matic/structs/currency.rb', line 58

def self.for_code(_code)
  return _code if _code.is_a? Currency
  registry[_code.to_sym]
end

.isolate_conversionsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/trade-o-matic/structs/currency.rb', line 23

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, &_block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/trade-o-matic/structs/currency.rb', line 3

def self.register_conversion(_from, _to, _converter=nil, &_block)
  if !_block.nil?
    _converter = SyncConverter.new(60, &_block)
  elsif _converter.is_a? Integer or _converter.is_a? Float
    _converter = FixedConverter.new(_converter)
  end

  converters[_from.to_sym][_to.to_sym] = _converter
  reset_comp_cache
end

.register_resolution(*_chain) ⇒ Object



14
15
16
# File 'lib/trade-o-matic/structs/currency.rb', line 14

def self.register_resolution(*_chain)
  converters[_chain.first][_chain.last] = build_compound_converter _chain
end

.reset_convertersObject



18
19
20
21
# File 'lib/trade-o-matic/structs/currency.rb', line 18

def self.reset_converters
  converters.clear
  reset_comp_cache
end

Instance Method Details

#compatible_with?(_currency) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/trade-o-matic/structs/currency.rb', line 75

def compatible_with?(_currency)
  _currency = klass.for_code _currency
  convertible_to? _currency and _currency.convertible_to? self
end

#convert(_value, _to) ⇒ Object



80
81
82
# File 'lib/trade-o-matic/structs/currency.rb', line 80

def convert(_value, _to)
  klass.converter_for!(code, _to.to_sym).rate * _value
end

#convertible_to?(_currency) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/trade-o-matic/structs/currency.rb', line 69

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



84
85
86
87
88
89
90
91
# File 'lib/trade-o-matic/structs/currency.rb', line 84

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_sObject



111
112
113
# File 'lib/trade-o-matic/structs/currency.rb', line 111

def to_s
  code.to_s
end

#to_symObject



115
116
117
# File 'lib/trade-o-matic/structs/currency.rb', line 115

def to_sym
  code
end

#unpack(_value, _options = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/trade-o-matic/structs/currency.rb', line 93

def unpack(_value, _options={})
  _options = { default: _options } unless _options.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?
    _options[:default]
  elsif _options.fetch(:strict, true)
    raise ArgumentError, "Must provide a currency bound price"
  else
    _value
  end
end