Class: Absa::Esd::Transmission::Set

Inherits:
Object
  • Object
show all
Defined in:
lib/absa-esd/transmission/set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSet

Returns a new instance of Set.



9
10
11
# File 'lib/absa-esd/transmission/set.rb', line 9

def initialize
  self.records = []
end

Instance Attribute Details

#recordsObject

Returns the value of attribute records.



7
8
9
# File 'lib/absa-esd/transmission/set.rb', line 7

def records
  @records
end

Class Method Details

.build(data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/absa-esd/transmission/set.rb', line 13

def self.build(data)
  set = self.new
      
  data.each do |hash|
    if hash[:data].is_a? Array
      klass = "Absa::Esd::Statement4Unpacked::#{hash[:type].capitalize.camelize}".constantize
      set.records << klass.build(hash[:data])
    else
      klass = "Absa::Esd::Statement4Unpacked::#{self.partial_class_name}::#{hash[:type].capitalize.camelize}".constantize
      set.records << klass.new(hash[:data])
    end
  end
      
  set.validate!
  set
end

.for_record(record) ⇒ Object

move this logic to yml file



52
53
54
55
56
57
58
59
60
61
# File 'lib/absa-esd/transmission/set.rb', line 52

def self.for_record(record) # move this logic to yml file
  record_id = record[0]
      
  case record_id
  when '0','9'
    return Absa::Esd::Statement4Unpacked::ReconTransmission
  when '1','2','8'
    return Absa::Esd::Statement4Unpacked::ReconAccount
  end
end

.hash_from_s(string) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/absa-esd/transmission/set.rb', line 93

def self.hash_from_s(string)
  set_info = {type: self.partial_class_name.underscore, data: []}
  lines = string.split(/^/)
    
  # look for rec_ids, split into chunks, and pass each related class a piece of string
      
  buffer = []
  current_set = nil
  subset = nil
      
  lines.each do |line|
    next if line.length < 10 # TODO: flaky - tighten it up
    if Set.for_record(line) == self
      if subset && (buffer.length > 0)
        set_info[:data] << subset.hash_from_s(buffer.join)
        buffer = []
        subset = nil
      end
  
      record = line
      set_info[:data] << self.process_record(record)
    else
      subset = Set.for_record(line) unless subset        
      buffer << line
  
      if self.is_trailer_record?(subset, line)
        set_info[:data] << subset.hash_from_s(buffer.join)
        buffer = []
        subset = nil
      end
    end
  end
      
  set_info
end

.is_trailer_record?(set, record) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/absa-esd/transmission/set.rb', line 72

def self.is_trailer_record?(set, record)
  record_id = record[0]
  self.trailer_id(set) == record_id
end

.layout_rulesObject



141
142
143
144
145
# File 'lib/absa-esd/transmission/set.rb', line 141

def self.layout_rules
  file_name = "#{Absa::Esd::CONFIG_DIR}/#{self.name.split("::")[-2].underscore}/#{self.partial_class_name.underscore}.yml"
      
  YAML.load(File.open(file_name))
end

.module_nameObject



133
134
135
# File 'lib/absa-esd/transmission/set.rb', line 133

def self.module_name
  self.name.split("::")[0..-1].join("::")
end

.partial_class_nameObject



137
138
139
# File 'lib/absa-esd/transmission/set.rb', line 137

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

.process_record(record) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/absa-esd/transmission/set.rb', line 77

def self.process_record(record)
  record_info = {}
      
  self.record_types.each do |record_type|
    klass = "#{self.name}::#{record_type.camelize}".constantize

    if klass.matches_definition?(record)
      options = klass.string_to_hash(record)
      record_info = {type: record_type, data: options}
      break
    end
  end
      
  record_info
end

.record_type(record_type) ⇒ Object



147
148
149
# File 'lib/absa-esd/transmission/set.rb', line 147

def self.record_type(record_type)
  "#{self.name}::#{record_type.camelize}".constantize
end

.record_typesObject



129
130
131
# File 'lib/absa-esd/transmission/set.rb', line 129

def self.record_types
  self.layout_rules.map {|k,v| k}
end

.trailer_id(klass) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/absa-esd/transmission/set.rb', line 63

def self.trailer_id(klass)
  case klass.name
  when 'Absa::Esd::Statement4Unpacked::ReconTransmission'
    return '9'
  when 'Absa::Esd::Statement4Unpacked::ReconAccount'
    return '8'
  end
end

Instance Method Details

#headerObject



30
31
32
# File 'lib/absa-esd/transmission/set.rb', line 30

def header
  records[0]
end

#to_sObject



46
47
48
49
50
# File 'lib/absa-esd/transmission/set.rb', line 46

def to_s
  string = ""
  records.each {|record| string += record.to_s }
  string
end

#trailerObject



34
35
36
# File 'lib/absa-esd/transmission/set.rb', line 34

def trailer
  records[-1]
end

#transactionsObject



38
39
40
# File 'lib/absa-esd/transmission/set.rb', line 38

def transactions
  records[1..-2]
end

#validate!Object



42
43
44
# File 'lib/absa-esd/transmission/set.rb', line 42

def validate!
      
end