Class: DolarHoy::Currency
- Inherits:
-
Object
- Object
- DolarHoy::Currency
- Includes:
- Comparable
- Defined in:
- lib/dolarhoy/currency.rb
Instance Attribute Summary collapse
-
#buy ⇒ Object
Returns the value of attribute buy.
-
#name ⇒ Object
Returns the value of attribute name.
-
#sell ⇒ Object
Returns the value of attribute sell.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #alias ⇒ Object
- #aliased? ⇒ Boolean
- #aliases ⇒ Object
- #average ⇒ Object
- #format(number) ⇒ Object
-
#initialize(name, buy, sell) ⇒ Currency
constructor
A new instance of Currency.
- #parse(text) ⇒ Object
- #reverse_aliases ⇒ Object
- #sanitize_name(name) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, buy, sell) ⇒ Currency
Returns a new instance of Currency.
10 11 12 13 14 |
# File 'lib/dolarhoy/currency.rb', line 10 def initialize(name, buy, sell) @name = sanitize_name(name) @buy = parse(buy) @sell = parse(sell) end |
Instance Attribute Details
#buy ⇒ Object
Returns the value of attribute buy.
8 9 10 |
# File 'lib/dolarhoy/currency.rb', line 8 def buy @buy end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/dolarhoy/currency.rb', line 8 def name @name end |
#sell ⇒ Object
Returns the value of attribute sell.
8 9 10 |
# File 'lib/dolarhoy/currency.rb', line 8 def sell @sell end |
Instance Method Details
#<=>(other) ⇒ Object
16 17 18 |
# File 'lib/dolarhoy/currency.rb', line 16 def <=>(other) self.alias <=> other.alias end |
#alias ⇒ Object
59 60 61 |
# File 'lib/dolarhoy/currency.rb', line 59 def alias reverse_aliases[name] end |
#aliased? ⇒ Boolean
55 56 57 |
# File 'lib/dolarhoy/currency.rb', line 55 def aliased? !! self.alias end |
#aliases ⇒ Object
44 45 46 |
# File 'lib/dolarhoy/currency.rb', line 44 def aliases @@aliases ||= YAML.load_file(File.(File.join(File.dirname(__FILE__), '..', 'aliases.yml'))) end |
#average ⇒ Object
63 64 65 |
# File 'lib/dolarhoy/currency.rb', line 63 def average (buy + sell) / 2.0 end |
#format(number) ⇒ Object
24 25 26 |
# File 'lib/dolarhoy/currency.rb', line 24 def format(number) "%.3f" % number end |
#parse(text) ⇒ Object
20 21 22 |
# File 'lib/dolarhoy/currency.rb', line 20 def parse(text) text[/([\d\.,]+)/, 1].to_f end |
#reverse_aliases ⇒ Object
48 49 50 51 52 53 |
# File 'lib/dolarhoy/currency.rb', line 48 def reverse_aliases @@reverse_aliases ||= aliases.inject({}) do |reverse, (code, names)| [*names].each { |name| reverse[name] = code } reverse end end |
#sanitize_name(name) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dolarhoy/currency.rb', line 28 def sanitize_name(name) name = name.dup name.gsub!('Á', 'A') name.gsub!('É', 'E') name.gsub!('Í', 'I') name.gsub!('Ó', 'O') name.gsub!('Ú', 'U') name.gsub!("\r", " ") name.gsub!("\n", " ") name[/^([\w\s\.\$]+)/, 1].strip.capitalize.squeeze(' ') end |
#to_s ⇒ Object
40 41 42 |
# File 'lib/dolarhoy/currency.rb', line 40 def to_s "#{self.alias} #{format buy} #{format sell} #{format average}" end |