Class: LightCurrencyConverter::CurrencyCollection
- Inherits:
-
Object
- Object
- LightCurrencyConverter::CurrencyCollection
- Includes:
- Enumerable
- Defined in:
- lib/light_currency_convert/currency_collection.rb
Instance Attribute Summary collapse
-
#currencies ⇒ Object
Returns the value of attribute currencies.
-
#graph ⇒ Object
Returns the value of attribute graph.
Instance Method Summary collapse
- #[](*args) ⇒ Object
- #base_find(currency_one, currency_two) ⇒ Object
- #each ⇒ Object
- #find_currency_course(from, to) ⇒ Object
- #graph_find(from, to) ⇒ Object
-
#initialize(json_string) ⇒ CurrencyCollection
constructor
A new instance of CurrencyCollection.
- #simple_find(from, to) ⇒ Object
Constructor Details
#initialize(json_string) ⇒ CurrencyCollection
Returns a new instance of CurrencyCollection.
8 9 10 11 12 13 |
# File 'lib/light_currency_convert/currency_collection.rb', line 8 def initialize(json_string) json_entries = JSON.parse(json_string) @currencies = json_entries.to_a.map { |e| CurrencyCourse.new(*e) } @graph = Graph.new populate_graph end |
Instance Attribute Details
#currencies ⇒ Object
Returns the value of attribute currencies.
6 7 8 |
# File 'lib/light_currency_convert/currency_collection.rb', line 6 def currencies @currencies end |
#graph ⇒ Object
Returns the value of attribute graph.
6 7 8 |
# File 'lib/light_currency_convert/currency_collection.rb', line 6 def graph @graph end |
Instance Method Details
#[](*args) ⇒ Object
19 20 21 |
# File 'lib/light_currency_convert/currency_collection.rb', line 19 def [](*args) @currencies[*args] end |
#base_find(currency_one, currency_two) ⇒ Object
27 28 29 |
# File 'lib/light_currency_convert/currency_collection.rb', line 27 def base_find(currency_one, currency_two) detect { |e| e.from.currency == currency_one && e.to.currency == currency_two } end |
#each ⇒ Object
15 16 17 |
# File 'lib/light_currency_convert/currency_collection.rb', line 15 def each @currencies.each { |e| yield(e) } end |
#find_currency_course(from, to) ⇒ Object
23 24 25 |
# File 'lib/light_currency_convert/currency_collection.rb', line 23 def find_currency_course(from, to) simple_find(from, to) || graph_find(from, to) end |
#graph_find(from, to) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/light_currency_convert/currency_collection.rb', line 35 def graph_find(from, to) graph.search_path(from, to, [], results = []) return nil if results.empty? result = graph.minimize_result(results) full_path = [result[0], *(result[1..-2].map{|x| [x, x] }.flatten), result[-1]].each_slice(2) courses = full_path.map { |x| simple_find(*x) } IndirectCurrencyCourse.new(courses, full_path) end |
#simple_find(from, to) ⇒ Object
31 32 33 |
# File 'lib/light_currency_convert/currency_collection.rb', line 31 def simple_find(from, to) base_find(from, to) || base_find(to, from) end |