Added EDI::E::Mapper#finalize method to re-parse the current interchange document
authormbklein <mbklein@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Wed, 1 Apr 2009 18:41:58 +0000 (18:41 +0000)
committermbklein <mbklein@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Wed, 1 Apr 2009 18:41:58 +0000 (18:41 +0000)
git-svn-id: svn://svn.open-ils.org/ILS-Contrib/acq_edi/trunk@240 6d9bc8c9-1ec2-4278-b937-99fde70a366f

lib/edi/mapper.rb

index 3400a59..463c9a2 100644 (file)
@@ -1,5 +1,6 @@
 require 'edi4r'
 require 'edi4r/edifact'
+require 'forwardable'
 require 'json'
 
 class String
@@ -14,9 +15,13 @@ end
 module EDI::E
 
   class Mapper
+    extend Forwardable
     
     attr :message
     attr_accessor :defaults
+    def_delegators :@ic, :charset, :groups_created, :inspect, :is_iedi?, 
+      :messages_created, :output_mode=, :show_una, :show_una=, :to_s, 
+      :to_xml, :una, :validate
     
     class << self
       def defaults
@@ -73,14 +78,12 @@ module EDI::E
       handler[:proc].call(self, name, value)
     end
     
-    @segments = []
-    
     def self.from_json(msg_type, json, msg_opts = {}, ic_opts = {})
       result = self.new(msg_type, msg_opts, ic_opts)
       result.add(JSON.parse(json))
-      result
+      result.finalize
     end
-    
+
     def initialize(msg_type, msg_opts = {}, ic_opts = {})
       @ic = EDI::E::Interchange.new(ic_opts || {})
       @message = @ic.new_message( { :msg_type => msg_type, :version => 'D', :release => '96A', :resp_agency => 'UN' }.merge(msg_opts || {}) )
@@ -101,6 +104,13 @@ module EDI::E
       end
     end
     
+    def finalize
+      mode = @ic.output_mode
+      @ic = EDI::E::Interchange.parse(StringIO.new(@ic.to_s))
+      @ic.output_mode = mode
+      return self
+    end
+    
     def to_s
       @ic.to_s
     end