Class: X12::Field

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • value

    the value to set the attribute content to.



32
33
34
# File 'lib/x12/field.rb', line 32

def content=(value)
  @content = value
end

#max_lengthObject (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_lengthObject (readonly)

Returns the value of attribute min_length.



31
32
33
# File 'lib/x12/field.rb', line 31

def min_length
  @min_length
end

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/x12/field.rb', line 31

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



31
32
33
# File 'lib/x12/field.rb', line 31

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type.



31
32
33
# File 'lib/x12/field.rb', line 31

def type
  @type
end

#validationObject (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

Returns:

  • (Boolean)


63
64
65
# File 'lib/x12/field.rb', line 63

def has_content?
  !@content.nil? && ('"'+@content+'"' != self.type)
end

#inspectObject

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

#renderObject



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_sObject

Synonym for ‘render’



51
52
53
# File 'lib/x12/field.rb', line 51

def to_s
  render
end