Class: X12::Field
- Inherits:
-
Object
- Object
- X12::Field
- Defined in:
- lib/x12/field.rb
Overview
$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.
Instance Attribute Summary collapse
-
#content ⇒ Object
writeonly
Sets the attribute content.
-
#max_length ⇒ Object
readonly
Returns the value of attribute max_length.
-
#min_length ⇒ Object
readonly
Returns the value of attribute min_length.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#validation ⇒ Object
readonly
Returns the value of attribute validation.
Instance Method Summary collapse
-
#has_content? ⇒ Boolean
Check if it’s been set yet and it’s not a constant.
-
#initialize(name, type, required, min_length, max_length, validation) ⇒ Field
constructor
Create a new field with given parameters.
-
#inspect ⇒ Object
Returns printable string with field’s content.
-
#proper_regexp(field_sep, segment_sep) ⇒ Object
Returns proper validating string regexp for this field, takes field separator and segment separator as arguments.
- #render ⇒ Object
-
#set_empty! ⇒ Object
Erase the content.
-
#simple_regexp(field_sep, segment_sep) ⇒ Object
Returns simplified string regexp for this field, takes field separator and segment separator as arguments.
-
#to_s ⇒ Object
Synonym for ‘render’.
Constructor Details
#initialize(name, type, required, min_length, max_length, validation) ⇒ Field
Create a new field with given parameters
35 36 37 38 39 40 41 42 43 |
# 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 |
Instance Attribute Details
#content=(value) ⇒ Object (writeonly)
Sets the attribute content
32 33 34 |
# File 'lib/x12/field.rb', line 32 def content=(value) @content = value end |
#max_length ⇒ Object (readonly)
Returns the value of attribute max_length.
31 32 33 |
# File 'lib/x12/field.rb', line 31 def max_length @max_length end |
#min_length ⇒ Object (readonly)
Returns the value of attribute min_length.
31 32 33 |
# File 'lib/x12/field.rb', line 31 def min_length @min_length end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
31 32 33 |
# File 'lib/x12/field.rb', line 31 def name @name end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
31 32 33 |
# File 'lib/x12/field.rb', line 31 def required @required end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
31 32 33 |
# File 'lib/x12/field.rb', line 31 def type @type end |
#validation ⇒ Object (readonly)
Returns the value of attribute validation.
31 32 33 |
# File 'lib/x12/field.rb', line 31 def validation @validation end |
Instance Method Details
#has_content? ⇒ Boolean
Check if it’s been set yet and it’s not a constant
63 64 65 |
# File 'lib/x12/field.rb', line 63 def has_content? !@content.nil? && ('"'+@content+'"' != self.type) end |
#inspect ⇒ Object
Returns printable string with field’s content
46 47 48 |
# 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) ⇒ Object
Returns proper validating string regexp for this field, takes field separator and segment separator as arguments
81 82 83 84 85 86 87 88 89 |
# 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 ⇒ Object
55 56 57 58 59 60 |
# 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! ⇒ Object
Erase the content
68 69 70 |
# File 'lib/x12/field.rb', line 68 def set_empty! @content = nil end |
#simple_regexp(field_sep, segment_sep) ⇒ Object
Returns simplified string regexp for this field, takes field separator and segment separator as arguments
73 74 75 76 77 78 |
# 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 ⇒ Object
Synonym for ‘render’
51 52 53 |
# File 'lib/x12/field.rb', line 51 def to_s render end |