Class | X12::Segment |
In: |
lib/X12/Segment.rb
|
Parent: | Base |
$Id: Segment.rb 35 2008-11-13 18:33:44Z ikk $
Implements a segment containing fields or composites
Finds a field in the segment. Returns EMPTY if not found.
# File lib/X12/Segment.rb, line 72 72: def find_field(str) 73: #puts "Finding field [#{str}] in #{self.class} #{name}" 74: # If there is such a field to begin with 75: field_num = nil 76: self.nodes.each_index{|i| 77: field_num = i if str == self.nodes[i].name 78: } 79: return EMPTY if field_num.nil? 80: #puts field_num 81: 82: # Parse the segment if not parsed already 83: unless @fields 84: @fields = self.to_s.chop.split(Regexp.new(Regexp.escape(field_separator))) 85: self.nodes.each_index{|i| self.nodes[i].content = @fields[i+1] } 86: end 87: #puts self.nodes[field_num].inspect 88: return self.nodes[field_num] 89: end
Parses this segment out of a string, puts the match into value, returns the rest of the string - nil if cannot parse
# File lib/X12/Segment.rb, line 34 34: def parse(str) 35: s = str 36: #puts "Parsing segment #{name} from #{s} with regexp [#{regexp.source}]" 37: m = regexp.match(s) 38: #puts "Matched #{m ? m[0] : 'nothing'}" 39: 40: return nil unless m 41: 42: s = m.post_match 43: self.parsed_str = m[0] 44: s = do_repeats(s) 45: 46: #puts "Parsed segment "+self.inspect 47: return s 48: end
Render all components of this segment as string suitable for EDI
# File lib/X12/Segment.rb, line 51 51: def render 52: self.to_a.inject(''){|repeat_str, i| 53: if i.repeats.begin < 1 and !i.has_content? 54: # Skip optional empty segments 55: repeat_str 56: else 57: # Have to render no matter how empty 58: repeat_str += i.name+i.nodes.reverse.inject(''){|nodes_str, j| 59: field = j.render 60: (j.required or nodes_str != '' or field != '') ? field_separator+field+nodes_str : nodes_str 61: } + segment_separator 62: end 63: } 64: end