Class: Keepr::Posting

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/keepr/posting.rb

Constant Summary collapse

SIDE_DEBIT =
'debit'
SIDE_CREDIT =
'credit'

Instance Method Summary collapse

Instance Method Details

#amountObject



55
56
57
# File 'lib/keepr/posting.rb', line 55

def amount
  raw_amount.try(:abs)
end

#amount=(value) ⇒ Object

Raises:

  • (ArgumentError)


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_amountObject



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

#sideObject



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

Raises:

  • (ArgumentError)


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