lib/X12/Segment.rb in X12-0.1.0 vs lib/X12/Segment.rb in X12-1.1.0
- old
+ new
@@ -21,11 +21,11 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#++
#
module X12
- # $Id: Segment.rb 54 2009-03-18 17:04:45Z ikk $
+ # $Id: Segment.rb 82 2009-05-13 18:07:22Z ikk $
#
# Implements a segment containing fields or composites
class Segment < Base
@@ -67,17 +67,19 @@
def regexp
unless @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|
- s+i.str_regexp(field_separator)+Regexp.escape(field_separator)
- } + '?'+Regexp.escape(segment_separator)
+ field_re = i.simple_regexp(field_separator, segment_separator)+Regexp.escape(field_separator)+'?'
+ field_re = "(#{field_re})?" unless i.required
+ s+field_re
+ } + Regexp.escape(segment_separator)
@regexp = Regexp.new(re_str)
else
# Simple match
@regexp = Regexp.new("^#{name}#{Regexp.escape(field_separator)}[^#{Regexp.escape(segment_separator)}]*#{Regexp.escape(segment_separator)}")
end
- #puts @regexp
+ #puts sprintf("%s %p", name, @regexp)
end
@regexp
end
# Finds a field in the segment. Returns EMPTY if not found.