lib/X12/Field.rb in X12-0.0.5 vs lib/X12/Field.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: Field.rb 35 2008-11-13 18:33:44Z ikk $
+ # $Id: Field.rb 56 2009-03-18 19:56:01Z 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
@@ -51,31 +51,35 @@
def to_s
render
end
def render
- # FIXME - this is for debug only
+ unless @content
+ @content = $1 if self.type =~ /"(.*)"/ # If it's a constant
+ end
@content || ''
end # render
- # Check if it's been set yet
+ # Check if it's been set yet and it's not a constant
def has_content?
- !@content.nil?
+ !@content.nil? && ('"'+@content+'"' != self.type)
end
# Erase the content
def set_empty!
@content = nil
end
-# def to_regexp
-# r = case type
-# when 'I' : "\\d{#{min_length},#{max_length}}"
-# when 'S' : ".{#{min_length},#{max_length}}"
-# when /C.*/ : 'composite'
-# when /"(.*)"/ : $1
-# else ''
-# end # case
-# Regexp.new("#{CONSTANT[:field]}#{r}")
-# end
+ # Returns string regexp for this field, takes field separator as an argument
+ def str_regexp(sep)
+ min = required ? @min_length : 0
+ 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 ''
+ end # case
+ end # str_regexp
+
end
end