class Headache::Document

Attributes

batches[R]

Public Class Methods

new(header = nil, control = nil, batches = []) click to toggle source
# File lib/headache/document.rb, line 7
def initialize(header = nil, control = nil, batches = [])
  @header  = header
  @control = control
  @batches = batches
  [@header, @control].each { |r| r.try :document=, self }
end

Public Instance Methods

<<(batch) click to toggle source
# File lib/headache/document.rb, line 29
def <<(batch)
  add_batch batch
  self
end
add_batch(batch) click to toggle source
# File lib/headache/document.rb, line 24
def add_batch(batch)
  batch.document = self
  @batches << batch
end
batch() click to toggle source
# File lib/headache/document.rb, line 19
def batch
  fail Headache::AmbiguousBatch, 'multiple batches detected, be more explicit or use first_batch' if @batches.count > 1
  first_batch
end
build() click to toggle source
# File lib/headache/document.rb, line 66
def build
  append_record header
  @batches.each do |batch|
    append_record batch.header
    batch.entries.each { |entry| append_record entry }
    append_record batch.control
  end
  append_record control
  overflow_lines_needed.times { append_record Record::Overflow.new }
end
control() click to toggle source
# File lib/headache/document.rb, line 42
def control
  @control ||= Record::FileControl.new self
end
entries() click to toggle source
# File lib/headache/document.rb, line 34
def entries
  @batches.map(&:entries).flatten
end
first_batch() click to toggle source
# File lib/headache/document.rb, line 14
def first_batch
  add_batch Batch.new(self) if @batches.empty?
  @batches.first
end
header() click to toggle source
# File lib/headache/document.rb, line 38
def header
  @header ||= Record::FileHeader.new self
end
lines() click to toggle source
# File lib/headache/document.rb, line 52
def lines
  @content.split("\n").count
end
overflow_lines_needed() click to toggle source
# File lib/headache/document.rb, line 56
def overflow_lines_needed
  10 - lines % 10
end
records() click to toggle source
# File lib/headache/document.rb, line 46
def records
  ([header] << batches.map do |batch|
    [batch.header, batch.entries, batch.control]
  end << control).flatten
end
to_h() click to toggle source
# File lib/headache/document.rb, line 60
def to_h
  { file_header: @header.to_h,
    batches: @batches.map(&:to_h),
    file_control: @control.to_h }
end