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