class X12::Field
Class to represent a segment field. Please note, it's not a descendant of Base
.
Attributes
content[W]
max_length[R]
min_length[R]
name[R]
required[R]
type[R]
validation[R]
Public Class Methods
new(name, type, required, min_length, max_length, validation)
click to toggle source
Create a new field with given parameters
# File lib/x12/field.rb, line 9 def initialize(name, type, required, min_length, max_length, validation) @name = name @type = type @required = required @min_length = min_length.to_i @max_length = max_length.to_i @validation = validation @content = nil end
Public Instance Methods
has_content?()
click to toggle source
Check if it's been set yet and it's not a constant
# File lib/x12/field.rb, line 40 def has_content? !@content.nil? && ('"'+@content+'"' != self.type) end
inspect()
click to toggle source
Returns printable string with field's content @return [String]
# File lib/x12/field.rb, line 21 def inspect "Field #{name}|#{type}|#{required}|#{min_length}-#{max_length}|#{validation} <#{@content}>" end
proper_regexp(field_sep, segment_sep)
click to toggle source
Returns proper validating string regexp for this field, takes field separator and segment separator as arguments
# File lib/x12/field.rb, line 58 def proper_regexp(field_sep, segment_sep) case self.type when 'I' then "\\d{#{@min_length},#{@max_length}}" when 'S' then "[^#{Regexp.escape(field_sep)}#{Regexp.escape(segment_sep)}]{#{@min_length},#{@max_length}}" when /C.*/ then "[^#{Regexp.escape(field_sep)}#{Regexp.escape(segment_sep)}]{#{@min_length},#{@max_length}}" when /"(.*)"/ then $1 else "[^#{Regexp.escape(field_sep)}#{Regexp.escape(segment_sep)}]*" end end
render()
click to toggle source
# File lib/x12/field.rb, line 30 def render unless @content @content = $1 if self.type =~ /"(.*)"/ # If it's a constant end rendered = @content || '' rendered = rendered.ljust(@min_length) if @required rendered end
set_empty!()
click to toggle source
Erase the content
# File lib/x12/field.rb, line 45 def set_empty! @content = nil end
simple_regexp(field_sep, segment_sep)
click to toggle source
Returns simplified string regexp for this field, takes field separator and segment separator as arguments
# File lib/x12/field.rb, line 50 def simple_regexp(field_sep, segment_sep) case self.type when /"(.*)"/ then $1 else "[^#{Regexp.escape(field_sep)}#{Regexp.escape(segment_sep)}]*" end # case end
to_s()
click to toggle source
Synonym for 'render'
# File lib/x12/field.rb, line 26 def to_s render end