class X12::Field

$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.

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 35
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 63
def has_content?
  !@content.nil? && ('"'+@content+'"' != self.type)
end
inspect() click to toggle source

Returns printable string with field's content

# File lib/x12/field.rb, line 46
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 81
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 # case
end
render() click to toggle source
# File lib/x12/field.rb, line 55
def render
  unless @content
    @content = $1 if self.type =~ /"(.*)"/ # If it's a constant
  end
  @content || ''
end
set_empty!() click to toggle source

Erase the content

# File lib/x12/field.rb, line 68
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 73
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 51
def to_s
  render
end