lib/X12/Segment.rb in X12-0.0.5 vs lib/X12/Segment.rb in X12-0.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 35 2008-11-13 18:33:44Z ikk $
+ # $Id: Segment.rb 54 2009-03-18 17:04:45Z ikk $
#
# Implements a segment containing fields or composites
class Segment < Base
@@ -63,10 +63,23 @@
}
end # render
# Returns a regexp that matches this particular segment
def regexp
- @regexp ||= Regexp.new("^#{name}#{Regexp.escape(field_separator)}[^#{Regexp.escape(segment_separator)}]*#{Regexp.escape(segment_separator)}")
+ 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)
+ @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
+ end
+ @regexp
end
# Finds a field in the segment. Returns EMPTY if not found.
def find_field(str)
#puts "Finding field [#{str}] in #{self.class} #{name}"