Class X12::Field
In: lib/X12/Field.rb
Parent: Object

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

Methods

Attributes

content  [W] 
max_length  [R] 
min_length  [R] 
name  [R] 
required  [R] 
type  [R] 
validation  [R] 

Public Class methods

Create a new field with given parameters

[Source]

    # File lib/X12/Field.rb, line 35
35:     def initialize(name, type, required, min_length, max_length, validation)
36:       @name       = name       
37:       @type       = type       
38:       @required   = required
39:       @min_length = min_length.to_i
40:       @max_length = max_length.to_i 
41:       @validation = validation
42:       @content = nil
43:     end

Public Instance methods

Check if it‘s been set yet and it‘s not a constant

[Source]

    # File lib/X12/Field.rb, line 63
63:     def has_content?
64:       !@content.nil? && ('"'+@content+'"' != self.type)
65:     end

Returns printable string with field‘s content

[Source]

    # File lib/X12/Field.rb, line 46
46:     def inspect
47:       "Field #{name}|#{type}|#{required}|#{min_length}-#{max_length}|#{validation} <#{@content}>"
48:     end

Returns proper validating string regexp for this field, takes field separator and segment separator as arguments

[Source]

    # File lib/X12/Field.rb, line 81
81:     def proper_regexp(field_sep, segment_sep)
82:       case self.type
83:       when 'I'      : "\\d{#{@min_length},#{@max_length}}"
84:       when 'S'      : "[^#{Regexp.escape(field_sep)}#{Regexp.escape(segment_sep)}]{#{@min_length},#{@max_length}}"
85:       when /C.*/    : "[^#{Regexp.escape(field_sep)}#{Regexp.escape(segment_sep)}]{#{@min_length},#{@max_length}}"
86:       when /"(.*)"/ : $1
87:       else "[^#{Regexp.escape(field_sep)}#{Regexp.escape(segment_sep)}]*"
88:       end # case
89:     end

[Source]

    # File lib/X12/Field.rb, line 55
55:     def render
56:       unless @content
57:         @content = $1 if self.type =~ /"(.*)"/ # If it's a constant
58:       end
59:       @content || ''
60:     end

Erase the content

[Source]

    # File lib/X12/Field.rb, line 68
68:     def set_empty!
69:       @content = nil
70:     end

Returns simplified string regexp for this field, takes field separator and segment separator as arguments

[Source]

    # File lib/X12/Field.rb, line 73
73:     def simple_regexp(field_sep, segment_sep)
74:       case self.type
75:       when /"(.*)"/ : $1
76:       else "[^#{Regexp.escape(field_sep)}#{Regexp.escape(segment_sep)}]*"
77:       end # case
78:     end

Synonym for ‘render

[Source]

    # File lib/X12/Field.rb, line 51
51:     def to_s
52:       render
53:     end