Class X12::Parser
In: lib/X12/Parser.rb
Parent: Object

$Id: Parser.rb 89 2009-05-13 19:36:20Z ikk $

Main class for creating X12 parsers and factories.

Methods

factory   new   parse  

Constants

MS_DEVICES = [ 'CON', 'PRN', 'AUX', 'CLOCK$', 'NUL', 'COM1', 'LPT1', 'LPT2', 'LPT3', 'COM2', 'COM3', 'COM4', ]   These constitute prohibited file names under Microsoft

Public Class methods

Creates a parser out of a definition

[Source]

    # File lib/X12/Parser.rb, line 55
55:     def initialize(file_name)
56:       save_definition = @x12_definition
57: 
58:       # Deal with Microsoft devices
59:       base_name = File.basename(file_name, '.xml')
60:       if MS_DEVICES.find{|i| i == base_name}
61:         file_name = File.join(File.dirname, "#{base_name}_.xml")
62:       end
63:       #puts "Reading definition from #{file_name}"
64: 
65:       # Read and parse the definition
66:       str = File.open(file_name, 'r').read
67:       @dir_name = File.dirname(File.expand_path(file_name)) # to look up other files if needed
68:       @x12_definition = X12::XMLDefinitions.new(str)
69: 
70:       # Populate fields in all segments found in all the loops
71:       @x12_definition[X12::Loop].each_pair{|k, v|
72:         #puts "Populating definitions for loop #{k}"
73:         process_loop(v)
74:       } if @x12_definition[X12::Loop]
75: 
76:       # Merge the newly parsed definition into a saved one, if any.
77:       if save_definition
78:         @x12_definition.keys.each{|t|
79:           save_definition[t] ||= {}
80:           @x12_definition[t].keys.each{|u|
81:             save_definition[t][u] = @x12_definition[t][u] 
82:           }
83:           @x12_definition = save_definition
84:         }
85:       end
86: 
87:       #puts PP.pp(self, '')
88:     end

Public Instance methods

Make an empty loop to be filled out with information

[Source]

     # File lib/X12/Parser.rb, line 101
101:     def factory(loop_name)
102:       loop = @x12_definition[X12::Loop][loop_name]
103:       throw Exception.new("Cannot find a definition for loop #{loop_name}") unless loop
104:       loop = loop.dup
105:       return loop
106:     end

Parse a loop of a given name out of a string. Throws an exception if the loop name is not defined.

[Source]

    # File lib/X12/Parser.rb, line 91
91:     def parse(loop_name, str)
92:       loop = @x12_definition[X12::Loop][loop_name]
93:       #puts "Loops to parse #{@x12_definition[X12::Loop].keys}"
94:       throw Exception.new("Cannot find a definition for loop #{loop_name}") unless loop
95:       loop = loop.dup
96:       loop.parse(str)
97:       return loop
98:     end