Class: ODDB::Util::Money
- Inherits:
-
Object
- Object
- ODDB::Util::Money
- Includes:
- Comparable
- Defined in:
- lib/oddb/util/money.rb
Instance Attribute Summary collapse
-
#country ⇒ Object
Returns the value of attribute country.
-
#credits ⇒ Object
readonly
Returns the value of attribute credits.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #<=>(other) ⇒ Object
- #amount=(amount) ⇒ Object
-
#initialize(amount, type = nil, country = nil) ⇒ Money
constructor
A new instance of Money.
- #is_for?(type, country) ⇒ Boolean
- #to_f ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(amount, type = nil, country = nil) ⇒ Money
Returns a new instance of Money.
18 19 20 21 22 |
# File 'lib/oddb/util/money.rb', line 18 def initialize(amount, type=nil, country=nil) self.amount = amount self.type = type self.country = country end |
Instance Attribute Details
#country ⇒ Object
Returns the value of attribute country.
8 9 10 |
# File 'lib/oddb/util/money.rb', line 8 def country @country end |
#credits ⇒ Object (readonly)
Returns the value of attribute credits.
7 8 9 |
# File 'lib/oddb/util/money.rb', line 7 def credits @credits end |
#type ⇒ Object
Returns the value of attribute type.
8 9 10 |
# File 'lib/oddb/util/money.rb', line 8 def type @type end |
Class Method Details
.five ⇒ Object
11 12 13 |
# File 'lib/oddb/util/money.rb', line 11 def five @five ||= self.new(5) end |
.ten ⇒ Object
14 15 16 |
# File 'lib/oddb/util/money.rb', line 14 def ten @ten ||= self.new(10) end |
Instance Method Details
#*(other) ⇒ Object
48 49 50 |
# File 'lib/oddb/util/money.rb', line 48 def *(other) Money.new((@amount || to_f) * other.to_f) end |
#+(other) ⇒ Object
42 43 44 |
# File 'lib/oddb/util/money.rb', line 42 def +(other) Money.new((@amount || to_f) + other.to_f) end |
#-(other) ⇒ Object
45 46 47 |
# File 'lib/oddb/util/money.rb', line 45 def -(other) Money.new((@amount || to_f) - other.to_f) end |
#/(other) ⇒ Object
51 52 53 |
# File 'lib/oddb/util/money.rb', line 51 def /(other) Money.new((@amount || to_f) / other.to_f) end |
#<=>(other) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/oddb/util/money.rb', line 54 def <=>(other) case other when Money @credits <=> other.credits else to_f <=> other.to_f end end |
#amount=(amount) ⇒ Object
23 24 25 26 |
# File 'lib/oddb/util/money.rb', line 23 def amount=(amount) @amount = amount.to_f @credits = (@amount * 100).round end |
#is_for?(type, country) ⇒ Boolean
30 31 32 |
# File 'lib/oddb/util/money.rb', line 30 def is_for?(type, country) @type == type.to_s.downcase && @country == country.to_s.upcase end |
#to_f ⇒ Object
33 34 35 |
# File 'lib/oddb/util/money.rb', line 33 def to_f @amount || (@credits.to_f / 100) end |
#to_s ⇒ Object
36 37 38 |
# File 'lib/oddb/util/money.rb', line 36 def to_s sprintf("%1.2f", to_f) end |