Class: Cardia::Money
Overview
A very basic Money class, at the time being works with Norwegian Kroner
Instance Attribute Summary collapse
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
Class Method Summary collapse
-
.kroner(an_amount) ⇒ Object
Create an instance for NOK.
Instance Method Summary collapse
-
#+(a_money) ⇒ Object
:nodoc:.
-
#==(an_object) ⇒ Object
Equality test, returns true for two objects of the same amount.
-
#format_kroner_dot_oere ⇒ Object
Formatted print.
-
#including_tax(tax_percentage) ⇒ Object
Return a new instance including tax.
-
#initialize(an_amount) ⇒ Money
constructor
:nodoc:.
-
#kroner ⇒ Object
Return the object as Norwegian Kroner.
-
#nil? ⇒ Boolean
:nodoc:.
-
#oere ⇒ Object
Return the øre.
-
#plus(a_money) ⇒ Object
:nodoc:.
-
#times(factor_integer) ⇒ Object
:nodoc:.
-
#to_i ⇒ Object
:nodoc:.
-
#to_s ⇒ Object
String representation.
Constructor Details
#initialize(an_amount) ⇒ Money
:nodoc:
12 13 14 |
# File 'lib/cardia/money.rb', line 12 def initialize(an_amount) #:nodoc: @amount = an_amount end |
Instance Attribute Details
#amount ⇒ Object (readonly)
Returns the value of attribute amount.
4 5 6 |
# File 'lib/cardia/money.rb', line 4 def amount @amount end |
Class Method Details
.kroner(an_amount) ⇒ Object
Create an instance for NOK
7 8 9 10 |
# File 'lib/cardia/money.rb', line 7 def self.kroner(an_amount) an_amount = an_amount.to_i self.new(an_amount * 100) end |
Instance Method Details
#+(a_money) ⇒ Object
:nodoc:
24 25 26 |
# File 'lib/cardia/money.rb', line 24 def +(a_money) #:nodoc: self.plus(a_money) end |
#==(an_object) ⇒ Object
Equality test, returns true for two objects of the same amount
48 49 50 |
# File 'lib/cardia/money.rb', line 48 def ==(an_object) self.class == an_object.class and self.amount == an_object.amount end |
#format_kroner_dot_oere ⇒ Object
Formatted print
62 63 64 |
# File 'lib/cardia/money.rb', line 62 def format_kroner_dot_oere return "%d.%.2d" % [self.kroner, self.oere] end |
#including_tax(tax_percentage) ⇒ Object
Return a new instance including tax
43 44 45 |
# File 'lib/cardia/money.rb', line 43 def including_tax(tax_percentage) self.class.new((self.amount + self.amount * (tax_percentage / 100.0)).round) end |
#kroner ⇒ Object
Return the object as Norwegian Kroner
29 30 31 |
# File 'lib/cardia/money.rb', line 29 def kroner (@amount / 100).truncate end |
#nil? ⇒ Boolean
:nodoc:
38 39 40 |
# File 'lib/cardia/money.rb', line 38 def nil? #:nodoc: @amount.nil? end |
#oere ⇒ Object
Return the øre
34 35 36 |
# File 'lib/cardia/money.rb', line 34 def oere @amount - kroner * 100 end |
#plus(a_money) ⇒ Object
:nodoc:
20 21 22 |
# File 'lib/cardia/money.rb', line 20 def plus(a_money) #:nodoc: self.class.new(a_money. amount + @amount) end |
#times(factor_integer) ⇒ Object
:nodoc:
16 17 18 |
# File 'lib/cardia/money.rb', line 16 def times(factor_integer) #:nodoc: self.class.new(factor_integer * @amount) end |
#to_i ⇒ Object
:nodoc:
57 58 59 |
# File 'lib/cardia/money.rb', line 57 def to_i #:nodoc: (@amount/100.0).round end |
#to_s ⇒ Object
String representation
53 54 55 |
# File 'lib/cardia/money.rb', line 53 def to_s "#{self.to_i},-" end |