Class: Farmoney::Money
- Inherits:
-
Object
- Object
- Farmoney::Money
- Defined in:
- lib/farmoney/money.rb
Instance Attribute Summary collapse
-
#pence ⇒ Object
(also: #to_i)
readonly
Returns the value of attribute pence.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #attributes ⇒ Object (also: #to_hash)
-
#initialize(pence) ⇒ Money
constructor
A new instance of Money.
- #to_s ⇒ Object
Constructor Details
#initialize(pence) ⇒ Money
12 13 14 |
# File 'lib/farmoney/money.rb', line 12 def initialize(pence) @pence = BigDecimal(pence) end |
Instance Attribute Details
#pence ⇒ Object (readonly) Also known as: to_i
Returns the value of attribute pence.
5 6 7 |
# File 'lib/farmoney/money.rb', line 5 def pence @pence end |
Instance Method Details
#*(other) ⇒ Object
44 45 46 |
# File 'lib/farmoney/money.rb', line 44 def *(other) Money.new(pence * other) end |
#+(other) ⇒ Object
31 32 33 |
# File 'lib/farmoney/money.rb', line 31 def +(other) Money.new(pence + other.pence) end |
#-(other) ⇒ Object
35 36 37 |
# File 'lib/farmoney/money.rb', line 35 def -(other) Money.new(pence - other.pence) end |
#/(other) ⇒ Object
39 40 41 42 |
# File 'lib/farmoney/money.rb', line 39 def /(other) divisor = other.respond_to?(:pence) ? other.pence : other Money.new(pence / BigDecimal(divisor, 2)) end |
#attributes ⇒ Object Also known as: to_hash
23 24 25 26 27 |
# File 'lib/farmoney/money.rb', line 23 def attributes instance_variables.each.with_object({}) do |attribute, hash| hash[attribute_name(attribute)] = instance_variable_get(attribute) end end |
#to_s ⇒ Object
16 17 18 19 20 21 |
# File 'lib/farmoney/money.rb', line 16 def to_s pounds = @pence / 100 pence = @pence % 100 format("£%.1i.%.2i", pounds, pence) end |