Class: Keepr::Posting
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Keepr::Posting
- Defined in:
- lib/keepr/posting.rb
Constant Summary collapse
- SIDE_DEBIT =
'debit'
- SIDE_CREDIT =
'credit'
Instance Method Summary collapse
- #amount ⇒ Object
- #amount=(value) ⇒ Object
- #credit? ⇒ Boolean
- #debit? ⇒ Boolean
- #raw_amount ⇒ Object
- #raw_amount=(value) ⇒ Object
- #side ⇒ Object
- #side=(value) ⇒ Object
Instance Method Details
#amount ⇒ Object
52 53 54 |
# File 'lib/keepr/posting.rb', line 52 def amount raw_amount.try(:abs) end |
#amount=(value) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/keepr/posting.rb', line 56 def amount=(value) raise ArgumentError.new('Negative amount not allowed!') if value.to_f < 0 @side ||= SIDE_DEBIT if credit? self.raw_amount = -value else self.raw_amount = value end end |
#credit? ⇒ Boolean
40 41 42 |
# File 'lib/keepr/posting.rb', line 40 def credit? side == SIDE_CREDIT end |
#raw_amount ⇒ Object
44 45 46 |
# File 'lib/keepr/posting.rb', line 44 def raw_amount read_attribute(:amount) end |
#raw_amount=(value) ⇒ Object
48 49 50 |
# File 'lib/keepr/posting.rb', line 48 def raw_amount=(value) write_attribute(:amount, value) end |
#side ⇒ Object
18 19 20 21 22 |
# File 'lib/keepr/posting.rb', line 18 def side @side || begin (raw_amount < 0 ? SIDE_CREDIT : SIDE_DEBIT) if raw_amount end end |
#side=(value) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/keepr/posting.rb', line 24 def side=(value) @side = value if credit? self.raw_amount = -amount if amount elsif debit? self.raw_amount = amount if amount else raise ArgumentError end end |