lib/X12/Field.rb in X12-0.1.0 vs lib/X12/Field.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: Field.rb 56 2009-03-18 19:56:01Z ikk $ + # $Id: Field.rb 90 2009-05-13 19:51:27Z ikk $ # # Class to represent a segment field. Please note, it's not a descendant of Base. class Field attr_reader :name, :type, :required, :min_length, :max_length, :validation @@ -33,11 +33,11 @@ # Create a new field with given parameters def initialize(name, type, required, min_length, max_length, validation) @name = name @type = type - @required = required == 'R' ? true : false + @required = required @min_length = min_length.to_i @max_length = max_length.to_i @validation = validation @content = nil end @@ -67,18 +67,25 @@ # Erase the content def set_empty! @content = nil end - # Returns string regexp for this field, takes field separator as an argument - def str_regexp(sep) - min = required ? @min_length : 0 + # Returns simplified string regexp for this field, takes field separator and segment separator as arguments + def simple_regexp(field_sep, segment_sep) case self.type - when 'I' : "\\d{#{min},#{@max_length}}" - when 'S' : "[^#{Regexp.escape(sep)}]{#{min},#{@max_length}}" - when /C.*/ : "[^#{Regexp.escape(sep)}]{#{min},#{@max_length}}" when /"(.*)"/ : $1 - else '' + else "[^#{Regexp.escape(field_sep)}#{Regexp.escape(segment_sep)}]*" + end # case + end # simple_regexp + + # Returns proper validating string regexp for this field, takes field separator and segment separator as arguments + def proper_regexp(field_sep, segment_sep) + case self.type + when 'I' : "\\d{#{@min_length},#{@max_length}}" + when 'S' : "[^#{Regexp.escape(field_sep)}#{Regexp.escape(segment_sep)}]{#{@min_length},#{@max_length}}" + when /C.*/ : "[^#{Regexp.escape(field_sep)}#{Regexp.escape(segment_sep)}]{#{@min_length},#{@max_length}}" + when /"(.*)"/ : $1 + else "[^#{Regexp.escape(field_sep)}#{Regexp.escape(segment_sep)}]*" end # case end # str_regexp end end