Class: Monee::Money
- Inherits:
-
Object
- Object
- Monee::Money
- Includes:
- Arithmetic, Comparison, Conversion
- Defined in:
- lib/monee/money.rb
Overview
class to manipulate currencies as required
Instance Attribute Summary collapse
-
#cents ⇒ Object
readonly
Returns the value of attribute cents.
-
#currency_code ⇒ Object
readonly
Returns the value of attribute currency_code.
Class Method Summary collapse
-
.conversion_rates(base_currency, currency_rates) ⇒ void
Configures the config singleton class with default values.
Instance Method Summary collapse
-
#amount ⇒ Numeric
Passed amount of the object.
-
#currency ⇒ String
The currency_code of the current object.
-
#initialize(amount, currency_code) ⇒ Money
constructor
Initializes a money object.
-
#inspect ⇒ String
Formatted string of money.
-
#klass ⇒ Money
Method to access this class.
Methods included from Comparison
Methods included from Conversion
#convert_to, #convert_to_cents
Methods included from Arithmetic
#choose_operand, #coerce, #operator
Constructor Details
#initialize(amount, currency_code) ⇒ Money
Initializes a money object
30 31 32 33 34 35 36 |
# File 'lib/monee/money.rb', line 30 def initialize(amount, currency_code) @amount = amount @cents = amount.to_cents @currency_code = currency_code validate_args! set_currency end |
Instance Attribute Details
#cents ⇒ Object (readonly)
Returns the value of attribute cents.
9 10 11 |
# File 'lib/monee/money.rb', line 9 def cents @cents end |
#currency_code ⇒ Object (readonly)
Returns the value of attribute currency_code.
9 10 11 |
# File 'lib/monee/money.rb', line 9 def currency_code @currency_code end |
Class Method Details
.conversion_rates(base_currency, currency_rates) ⇒ void
This method returns an undefined value.
Configures the config singleton class with default values
17 18 19 20 21 22 23 |
# File 'lib/monee/money.rb', line 17 def conversion_rates(base_currency, currency_rates) Currency.configure do |config| config.base_currency = base_currency config.currency_rates = currency_rates config.set_default_rate end end |
Instance Method Details
#amount ⇒ Numeric
Returns passed amount of the object.
49 50 51 |
# File 'lib/monee/money.rb', line 49 def amount cents.to_amount end |
#currency ⇒ String
Returns the currency_code of the current object.
44 45 46 |
# File 'lib/monee/money.rb', line 44 def currency currency_code end |
#inspect ⇒ String
Returns formatted string of money.
54 55 56 |
# File 'lib/monee/money.rb', line 54 def inspect "#{format('%.2f', amount)} #{currency}" end |
#klass ⇒ Money
Returns method to access this class.
39 40 41 |
# File 'lib/monee/money.rb', line 39 def klass self.class end |