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



52
53
54
# File 'lib/keepr/posting.rb', line 52

def amount
  raw_amount.try(:abs)
end

#amount=(value) ⇒ Object

Raises:

  • (ArgumentError)


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

Returns:

  • (Boolean)


40
41
42
# File 'lib/keepr/posting.rb', line 40

def credit?
  side == SIDE_CREDIT
end

#debit?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/keepr/posting.rb', line 36

def debit?
  side == SIDE_DEBIT
end

#raw_amountObject



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

#sideObject



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