Class: X12::Field

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

Instance Method Summary collapse

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

Parameters:

  • value

    the value to set the attribute content to.



6
7
8
# File 'lib/x12/field.rb', line 6

def content=(value)
  @content = value
end

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

Returns the value of attribute min_length.



5
6
7
# File 'lib/x12/field.rb', line 5

def min_length
  @min_length
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/x12/field.rb', line 5

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



5
6
7
# File 'lib/x12/field.rb', line 5

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/x12/field.rb', line 5

def type
  @type
end

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

Returns:

  • (Boolean)


40
41
42
# File 'lib/x12/field.rb', line 40

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

#inspectString

Returns printable string with field’s content

Returns:

  • (String)


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

#renderObject



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_sObject

Synonym for ‘render’



26
27
28
# File 'lib/x12/field.rb', line 26

def to_s
  render
end