Class: Cashish::Amount
- Inherits:
-
Object
- Object
- Cashish::Amount
- Includes:
- ActionView::Helpers::NumberHelper, Comparable
- Defined in:
- lib/cashish/amount.rb
Instance Attribute Summary collapse
-
#currency_code ⇒ Object
readonly
Returns the value of attribute currency_code.
-
#currency_data ⇒ Object
readonly
Returns the value of attribute currency_data.
-
#integer_value ⇒ Object
readonly
Returns the value of attribute integer_value.
Instance Method Summary collapse
- #*(value) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(value) ⇒ Object
- #<=>(other) ⇒ Object
- #decimal_places ⇒ Object
- #decimal_value ⇒ Object
- #formatted_value(opts = {}) ⇒ Object
-
#initialize(integer_value, currency_code) ⇒ Amount
constructor
A new instance of Amount.
- #inspect ⇒ Object
-
#to_f ⇒ Object
the underlying value explicitly as a float.
-
#to_i ⇒ Object
the underlying value explicitly as an integer.
-
#to_s(*args) ⇒ Object
use XE.com’s format eg.
Constructor Details
Instance Attribute Details
#currency_code ⇒ Object (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_data ⇒ Object (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_value ⇒ Object (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_places ⇒ Object
76 77 78 |
# File 'lib/cashish/amount.rb', line 76 def decimal_places currency_data["e"] end |
#decimal_value ⇒ Object
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 |
#inspect ⇒ Object
24 25 26 |
# File 'lib/cashish/amount.rb', line 24 def inspect "#<Cashish::Amount #{self.to_s}>" end |
#to_f ⇒ Object
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_i ⇒ Object
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. 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 |