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) ⇒ 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

#creditsObject (readonly)

Returns the value of attribute credits.



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

def credits
  @credits
end

#valid_fromObject

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

.fiveObject



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

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

.tenObject



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

Returns:

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



28
29
30
# File 'lib/oddb/util/money.rb', line 28

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

#to_sObject



31
32
33
# File 'lib/oddb/util/money.rb', line 31

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