Class: Gera::CurrencyPair

Inherits:
Object
  • Object
show all
Defined in:
lib/gera/currency_pair.rb

Overview

Валютная пара

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CurrencyPair

Варианты:

new cur_from: :rub, cur_to: usd new :rub, :usd new ‘rub/usd’



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gera/currency_pair.rb', line 20

def initialize(*args)
  if args.first.is_a? Hash
    super(args.first).freeze

  elsif args.count ==1
    initialize(*args.first.split(/[\/_-]/)).freeze

  elsif args.count == 2
    super(cur_from: args[0], cur_to: args[1]).freeze

  else
    raise "WTF? #{args}"
  end
end

Class Method Details

.allObject



37
38
39
40
41
# File 'lib/gera/currency_pair.rb', line 37

def self.all
  @all ||= Money::Currency.all.each_with_object([]) { |cur_from, list|
    Money::Currency.all.each { |cur_to| list << CurrencyPair.new(cur_from, cur_to) }
  }.freeze
end

Instance Method Details

#change_from(cur) ⇒ Object



71
72
73
# File 'lib/gera/currency_pair.rb', line 71

def change_from(cur)
  CurrencyPair.new cur, cur_to
end

#change_to(cur) ⇒ Object



67
68
69
# File 'lib/gera/currency_pair.rb', line 67

def change_to(cur)
  CurrencyPair.new cur_from, cur
end

#cur_from=(value) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/gera/currency_pair.rb', line 59

def cur_from=(value)
  if value.is_a? Money::Currency
    super value
  else
    super Money::Currency.find(value) || raise("Не известная валюта #{value} в cur_from")
  end
end

#cur_to=(value) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/gera/currency_pair.rb', line 51

def cur_to=(value)
  if value.is_a? Money::Currency
    super value
  else
    super Money::Currency.find(value) || raise("Не известная валюта #{value} в cur_to")
  end
end

#inspectObject



43
44
45
# File 'lib/gera/currency_pair.rb', line 43

def inspect
  to_s
end

#inverseObject



47
48
49
# File 'lib/gera/currency_pair.rb', line 47

def inverse
  self.class.new cur_to, cur_from
end

#keyObject

Для машин



89
90
91
# File 'lib/gera/currency_pair.rb', line 89

def key
  join '_'
end

#same?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/gera/currency_pair.rb', line 75

def same?
  cur_from == cur_to
end

#to_aObject



79
80
81
# File 'lib/gera/currency_pair.rb', line 79

def to_a
  [cur_from, cur_to]
end

#to_sObject

Для людей



84
85
86
# File 'lib/gera/currency_pair.rb', line 84

def to_s
  join '/'
end