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
55 56 57 |
# File 'lib/keepr/posting.rb', line 55 def amount raw_amount.try(:abs) end |
#amount=(value) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/keepr/posting.rb', line 59 def amount=(value) @side ||= SIDE_DEBIT unless value self.raw_amount = nil return end raise ArgumentError, 'Negative amount not allowed!' if value.to_d.negative? self.raw_amount = credit? ? -value.to_d : value.to_d end |
#credit? ⇒ Boolean
43 44 45 |
# File 'lib/keepr/posting.rb', line 43 def credit? side == SIDE_CREDIT end |
#debit? ⇒ Boolean
39 40 41 |
# File 'lib/keepr/posting.rb', line 39 def debit? side == SIDE_DEBIT end |
#raw_amount ⇒ Object
47 48 49 |
# File 'lib/keepr/posting.rb', line 47 def raw_amount read_attribute(:amount) end |
#raw_amount=(value) ⇒ Object
51 52 53 |
# File 'lib/keepr/posting.rb', line 51 def raw_amount=(value) write_attribute(:amount, value) end |
#side ⇒ Object
20 21 22 23 24 |
# File 'lib/keepr/posting.rb', line 20 def side @side || begin (raw_amount.negative? ? SIDE_CREDIT : SIDE_DEBIT) if raw_amount end end |
#side=(value) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/keepr/posting.rb', line 26 def side=(value) raise ArgumentError unless [SIDE_DEBIT, SIDE_CREDIT].include?(value) @side = value return unless amount if credit? self.raw_amount = -amount.to_d elsif debit? self.raw_amount = amount.to_d end end |