Class: Countir::Journal

Inherits:
Object
  • Object
show all
Defined in:
lib/countir/resources/journal.rb

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transaction_date:, memo:) ⇒ Journal

Returns a new instance of Journal.



23
24
25
26
27
# File 'lib/countir/resources/journal.rb', line 23

def initialize(transaction_date:, memo:)
  self.transaction_date = transaction_date
  self.memo             = memo
  self.entries          = []
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



3
4
5
# File 'lib/countir/resources/journal.rb', line 3

def entries
  @entries
end

#memoObject

Returns the value of attribute memo.



3
4
5
# File 'lib/countir/resources/journal.rb', line 3

def memo
  @memo
end

#transaction_dateObject

Returns the value of attribute transaction_date.



3
4
5
# File 'lib/countir/resources/journal.rb', line 3

def transaction_date
  @transaction_date
end

Class Method Details

.response_schema(data) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/countir/resources/journal.rb', line 62

def self.response_schema(data)
  data["journals"].map do |journal_data|
    journal = self.new(
      transaction_date: journal_data["transaction_date"],
      memo:             journal_data["memo"],
    )

    journal_data["entries"].each do |entry|
      journal.add_entry(
        account_code_id: entry["account_code_id"],
        debit_or_credit: entry["debit_or_credit"],
        price:           entry["price"],
      )
    end

    journal
  end
end

Instance Method Details

#add_entry(account_code_id:, debit_or_credit:, price:) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/countir/resources/journal.rb', line 29

def add_entry(account_code_id:, debit_or_credit:, price:)
  self.entries << Entry.new(
    account_code_id: ,
    debit_or_credit: debit_or_credit,
    price:           price
  )
end

#to_hObject



55
56
57
58
59
60
# File 'lib/countir/resources/journal.rb', line 55

def to_h
  { transaction_date: self.transaction_date,
    memo:             self.memo,
    entries:          self.entries.map(&:to_h),
  }
end

#valid?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/countir/resources/journal.rb', line 43

def valid?
  debit_price  = 0
  credit_price = 0

  self.entries.each do |entry|
    debit_price  += entry.price if entry.debit_or_credit  == "debit"
    credit_price += entry.price if entry.credit_or_credit == "credit"
  end

  debit_price == credit_price
end

#validation!Object



37
38
39
40
41
# File 'lib/countir/resources/journal.rb', line 37

def validation!
  unless self.valid?
    raise
  end
end