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 164 164: def [](*args) 165: #puts "squares #{args.inspect}" 166: return self.to_a[args[0]] || EMPTY 167: 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 75 75: def do_repeats(s) 76: if self.repeats.end > 1 77: possible_repeat = self.dup 78: p_s = possible_repeat.parse(s) 79: if p_s 80: s = p_s 81: self.next_repeat = possible_repeat 82: end # if parsed 83: end # more repeats 84: s 85: end
Make a deep copy of the element
# File lib/X12/Base.rb, line 95 95: def dup 96: n = clone 97: n.set_empty! 98: n.nodes = n.nodes.dup 99: n.nodes.each_index{|i| 100: n.nodes[i] = n.nodes[i].dup 101: n.nodes[i].set_empty! 102: } 103: #puts "Duped #{self.class} #{self.name} #{self.object_id} #{super.object_id} -> #{n.name} #{n.super.object_id} #{n.object_id} " 104: n 105: end
Recursively find a sub-element, which also has to be of type Base.
# File lib/X12/Base.rb, line 108 108: def find(e) 109: #puts "Finding [#{e}] in #{self.class} #{name}" 110: case self 111: when X12::Loop 112: nodes.each{|i| 113: return i if e==i.name 114: res = i.find(e) if i.kind_of?(X12::Loop) 115: return res unless res.nil? or EMPTY==res # otherwise keep looping 116: } 117: when X12::Segment 118: return find_field(e).to_s 119: end # case 120: return EMPTY 121: end
Check if any of the fields has been set yet
# File lib/X12/Base.rb, line 175 175: def has_content? 176: self.nodes.find{|i| i.has_content?} 177: 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 140 140: def method_missing(meth, *args, &block) 141: str = meth.id2name 142: if str =~ /=$/ 143: # Assignment 144: str.chop! 145: #puts str 146: case self 147: when X12::Segment 148: res = find_field(str) 149: throw Exception.new("No field '#{str}' in segment '#{self.name}'") if EMPTY == res 150: res.content = args[0].to_s 151: #puts res.inspect 152: else 153: throw Exception.new("Illegal assignment to #{meth} of #{self.class}") 154: end # case 155: else 156: # Retrieval 157: res = find(str) 158: yield res if block_given? 159: res 160: end # if assignment 161: end
Adds a repeat to a segment or loop. Returns a new segment/loop or self if empty.
# File lib/X12/Base.rb, line 180 180: def repeat 181: res = if self.has_content? # Do not repeat an empty segment 182: last_repeat = self.to_a[-1] 183: last_repeat.next_repeat = last_repeat.dup 184: else 185: self 186: end 187: yield res if block_given? 188: res 189: 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: i.nodes.each{|j| 64: case 65: when j.kind_of?(X12::Base) : j.show(ind+' ') 66: when j.kind_of?(X12::Field) : puts "#{ind+' '}#{j.name} -> '#{j.to_s}'" 67: end 68: } 69: count += 1 70: } 71: end
Returns number of repeats
# File lib/X12/Base.rb, line 170 170: def size 171: return self.to_a.size 172: end