From 2eca6c142778d7be35247a0c47f91d3c3df1f1cc Mon Sep 17 00:00:00 2001 From: mbklein Date: Wed, 1 Apr 2009 18:41:58 +0000 Subject: [PATCH] Added EDI::E::Mapper#finalize method to re-parse the current interchange document git-svn-id: svn://svn.open-ils.org/ILS-Contrib/acq_edi/trunk@240 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- lib/edi/mapper.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/edi/mapper.rb b/lib/edi/mapper.rb index 3400a59ef..463c9a20a 100644 --- a/lib/edi/mapper.rb +++ b/lib/edi/mapper.rb @@ -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 -- 2.11.0