Class: Wowecon::Currency

Inherits:
Object
  • Object
show all
Defined in:
lib/wowecon/currency.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#copperObject

Returns the value of attribute copper.



4
5
6
# File 'lib/wowecon/currency.rb', line 4

def copper
  @copper
end

#goldObject

Returns the value of attribute gold.



4
5
6
# File 'lib/wowecon/currency.rb', line 4

def gold
  @gold
end

#silverObject

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_fObject



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_hashObject



23
24
25
# File 'lib/wowecon/currency.rb', line 23

def to_hash
  {:gold => @gold, :silver => @silver, :copper => @copper}
end

#to_htmlObject



41
42
43
44
45
46
47
48
49
# File 'lib/wowecon/currency.rb', line 41

def to_html
  tags  = ""
  self.to_hash.each do |denom, value|
    if value > 0 || tags.length > 0
      tags += "<var class=\"#{denom.to_s}\">#{value.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")}</var>"
    end
  end
  tags
end

#to_sObject



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