Class: Money

Inherits:
Numeric show all
Extended by:
Mongoid::Extensions::Money::Conversions
Includes:
Comparable
Defined in:
lib/mongoid_money/money.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mongoid::Extensions::Money::Conversions

get, set

Methods inherited from Numeric

#cent, #dollar

Constructor Details

#initialize(cents) ⇒ Money

Returns a new instance of Money.



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

def initialize(cents)
  @cents = cents.round.to_i
end

Instance Attribute Details

#centsObject (readonly)

Returns the value of attribute cents.



5
6
7
# File 'lib/mongoid_money/money.rb', line 5

def cents
  @cents
end

Class Method Details

.new_from_cents(cents) ⇒ Object



24
25
26
# File 'lib/mongoid_money/money.rb', line 24

def self.new_from_cents(cents)
  Money.new cents.round.to_i
end

.new_from_dollars(value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mongoid_money/money.rb', line 7

def self.new_from_dollars(value)
  case value
    when Fixnum
      Money.new(value * 100)
    when BigDecimal
      Money.new((value * 100).fix)
    when Float
      Money.new((BigDecimal.new(value.to_s) * 100).fix)
    when Numeric
      Money.new((BigDecimal.new(value.to_s) * 100).fix)
    when String
      Money.new((BigDecimal.new(value.to_s) * 100).fix)
    else
      raise ArgumentError, "#{value} must be a numeric object or a string representation of a number."
  end
end

Instance Method Details

#%(val) ⇒ Object



105
106
107
# File 'lib/mongoid_money/money.rb', line 105

def %(val)
  self.modulo(val)
end

#*(value) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/mongoid_money/money.rb', line 70

def *(value)
  if value.is_a?(Money)
    raise ArgumentError, "Can't multiply a Money by a Money"
  else
    Money.new(cents * value)
  end
end

#+(other_money) ⇒ Object



62
63
64
# File 'lib/mongoid_money/money.rb', line 62

def +(other_money)
  Money.new(cents + other_money.cents)
end

#-(other_money) ⇒ Object



66
67
68
# File 'lib/mongoid_money/money.rb', line 66

def -(other_money)
  Money.new(cents - other_money.cents)
end

#/(value) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/mongoid_money/money.rb', line 78

def /(value)
  if value.is_a?(Money)
    (cents / BigDecimal.new(value.cents.to_s)).to_f
  else
    Money.new(cents / value)
  end
end

#<=>(other_money) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/mongoid_money/money.rb', line 53

def <=>(other_money)
  if other_money.respond_to?(:to_money)
    other_money = other_money.to_money
    cents <=> other_money.cents
  else
    raise ArgumentError, "Comparison of #{self.class} with #{other_money.inspect} failed"
  end
end

#==(other_money) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/mongoid_money/money.rb', line 36

def ==(other_money)
  if other_money.respond_to?(:to_money)
    other_money = other_money.to_money
    cents == other_money.cents
  else
    false
  end
end

#absObject



120
121
122
# File 'lib/mongoid_money/money.rb', line 120

def abs
  Money.new(self.cents.abs)
end

#coerce(other) ⇒ Object



156
157
158
# File 'lib/mongoid_money/money.rb', line 156

def coerce(other)
  return other, to_f
end

#div(value) ⇒ Object



86
87
88
# File 'lib/mongoid_money/money.rb', line 86

def div(value)
  self / value
end

#divmod(val) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/mongoid_money/money.rb', line 90

def divmod(val)
  if val.is_a?(Money)
    a = self.cents
    b = val.cents
    q, m = a.divmod(b)
    return [q, Money.new(m)]
  else
    return [self.div(val), Money.new(self.cents.modulo(val))]
  end
end

#dollarsObject



32
33
34
# File 'lib/mongoid_money/money.rb', line 32

def dollars
  (BigDecimal.new(cents.to_s) / 100 ).to_f
end

#eql?(other_money) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/mongoid_money/money.rb', line 45

def eql?(other_money)
  self == other_money
end

#even?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/mongoid_money/money.rb', line 144

def even?
  @cents%2 == 0
end

#hashObject



49
50
51
# File 'lib/mongoid_money/money.rb', line 49

def hash
  cents.hash
end

#inspectObject



136
137
138
# File 'lib/mongoid_money/money.rb', line 136

def inspect
  to_s
end

#modulo(val) ⇒ Object



101
102
103
# File 'lib/mongoid_money/money.rb', line 101

def modulo(val)
  self.divmod(val)[1]
end

#nonzero?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/mongoid_money/money.rb', line 128

def nonzero?
  cents != 0 ? self : nil
end

#odd?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/mongoid_money/money.rb', line 140

def odd?
  @cents%2 > 0
end

#remainder(val) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/mongoid_money/money.rb', line 109

def remainder(val)
  a, b = self, val

  a_sign, b_sign = :pos, :pos
  a_sign = :neg if a.cents < 0
  b_sign = :neg if (b.is_a?(Money) and b.cents < 0) or (b < 0)

  return a.modulo(b) if a_sign == b_sign
  a.modulo(b) - (b.is_a?(Money) ? b : Money.new(b))
end

#to_fObject



152
153
154
# File 'lib/mongoid_money/money.rb', line 152

def to_f
  dollars
end

#to_moneyObject



132
133
134
# File 'lib/mongoid_money/money.rb', line 132

def to_money
  self
end

#to_sObject



148
149
150
# File 'lib/mongoid_money/money.rb', line 148

def to_s
  dollars.to_s
end

#zero?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/mongoid_money/money.rb', line 124

def zero?
  cents == 0
end