Class X12::Segment
In: lib/X12/Segment.rb
Parent: Base

$Id: Segment.rb 82 2009-05-13 18:07:22Z ikk $

Implements a segment containing fields or composites

Methods

find_field   parse   regexp   render  

Public Instance methods

Finds a field in the segment. Returns EMPTY if not found.

[Source]

     # File lib/X12/Segment.rb, line 87
 87:     def find_field(str)
 88:       #puts "Finding field [#{str}] in #{self.class} #{name}"
 89:       # If there is such a field to begin with
 90:       field_num = nil
 91:       self.nodes.each_index{|i|
 92:         field_num = i if str == self.nodes[i].name
 93:       }
 94:       return EMPTY if field_num.nil?
 95:       #puts field_num
 96: 
 97:       # Parse the segment if not parsed already
 98:       unless @fields
 99:         @fields = self.to_s.chop.split(Regexp.new(Regexp.escape(field_separator)))
100:         self.nodes.each_index{|i| self.nodes[i].content = @fields[i+1] }
101:       end
102:       #puts self.nodes[field_num].inspect
103:       return self.nodes[field_num]
104:     end

Parses this segment out of a string, puts the match into value, returns the rest of the string - nil if cannot parse

[Source]

    # 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

Returns a regexp that matches this particular segment

[Source]

    # File lib/X12/Segment.rb, line 67
67:     def regexp
68:       unless @regexp
69:         if self.nodes.find{|i| i.type =~ /^".+"$/ }
70:           # It's a very special regexp if there are constant fields
71:           re_str = self.nodes.inject("^#{name}#{Regexp.escape(field_separator)}"){|s, i|
72:             field_re = i.simple_regexp(field_separator, segment_separator)+Regexp.escape(field_separator)+'?'
73:             field_re = "(#{field_re})?" unless i.required
74:             s+field_re
75:           } + Regexp.escape(segment_separator)
76:           @regexp = Regexp.new(re_str)
77:         else
78:           # Simple match
79:           @regexp = Regexp.new("^#{name}#{Regexp.escape(field_separator)}[^#{Regexp.escape(segment_separator)}]*#{Regexp.escape(segment_separator)}")
80:         end
81:         #puts sprintf("%s %p", name, @regexp)
82:       end
83:       @regexp
84:     end

Render all components of this segment as string suitable for EDI

[Source]

    # 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