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, #parse, #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



88
89
90
91
92
93
# File 'lib/x12/loop.rb', line 88

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

#inspectObject

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



82
83
84
# File 'lib/x12/loop.rb', line 82

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

#parse_helper(str, yield_loop_name, block) ⇒ 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.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/x12/loop.rb', line 46

def parse_helper(str, yield_loop_name, block)
  # puts "#{$count}: parse for #{name}" if name == yield_loop_name

  $count ||=0
  # $count+=1 if name == yield_loop_name

  # return nil if name == yield_loop_name && (($count+=1) > 2)

  #puts "Parsing loop #{name}: "+str

  current_location = str
  nodes.each{|node|
    match_location = node.parse(current_location, yield_loop_name, block)
    current_location = match_location if match_location
  } 
  if str == current_location
    return nil
  else
    self.parsed_str = str[0..-current_location.length-1]
    block.call(self) if yield_loop_name && name == yield_loop_name
    return current_location
  end
end

#renderObject

Render all components of this loop as string suitable for EDI



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/x12/loop.rb', line 67

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