Class: Gera::CurrencyPair
- Inherits:
-
Object
- Object
- Gera::CurrencyPair
- Defined in:
- lib/gera/currency_pair.rb
Overview
Валютная пара
Class Method Summary collapse
Instance Method Summary collapse
- #change_from(cur) ⇒ Object
- #change_to(cur) ⇒ Object
- #cur_from=(value) ⇒ Object
- #cur_to=(value) ⇒ Object
-
#initialize(*args) ⇒ CurrencyPair
constructor
Варианты:.
- #inspect ⇒ Object
- #inverse ⇒ Object
-
#key ⇒ Object
Для машин.
- #same? ⇒ Boolean
- #to_a ⇒ Object
-
#to_s ⇒ Object
Для людей.
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
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 |
#inspect ⇒ Object
43 44 45 |
# File 'lib/gera/currency_pair.rb', line 43 def inspect to_s end |
#inverse ⇒ Object
47 48 49 |
# File 'lib/gera/currency_pair.rb', line 47 def inverse self.class.new cur_to, cur_from end |
#key ⇒ Object
Для машин
89 90 91 |
# File 'lib/gera/currency_pair.rb', line 89 def key join '_' end |
#same? ⇒ Boolean
75 76 77 |
# File 'lib/gera/currency_pair.rb', line 75 def same? cur_from == cur_to end |
#to_a ⇒ Object
79 80 81 |
# File 'lib/gera/currency_pair.rb', line 79 def to_a [cur_from, cur_to] end |
#to_s ⇒ Object
Для людей
84 85 86 |
# File 'lib/gera/currency_pair.rb', line 84 def to_s join '/' end |