Class: X12::CF

Inherits:
Object
  • Object
show all
Defined in:
lib/x12/cf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCF

Returns a new instance of CF.



39
40
41
42
43
44
# File 'lib/x12/cf.rb', line 39

def initialize
  @loop_trees = []
  @segmentstart = {}
  @file_contents = []
  @loops = []
end

Instance Attribute Details

#file_contentsObject

Returns the value of attribute file_contents.



35
36
37
# File 'lib/x12/cf.rb', line 35

def file_contents
  @file_contents
end

#levelObject

Returns the value of attribute level.



37
38
39
# File 'lib/x12/cf.rb', line 37

def level
  @level
end

#loop_treesObject

Returns the value of attribute loop_trees.



33
34
35
# File 'lib/x12/cf.rb', line 33

def loop_trees
  @loop_trees
end

#loopsObject

Returns the value of attribute loops.



36
37
38
# File 'lib/x12/cf.rb', line 36

def loops
  @loops
end

#segmentstartObject

Returns the value of attribute segmentstart.



34
35
36
# File 'lib/x12/cf.rb', line 34

def segmentstart
  @segmentstart
end

Instance Method Details

#_find_loopsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/x12/cf.rb', line 72

def _find_loops

  in_loops = false

  @file_contents.each do |line|
    if line == '[LOOPS]'
      in_loops = true
      next
    end

    return if line == ''

    @loops << line if in_loops == true

  end
end

#_parse_loop(loop) ⇒ Object



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
# File 'lib/x12/cf.rb', line 100

def _parse_loop(loop)

  in_loop = false

  @level += 1

  @file_contents.each { |line|

    if line == "[#{loop}]"

      loop_tree = LoopTree.new
      loop_tree.loop = loop
      loop_tree.level = @level
      @loop_trees << loop_tree
      in_loop = true

    elsif in_loop == true and line =~ /^loop=/
      tmp = line.split('=')
      _parse_loop(tmp[1])
      @level -= 1
    end

    return if in_loop and line == ''
  }

end

#_parse_loopsObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/x12/cf.rb', line 89

def _parse_loops

  @loops.each { |loop|
    @level = 0
    _parse_loop(loop)
  }
  @loops.each { |loop|
    _parse_segment(loop)
  }
end

#_parse_segment(loop) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/x12/cf.rb', line 127

def _parse_segment(loop)

  in_loop = false

  @file_contents.each { |line|

    if line == "[#{loop}]"
      in_loop = true
    end

    if in_loop == true

      if line =~ /^segment=/
        tmp = line.split('=')
        @segmentstart[loop] = [] if not @segmentstart[loop]
        @segmentstart[loop] << tmp[1]
      end

      if line =~ /^loop=/
        tmp = line.split('=')
        _parse_segment(tmp[1])
      end
    end

    return if in_loop == true and line == ''

  }
end

#get_level_oneObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/x12/cf.rb', line 156

def get_level_one

  tmp = []

  i = 0

  @loop_trees.each do |lt|
    if lt.level == 1
      tmp.push(i)
    end

    i += 1
  end
  tmp

end

#get_next_level(current_pos) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/x12/cf.rb', line 173

def get_next_level(current_pos)
  current_level = @loop_trees[current_pos].level || 0

  if @loop_trees[current_pos + 1]
    next_level = @loop_trees[current_pos + 1].level
  else
    next_level = 0
  end

  tmp = []

  if current_level < next_level
    current_pos += 1

    while @loop_trees[current_pos].level > current_level
      if @loop_trees[current_pos].level == next_level
        tmp.push(current_pos)
      end
      current_pos += 1
    end
  else
    tmp.push('END')
  end

  tmp

end

#load(file) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/x12/cf.rb', line 46

def load(file)
  
  load_file = nil   

  local_path = File.dirname(__FILE__).to_s + "/../../cf/#{file}"

  if File.exist?(file)
    load_file = file
  end

  if File.exist?(local_path)
    load_file = local_path
  end

  return false if not load_file

  File.open(load_file, 'r') { |file|
    file.each do |line|
      @file_contents << line.strip
    end
  }

  self._find_loops
  self._parse_loops
end