Class: Mint::Money
- Inherits:
-
Object
- Object
- Mint::Money
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/mint/money/money.rb,
lib/mint/money/version.rb
Overview
Minty Money Management
Constant Summary collapse
- VERSION =
'0.1.1'.freeze
Instance Attribute Summary collapse
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#currency_sym ⇒ Object
readonly
Returns the value of attribute currency_sym.
-
#value ⇒ Object
(also: #amount)
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#*(other) ⇒ Mint::Money
Multiplication operation.
-
#+(other) ⇒ Mint::Money
Plus operation.
-
#-(other) ⇒ Mint::Money
Minus operation.
-
#/(other) ⇒ Mint::Money
Divide operation.
-
#<=>(other) ⇒ boolean
Lower and bigger (sort, spaceship) operation.
-
#==(other) ⇒ Boolean
Equal operation Two Mint::Money objects are equal or one of them is a String and looks like .inspect results.
-
#convert_to(currency, use_base = false) ⇒ Mint::Money
Create new Mint::Money with amount converted to another currency.
-
#eql?(other) ⇒ Boolean
rubocop:disable all.
- #initialize(value = 0.00, currency = nil) ⇒ Mint::Money constructor
-
#inspect ⇒ String
Returns formatted value and currency name.
-
#to_s ⇒ String
Auto/Stringify instance.
Constructor Details
#initialize(value = 0.00, currency = nil) ⇒ Mint::Money
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
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
19 20 21 |
# File 'lib/mint/money/money.rb', line 19 def currency @currency end |
#currency_sym ⇒ Object (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 |
#value ⇒ Object (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
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
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
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
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
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
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
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
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 |
#inspect ⇒ String
Returns formatted value and currency name
40 41 42 |
# File 'lib/mint/money/money.rb', line 40 def inspect "#{self} #{currency}" end |
#to_s ⇒ String
Auto/Stringify instance
46 47 48 |
# File 'lib/mint/money/money.rb', line 46 def to_s Mint::Utils.to_format(value, currency_sym) end |