Class: ODDB::Util::Money

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/oddb/util/money.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#countryObject

Returns the value of attribute country.



8
9
10
# File 'lib/oddb/util/money.rb', line 8

def country
  @country
end

#creditsObject (readonly)

Returns the value of attribute credits.



7
8
9
# File 'lib/oddb/util/money.rb', line 7

def credits
  @credits
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/oddb/util/money.rb', line 8

def type
  @type
end

Class Method Details

.fiveObject



11
12
13
# File 'lib/oddb/util/money.rb', line 11

def five
  @five ||= self.new(5)
end

.tenObject



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

Returns:

  • (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_fObject



33
34
35
# File 'lib/oddb/util/money.rb', line 33

def to_f
  @amount || (@credits.to_f / 100)
end

#to_sObject



36
37
38
# File 'lib/oddb/util/money.rb', line 36

def to_s
  sprintf("%1.2f", to_f)
end