Class: Wowecon::Currency
- Inherits:
-
Object
- Object
- Wowecon::Currency
- Defined in:
- lib/wowecon/currency.rb
Instance Attribute Summary collapse
-
#copper ⇒ Object
Returns the value of attribute copper.
-
#gold ⇒ Object
Returns the value of attribute gold.
-
#silver ⇒ Object
Returns the value of attribute silver.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(value) ⇒ Currency
constructor
A new instance of Currency.
- #to_f ⇒ Object
- #to_hash ⇒ Object
- #to_html ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(value) ⇒ Currency
Returns a new instance of Currency.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/wowecon/currency.rb', line 6 def initialize(value) if value.class == Float f = sprintf("%.4f", value).split(".") self.gold = f[0].to_i self.silver = f[1][0,2].to_i self.copper = f[1][2,2].to_i elsif value.class == Hash self.gold = value[:gold] self.silver = value[:silver] self.copper = value[:copper] end end |
Instance Attribute Details
#copper ⇒ Object
Returns the value of attribute copper.
4 5 6 |
# File 'lib/wowecon/currency.rb', line 4 def copper @copper end |
#gold ⇒ Object
Returns the value of attribute gold.
4 5 6 |
# File 'lib/wowecon/currency.rb', line 4 def gold @gold end |
#silver ⇒ Object
Returns the value of attribute silver.
4 5 6 |
# File 'lib/wowecon/currency.rb', line 4 def silver @silver end |
Class Method Details
.from_hash(denoms) ⇒ Object
19 20 21 |
# File 'lib/wowecon/currency.rb', line 19 def self.from_hash(denoms) (denoms[:gold] + (denoms[:silver].to_f / 100) + (denoms[:copper].to_f / 10000)).to_f end |
Instance Method Details
#to_f ⇒ Object
27 28 29 |
# File 'lib/wowecon/currency.rb', line 27 def to_f (@gold + (@silver.to_f / 100) + (@copper.to_f / 10000)).to_f end |
#to_hash ⇒ Object
23 24 25 |
# File 'lib/wowecon/currency.rb', line 23 def to_hash {:gold => @gold, :silver => @silver, :copper => @copper} end |
#to_html ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/wowecon/currency.rb', line 41 def to_html = "" self.to_hash.each do |denom, value| if value > 0 || .length > 0 += "<var class=\"#{denom.to_s}\">#{value.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")}</var>" end end end |
#to_s ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/wowecon/currency.rb', line 31 def to_s output = "" self.to_hash.each do |denom, value| if value > 0 || output.length > 0 output += "#{value.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")}#{denom.to_s[0,1]} " end end output.strip end |