| Class | X12::Base |
| In: |
lib/X12/Base.rb
|
| Parent: | Object |
| composite_separator | [W] | |
| composite_separator | [R] | |
| field_separator | [W] | |
| field_separator | [R] | |
| name | [R] | |
| next_repeat | [R] | |
| next_repeat | [W] | |
| nodes | [R] | |
| nodes | [W] | |
| parsed_str | [R] | |
| parsed_str | [W] | |
| repeats | [R] | |
| segment_separator | [R] | |
| segment_separator | [W] |
Creates a new base element with a given name, array of sub-elements, and array of repeats if any.
# File lib/X12/Base.rb, line 38
38: def initialize(name, arr, repeats = nil)
39: @nodes = arr
40: @name = name
41: @repeats = repeats
42: @next_repeat = nil # Next repeat of the same element, if any
43: @parsed_str = nil
44:
45: @segment_separator = '~'
46: @field_separator = '*'
47: @composite_separator = ':'
48:
49: #puts "Created #{name} #{object_id} #{self.class} "
50: end
The main method implementing Ruby-like access methods for repeating elements
# File lib/X12/Base.rb, line 173
173: def [](*args)
174: #puts "squares #{args.inspect}"
175: return self.to_a[args[0]] || EMPTY
176: end
Try to parse the current element one more time if required. Returns the rest of the string or the same string if no more repeats are found or required.
# File lib/X12/Base.rb, line 79
79: def do_repeats(s)
80: if self.repeats.end > 1
81: possible_repeat = self.dup
82: p_s = possible_repeat.parse(s)
83: if p_s
84: s = p_s
85: self.next_repeat = possible_repeat
86: end # if parsed
87: end # more repeats
88: s
89: end
Make a deep copy of the element
# File lib/X12/Base.rb, line 99
99: def dup
100: n = clone
101: n.set_empty!
102: n.nodes = n.nodes.dup
103: n.nodes.each_index{|i|
104: n.nodes[i] = n.nodes[i].dup
105: n.nodes[i].set_empty!
106: }
107: #puts "Duped #{self.class} #{self.name} #{self.object_id} #{super.object_id} -> #{n.name} #{n.super.object_id} #{n.object_id} "
108: n
109: end
Recursively find a sub-element, which also has to be of type Base.
# File lib/X12/Base.rb, line 112
112: def find(e)
113: #puts "Finding [#{e}] in #{self.class} #{name}"
114: case self
115: when X12::Loop
116: # Breadth first
117: res = nodes.find{|i| e==i.name }
118: return res if res
119: # Depth now
120: nodes.each{|i|
121: res = i.find(e) if i.kind_of?(X12::Loop)
122: return res unless res.nil? or EMPTY==res # otherwise keep looping
123: }
124: when X12::Segment
125: return find_field(e).to_s
126: end # case
127: return EMPTY
128: end
Check if any of the fields has been set yet
# File lib/X12/Base.rb, line 193
193: def has_content?
194: self.nodes.find{|i| i.has_content?}
195: end
Formats a printable string containing the base element‘s content
# File lib/X12/Base.rb, line 53
53: def inspect
54: "#{self.class.to_s.sub(/^.*::/, '')} (#{name}) #{repeats} #{super.inspect[1..-2]} =<#{parsed_str}, #{next_repeat.inspect}> ".gsub(/\\*\"/, '"')
55: end
The main method implementing Ruby-like access methods for nested elements
# File lib/X12/Base.rb, line 147
147: def method_missing(meth, *args, &block)
148: str = meth.id2name
149: str = str[1..str.length] if str =~ /^_\d+$/ # to avoid pure number names like 270, 997, etc.
150: #puts "Missing #{str}"
151: if str =~ /=$/
152: # Assignment
153: str.chop!
154: #puts str
155: case self
156: when X12::Segment
157: res = find_field(str)
158: throw Exception.new("No field '#{str}' in segment '#{self.name}'") if EMPTY == res
159: res.content = args[0].to_s
160: #puts res.inspect
161: else
162: throw Exception.new("Illegal assignment to #{meth} of #{self.class}")
163: end # case
164: else
165: # Retrieval
166: res = find(str)
167: yield res if block_given?
168: res
169: end # if assignment
170: end
Adds a repeat to a segment or loop. Returns a new segment/loop or self if empty.
# File lib/X12/Base.rb, line 198
198: def repeat
199: res = if self.has_content? # Do not repeat an empty segment
200: last_repeat = self.to_a[-1]
201: last_repeat.next_repeat = last_repeat.dup
202: else
203: self
204: end
205: yield res if block_given?
206: res
207: end
Prints a tree-like representation of the element
# File lib/X12/Base.rb, line 58
58: def show(ind = '')
59: count = 0
60: self.to_a.each{|i|
61: #puts "#{ind}#{i.name} #{i.object_id} #{i.super.object_id} [#{count}]: #{i.parsed_str} #{i.super.class}"
62: puts "#{ind}#{i.name} [#{count}]: #{i.to_s.sub(/^(.{30})(.*?)(.{30})$/, '\1...\3')}"
63: # Force parsing a segment
64: if i.kind_of?(X12::Segment) && i.nodes[0]
65: i.find_field(i.nodes[0].name)
66: end
67: i.nodes.each{|j|
68: case
69: when j.kind_of?(X12::Base) : j.show(ind+' ')
70: when j.kind_of?(X12::Field) : puts "#{ind+' '}#{j.name} -> '#{j.to_s}'"
71: end
72: }
73: count += 1
74: }
75: end
Returns number of repeats
# File lib/X12/Base.rb, line 188
188: def size
189: return self.to_a.size
190: end
Returns a parsed string representation of the element
# File lib/X12/Base.rb, line 142
142: def to_s
143: @parsed_str || ''
144: end