Class: Cashish::Amount

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper, Comparable
Defined in:
lib/cashish/amount.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(integer_value, currency_code) ⇒ Amount

Returns a new instance of Amount.



17
18
19
20
21
# File 'lib/cashish/amount.rb', line 17

def initialize(integer_value, currency_code)
  @integer_value = integer_value
  @currency_code = currency_code
  @currency_data = Currency[currency_code]
end

Instance Attribute Details

#currency_codeObject (readonly)

Returns the value of attribute currency_code.



22
23
24
# File 'lib/cashish/amount.rb', line 22

def currency_code
  @currency_code
end

#currency_dataObject (readonly)

Returns the value of attribute currency_data.



22
23
24
# File 'lib/cashish/amount.rb', line 22

def currency_data
  @currency_data
end

#integer_valueObject (readonly)

Returns the value of attribute integer_value.



22
23
24
# File 'lib/cashish/amount.rb', line 22

def integer_value
  @integer_value
end

Instance Method Details

#*(value) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cashish/amount.rb', line 50

def *(value)
  if value.is_a?(Numeric)
    if @integer_value
      Cashish::Amount.new(@integer_value*value, @currency_code)
    else
      self
    end
  else
    raise "you can only multiply a currency by a numeric"
  end
end

#+(other) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/cashish/amount.rb', line 34

def +(other)
  if @currency_code == other.currency_code
    Cashish::Amount.new(@integer_value.to_f + other.integer_value.to_f, @currency_code)
  else
    raise "you can't add different currencies"
  end
end

#-(other) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/cashish/amount.rb', line 42

def -(other)
  if @currency_code == other.currency_code
    Cashish::Amount.new(@integer_value.to_f - other.integer_value.to_f, @currency_code)
  else
    raise "you can't subtract different currencies"
  end
end

#/(value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cashish/amount.rb', line 62

def /(value)
  if value.is_a?(Numeric)
    if @integer_value
      Cashish::Amount.new(@integer_value/value, @currency_code)
    else
      self
    end
  elsif value.is_a?(Cashish::Amount)
    ratio(value)
  else
    raise "you can only divide a currency by a numeric or another currency"
  end
end

#<=>(other) ⇒ Object



28
29
30
31
32
# File 'lib/cashish/amount.rb', line 28

def <=>(other)
  if self.currency_code == other.currency_code
    self.integer_value <=> other.integer_value
  end
end

#decimal_placesObject



76
77
78
# File 'lib/cashish/amount.rb', line 76

def decimal_places
  currency_data["e"]
end

#decimal_valueObject



80
81
82
83
84
# File 'lib/cashish/amount.rb', line 80

def decimal_value
  if integer_value
    @decimal_value ||= integer_value * BigDecimal.new("1E-#{decimal_places}")
  end
end

#formatted_value(opts = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cashish/amount.rb', line 114

def formatted_value(opts={})
  this_value = decimal_value
  if opts.delete(:default_to_zero)
    this_value ||= 0
  end

  if this_value
    precision = opts[:precision] || self.decimal_places
    with_precision = "%01.#{precision}f" % this_value
    number_with_delimiter(with_precision, {:separator => '.', :delimiter => ','}.merge(opts))
  end
end

#inspectObject



24
25
26
# File 'lib/cashish/amount.rb', line 24

def inspect
  "#<Cashish::Amount #{self.to_s}>"
end

#to_fObject

the underlying value explicitly as a float



110
111
112
# File 'lib/cashish/amount.rb', line 110

def to_f
  self.integer_value.to_f
end

#to_iObject

the underlying value explicitly as an integer



105
106
107
# File 'lib/cashish/amount.rb', line 105

def to_i
  self.integer_value.to_i
end

#to_s(*args) ⇒ Object

use XE.com’s format eg. 14,399.43 USD



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cashish/amount.rb', line 88

def to_s(*args)
  opts = args.extract_options!
  format = args.first || :full
  
  case format
    when :full   # USD 14,399.43
      "#{formatted_value(opts) || "-"} #{currency_code}"
    when :hide_currency   # 14,399.43
      "#{formatted_value(opts) || "-"}"
    when :simple # 14399.43
      formatted_value(opts.merge(:delimiter => "", :default_to_zero => true))
    else
      raise "bad format"
  end
end