Class: X12::Field
- Inherits:
-
Object
- Object
- X12::Field
- Defined in:
- lib/x12/field.rb
Overview
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 ⇒ String
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
9 10 11 12 13 14 15 16 17 |
# 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 |
Instance Attribute Details
#content=(value) ⇒ Object (writeonly)
Sets the attribute content
6 7 8 |
# File 'lib/x12/field.rb', line 6 def content=(value) @content = value end |
#max_length ⇒ Object (readonly)
Returns the value of attribute max_length.
5 6 7 |
# File 'lib/x12/field.rb', line 5 def max_length @max_length end |
#min_length ⇒ Object (readonly)
Returns the value of attribute min_length.
5 6 7 |
# File 'lib/x12/field.rb', line 5 def min_length @min_length end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/x12/field.rb', line 5 def name @name end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
5 6 7 |
# File 'lib/x12/field.rb', line 5 def required @required end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/x12/field.rb', line 5 def type @type end |
#validation ⇒ Object (readonly)
Returns the value of attribute validation.
5 6 7 |
# File 'lib/x12/field.rb', line 5 def validation @validation end |
Instance Method Details
#has_content? ⇒ Boolean
Check if it’s been set yet and it’s not a constant
40 41 42 |
# File 'lib/x12/field.rb', line 40 def has_content? !@content.nil? && ('"'+@content+'"' != self.type) end |
#inspect ⇒ String
Returns printable string with field’s content
21 22 23 |
# 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) ⇒ Object
Returns proper validating string regexp for this field, takes field separator and segment separator as arguments
58 59 60 61 62 63 64 65 66 |
# 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 ⇒ Object
30 31 32 33 34 35 36 37 |
# 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! ⇒ Object
Erase the content
45 46 47 |
# File 'lib/x12/field.rb', line 45 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
50 51 52 53 54 55 |
# 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 ⇒ Object
Synonym for ‘render’
26 27 28 |
# File 'lib/x12/field.rb', line 26 def to_s render end |