Class: X12::Loop

Inherits:
Base
  • Object
show all
Defined in:
lib/x12/loop.rb

Overview

Implements nested loops of segments.

Instance Attribute Summary

Attributes inherited from Base

#composite_separator, #field_separator, #name, #next_repeat, #nodes, #parsed_str, #repeats, #segment_separator

Instance Method Summary collapse

Methods inherited from Base

#[], #do_repeats, #dup, #find, #has_content?, #initialize, #method_missing, #repeat, #set_empty!, #show, #size, #to_a, #to_s, #with

Constructor Details

This class inherits a constructor from X12::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class X12::Base

Instance Method Details

#eachObject

Provides looping through repeats of a message.



60
61
62
63
64
65
# File 'lib/x12/loop.rb', line 60

def each
  res = self.to_a
  0.upto(res.length - 1) do |x|
    yield res[x]
  end
end

#inspectString

Formats a printable string containing the loops element’s content added to provide compatability with ruby > 2.0.0.

Returns:

  • (String)


55
56
57
# File 'lib/x12/loop.rb', line 55

def inspect
  "#{self.class.to_s.sub(/^.*::/, '')} (#{name}) #{repeats} =<#{parsed_str}, #{next_repeat.inspect}> ".gsub(/\\*\"/, '"')
end

#parse(str) ⇒ Object

Parse a string and fill out internal structures with the pieces of it. Returns an unparsed portion of the string or the original string if nothing was parsed out.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/x12/loop.rb', line 21

def parse(str)
  # puts "Parsing loop #{name}: "+str
  s = str
  nodes.each{|i|
    m = i.parse(s)
    s = m if m
  }
  if str == s
    return nil
  else
    self.parsed_str = str[0..-s.length-1]
    s = do_repeats(s)
  end
  # puts 'Parsed loop '+self.inspect
  return s
end

#renderObject

Render all components of this loop as string suitable for EDI.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/x12/loop.rb', line 39

def render
  if self.has_content?
    self.to_a.inject(''){|loop_str, i|
      loop_str += i.nodes.inject(''){|nodes_str, j|
        nodes_str += j.render
      }
    }
  else
    ''
  end
end