Class: MagicSheet::Cost

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, currency) ⇒ Cost

Returns a new instance of Cost.



45
46
47
48
# File 'lib/magicsheet.rb', line 45

def initialize(amount,currency)
  @amount = amount
  @currency = currency
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



32
33
34
# File 'lib/magicsheet.rb', line 32

def amount
  @amount
end

#currencyObject (readonly)

Returns the value of attribute currency.



32
33
34
# File 'lib/magicsheet.rb', line 32

def currency
  @currency
end

Class Method Details

.[](arg) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/magicsheet.rb', line 34

def self.[](arg)
  arg = arg.to_s.strip
  currency = Currency[arg]
  if currency
    amount = (arg[/\d+(\.\d+)?/,0] || 0).to_f
    return self.new(amount,currency)
  else
    return nil
  end
end

Instance Method Details

#to_sObject



50
51
52
# File 'lib/magicsheet.rb', line 50

def to_s
  "#{"%.2f" % @amount} #{@currency}"
end