Class: Guevara::Nacha

Inherits:
Object
  • Object
show all
Defined in:
lib/guevara/nacha.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Nacha

Returns a new instance of Nacha.



10
11
12
13
# File 'lib/guevara/nacha.rb', line 10

def initialize attributes
  self.batches = attributes.delete(:batches)
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/guevara/nacha.rb', line 8

def attributes
  @attributes
end

#batchesObject

Returns the value of attribute batches.



8
9
10
# File 'lib/guevara/nacha.rb', line 8

def batches
  @batches
end

Instance Method Details

#file_controlObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/guevara/nacha.rb', line 40

def file_control
  entry_count = total { |b| b.batch_control.attributes[:entry_count] }
  block_count = entry_count + batches.size * 2 + 2
  FileControl.new(
    batch_count:  batches.count,
    block_count:  block_count,
    entry_count:  entry_count,
    entry_hash:   total { |b| b.batch_control.attributes[:entry_hash] },
    total_debit:  total { |b| b.total('debit') },
    total_credit: total { |b| b.total('credit') }
)
end

#file_headerObject



30
31
32
# File 'lib/guevara/nacha.rb', line 30

def file_header
  FileHeader.new attributes
end

#to_sObject



22
23
24
25
26
27
28
# File 'lib/guevara/nacha.rb', line 22

def to_s
  file = []
  file << file_header.to_s
  batches.each { |batch| file << batch.to_s }
  file << file_control.to_s
  file.join('')
end

#totalObject



34
35
36
37
38
# File 'lib/guevara/nacha.rb', line 34

def total
  batches.
    map { |b| yield b }.
    reduce(:+)
end