Class: Mint::Money

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/mint/money/money.rb,
lib/mint/money/version.rb

Overview

Minty Money Management

See Also:

Author:

Constant Summary collapse

VERSION =
'0.1.1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = 0.00, currency = nil) ⇒ Mint::Money

Parameters:

  • value (Integer) (defaults to: 0.00)
  • currency (String, Symbol) (defaults to: nil)


25
26
27
28
29
# File 'lib/mint/money/money.rb', line 25

def initialize(value = 0.00, currency = nil)
  @currency_sym = normalize_currency(currency)
  @currency = currency.to_s.upcase
  @value = Mint::Utils.to_amount(value, @currency_sym)
end

Instance Attribute Details

#currencyObject (readonly)

Returns the value of attribute currency.



19
20
21
# File 'lib/mint/money/money.rb', line 19

def currency
  @currency
end

#currency_symObject (readonly)

Returns the value of attribute currency_sym.



19
20
21
# File 'lib/mint/money/money.rb', line 19

def currency_sym
  @currency_sym
end

#valueObject (readonly) Also known as: amount

Returns the value of attribute value.



19
20
21
# File 'lib/mint/money/money.rb', line 19

def value
  @value
end

Instance Method Details

#*(other) ⇒ Mint::Money

Multiplication operation

Returns:



98
99
100
101
# File 'lib/mint/money/money.rb', line 98

def *(other)
  other = self.class.new(other, @currency_sym) unless other.is_a? self.class
  self.class.new(amount * other.amount, @currency_sym)
end

#+(other) ⇒ Mint::Money

Plus operation

Returns:



60
61
62
63
# File 'lib/mint/money/money.rb', line 60

def +(other)
  other = self.class.new(other, @currency_sym) unless other.is_a? self.class
  self.class.new(amount + cast_type(other).amount, @currency_sym)
end

#-(other) ⇒ Mint::Money

Minus operation

Returns:



67
68
69
70
# File 'lib/mint/money/money.rb', line 67

def -(other)
  other = self.class.new(other, @currency_sym) unless other.is_a? self.class
  self.class.new(amount - cast_type(other).amount, @currency_sym)
end

#/(other) ⇒ Mint::Money

Divide operation

Returns:



91
92
93
94
# File 'lib/mint/money/money.rb', line 91

def /(other)
  other = self.class.new(other, @currency_sym) unless other.is_a? self.class
  self.class.new(amount / other.amount, @currency_sym)
end

#<=>(other) ⇒ boolean

Lower and bigger (sort, spaceship) operation

Returns:

  • (boolean)


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

def <=>(other)
  other = self.class.new(other, @currency_sym) unless other.is_a? self.class
  amount <=> other.amount
end

#==(other) ⇒ Boolean

Equal operation Two Mint::Money objects are equal or one of them is a String and looks like .inspect results

Returns:

  • (Boolean)


75
76
77
# File 'lib/mint/money/money.rb', line 75

def ==(other)
  eql?(other)
end

#convert_to(currency, use_base = false) ⇒ Mint::Money

Create new Mint::Money with amount converted to another currency

Parameters:

  • currency (Symbol)
  • use_base (Boolean) (defaults to: false)

Returns:



54
55
56
# File 'lib/mint/money/money.rb', line 54

def convert_to(currency, use_base = false)
  Mint::Currency.convert_to(self, currency, use_base)
end

#eql?(other) ⇒ Boolean

rubocop:disable all

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/mint/money/money.rb', line 81

def eql?(other)
  return inspect == other if other.class == String
  # what if they are same class just different currencies
  other = cast_type(other) if other.is_a?(self.class) && currency_sym != other.currency_sym
  self.class == other.class && amount == other.amount && currency_sym == other.currency_sym
end

#inspectString

Returns formatted value and currency name

Returns:

  • (String)


40
41
42
# File 'lib/mint/money/money.rb', line 40

def inspect
  "#{self} #{currency}"
end

#to_sString

Auto/Stringify instance

Returns:

  • (String)


46
47
48
# File 'lib/mint/money/money.rb', line 46

def to_s
  Mint::Utils.to_format(value, currency_sym)
end