Class: Bankserv::Set

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/bankserv/transmission/set.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_hash(options) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bankserv/transmission/set.rb', line 82

def self.from_hash(options)
  header_options = options[:data].select{|h| h[:type] == 'header'}.first
  trailer_options = options[:data].select{|h| h[:type] == 'trailer'}.first
  transaction_options = options[:data].select{|h| not ['header','trailer'].include?(h[:type])}

  klass_name = "Bankserv::Transmission::UserSet::#{options[:type].camelize}"
  
  if klass_name == "Bankserv::Transmission::UserSet::Eft"
    # hack for debit/credit eft
    klass_name = "Bankserv::Transmission::UserSet::Debit" if header_options[:data][:rec_id] == "001"
    klass_name = "Bankserv::Transmission::UserSet::Credit" if header_options[:data][:rec_id] == "020"  
  end
  
  set = klass_name.constantize.new
  set.build_header(header_options[:data]) if header_options
  
  transaction_options.each do |option|
    if option[:data].is_a? Array
      set.sets << self.from_hash(option)
    else
      set.records << Record.new(record_type: option[:type], data: option[:data], reference: option[:data][:user_ref])
    end
  end
  
  set.build_trailer(trailer_options[:data]) if trailer_options
  set
end

.partial_class_nameObject



52
53
54
# File 'lib/bankserv/transmission/set.rb', line 52

def self.partial_class_name
  self.name.split("::")[-1]
end

Instance Method Details

#base_setObject



118
119
120
121
# File 'lib/bankserv/transmission/set.rb', line 118

def base_set
  return parent.base_set if parent
  self
end

#build_header(options = {}) ⇒ Object



10
11
12
# File 'lib/bankserv/transmission/set.rb', line 10

def build_header(options = {})
  records.build(record_type: "header", data: options)
end

#build_trailer(options = {}) ⇒ Object



14
15
16
# File 'lib/bankserv/transmission/set.rb', line 14

def build_trailer(options = {})
  records.build(record_type: "trailer", data: options)
end

#contained_setsObject



110
111
112
# File 'lib/bankserv/transmission/set.rb', line 110

def contained_sets
  ([self] + sets.map(&:contained_sets)).flatten
end

#decorate_recordsObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bankserv/transmission/set.rb', line 40

def decorate_records
  klass = "Absa::H2h::Transmission::#{set_type.camelize}".constantize
  
  records.each do |record|
    defaults = klass.record_type(record.record_type).template_options
    record.data = defaults.merge(record.data)
    record.data[:rec_status] ||= rec_status
  end
  
  self.records.each{|rec| rec.save!} # TODO: does this cause records to save before set?
end

#get_documentObject



123
124
125
# File 'lib/bankserv/transmission/set.rb', line 123

def get_document
  base_set.document
end

#headerObject



28
29
30
# File 'lib/bankserv/transmission/set.rb', line 28

def header
  records.select {|rec| rec.record_type == "header"}.first
end

#number_of_recordsObject



56
57
58
# File 'lib/bankserv/transmission/set.rb', line 56

def number_of_records
  records.size + sets.inject(0) { |sum, set| sum + set.number_of_records }
end

#parentObject



18
19
20
# File 'lib/bankserv/transmission/set.rb', line 18

def parent
  set
end

#processObject



78
79
80
# File 'lib/bankserv/transmission/set.rb', line 78

def process
  sets.each{|s| s.process}
end

#rec_statusObject

is it test/live data



22
23
24
25
26
# File 'lib/bankserv/transmission/set.rb', line 22

def rec_status # is it test/live data
  return document.rec_status if document
  return parent.rec_status if parent
  "T"
end

#record_with_sequence_number(sequence_number) ⇒ Object



114
115
116
# File 'lib/bankserv/transmission/set.rb', line 114

def record_with_sequence_number(sequence_number)
  transactions.select{|rec| rec.data[:user_sequence_number] == sequence_number}.first
end

#set_typeObject



60
61
62
# File 'lib/bankserv/transmission/set.rb', line 60

def set_type
  self.class.partial_class_name.underscore
end

#to_hashObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bankserv/transmission/set.rb', line 64

def to_hash
  data = []
  data << header.to_hash if header
  data << transactions.collect{|rec| rec.to_hash}
  data << sets.collect{|s| s.to_hash}
  data << trailer.to_hash if trailer
  data.flatten!
  
  {
    type: set_type,
    data: data
  }
end

#trailerObject



32
33
34
# File 'lib/bankserv/transmission/set.rb', line 32

def trailer
  records.select {|rec| rec.record_type == "trailer"}.first
end

#transactionsObject



36
37
38
# File 'lib/bankserv/transmission/set.rb', line 36

def transactions
  records.select {|rec| !(["header", "trailer"].include? rec.record_type)  }
end