lib/x12/segment.rb in tcd_x12-1.6.2 vs lib/x12/segment.rb in tcd_x12-1.6.3
- old
+ new
@@ -1,11 +1,16 @@
module X12
# Implements a segment containing fields or composites
class Segment < Base
+ attr_accessor :fields
+
# Parses this segment out of a string, puts the match into value,
# returns the rest of the string - nil if cannot parse.
+ #
+ # @param str [String]
+ # @return [nil]
def parse(str)
s = str
# puts "Parsing segment #{name} from #{s} with regexp [#{regexp.source}]"
m = regexp.match(s)
# puts "Matched #{m ? m[0] : 'nothing'}"
@@ -36,11 +41,11 @@
}
end
# Returns a regexp that matches this particular segment.
def regexp
- unless @regexp
+ unless defined? @regexp
if self.nodes.find { |i| i.type =~ /^".+"$/ }
# It's a very special regexp if there are constant fields
re_str = self.nodes.inject("^#{name}#{Regexp.escape(field_separator)}") { |s, i|
field_re = i.simple_regexp(field_separator, segment_separator) + Regexp.escape(field_separator) + '?'
field_re = "(#{field_re})?" unless i.required
@@ -66,10 +71,10 @@
}
return EMPTY if field_num.nil?
# puts field_num
# Parse the segment if not parsed already
- unless @fields
+ unless defined? @fields
@fields = self.to_s.chop.split(Regexp.new(Regexp.escape(field_separator)))
self.nodes.each_index { |i| self.nodes[i].content = @fields[i + 1] }
end
# puts self.nodes[field_num].inspect
return self.nodes[field_num]