Class: EDI::E::Mapper
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/edi/mapper.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(ic_opts = {}) ⇒ Mapper
Returns a new instance of Mapper.
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/edi/mapper.rb', line 153
def initialize(ic_opts = {})
local_ic_opts = ic_opts.reject { |k,v| [:sender,:sender_qual,:recipient,:recipient_qual].include?(k) }
@ic = EDI::E::Interchange.new(local_ic_opts || {})
['UNB','UNZ'].each { |seg|
seg_defs = self.class.defaults[seg]
if seg_defs
seg_defs.each_pair { |cde,defs|
segment = @ic.[cde].first
unless segment.nil?
defs.each_pair { |de,val|
segment[de][0].value = val
}
end
}
end
}
@ic..cS002.d0004 = ic_opts[:sender] unless ic_opts[:sender].nil?
@ic..cS002.d0007 = ic_opts[:sender_qual] unless ic_opts[:sender_qual].nil?
@ic..cS003.d0010 = ic_opts[:recipient] unless ic_opts[:recipient].nil?
@ic..cS003.d0007 = ic_opts[:recipient_qual] unless ic_opts[:recipient_qual].nil?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
210
211
212
213
214
215
216
|
# File 'lib/edi/mapper.rb', line 210
def method_missing(sym, *args)
if @ic.respond_to?(sym)
@ic.send(sym, *args)
else
super(sym, *args)
end
end
|
Instance Attribute Details
#defaults ⇒ Object
Returns the value of attribute defaults.
38
39
40
|
# File 'lib/edi/mapper.rb', line 38
def defaults
@defaults
end
|
#message ⇒ Object
Returns the value of attribute message.
37
38
39
|
# File 'lib/edi/mapper.rb', line 37
def message
@message
end
|
Class Method Details
.defaults ⇒ Object
46
47
48
|
# File 'lib/edi/mapper.rb', line 46
def defaults
@defaults || {}
end
|
.defaults=(value) ⇒ Object
50
51
52
53
54
55
|
# File 'lib/edi/mapper.rb', line 50
def defaults=(value)
unless value.is_a?(Hash)
raise TypeError, "Mapper defaults must be in the form of a Hash"
end
@defaults = value
end
|
.find_mapping(name) ⇒ Object
77
78
79
80
81
|
# File 'lib/edi/mapper.rb', line 77
def find_mapping(name)
segment_handlers.find { |h|
h[:re].match(name)
}
end
|
.from_json(json, ic_opts = {}) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/edi/mapper.rb', line 100
def self.from_json(json, ic_opts = {})
struct = Yajl::Parser.parse(json)
json_opts = {}
[:sender,:recipient].each { |party|
party_info = struct[party.to_s]
if party_info.is_a?(Hash)
json_opts[party] = party_info['id']
json_opts["#{party}_qual".to_sym] = party_info['id-qualifier']
elsif party_info.is_a?(String)
(id,qual) = party_info.split(/:/)
json_opts[party] = id
json_opts["#{party}_qual".to_sym] = qual
end
}
json_msg_opts = {}
if struct['msg_opts'].is_a?(Hash)
struct['msg_opts'].each_pair { |k,v| json_msg_opts[k.to_sym] = v }
end
result = self.new(ic_opts.merge(json_opts))
['header','trailer'].each { |envseg|
if struct[envseg]
target = result.send(envseg.to_sym)
struct[envseg].last.each_pair { |de,val|
if val.is_a?(Hash)
val.each_pair { |cde,sval|
target[de][0][cde][0].value = sval
}
else
target[de][0].value = val
end
}
end
}
struct['body'].each { |msg_def|
msg_def.each_pair { |msg_type, msg_body|
if unh = msg_body.find { |s| s[0] == 'UNH' }
version_info = unh[1]['S009']
json_msg_opts[:resp_agency] = version_info['0051']
json_msg_opts[:version] = version_info['0052']
json_msg_opts[:release] = version_info['0054']
end
result.add_message(msg_type, json_msg_opts)
result.add(msg_body)
}
}
result.finalize
end
|
.map(name, expr = nil, &block) ⇒ Object
57
58
59
|
# File 'lib/edi/mapper.rb', line 57
def map(name,expr = nil,&block)
register_mapping(name,expr,block)
end
|
.register_mapping(name, expr, proc) ⇒ Object
61
62
63
64
65
66
67
68
69
|
# File 'lib/edi/mapper.rb', line 61
def register_mapping(name, expr, proc)
if segment_handlers.find { |h| h[:name] == name }
raise NameError, "A pseudo-segment called '#{name}' is already registered"
end
if expr.nil?
expr = Regexp.new("^#{name}$")
end
segment_handlers.push({:name => name,:re => expr,:proc => proc})
end
|
.unregister_mapping(name) ⇒ Object
71
72
73
74
75
|
# File 'lib/edi/mapper.rb', line 71
def unregister_mapping(name)
segment_handlers.delete_if { |h|
h[:name] == name
}
end
|
Instance Method Details
#add(*args) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/edi/mapper.rb', line 185
def add(*args)
if args[0].is_a?(String)
while args.length > 0
add_segment(args.shift, args.shift)
end
elsif args.length == 1 and args[0].is_a?(Array)
add(*args[0])
else
args.each { |arg|
add(arg)
}
end
end
|
#add_message(msg_type, msg_opts = {}) ⇒ Object
180
181
182
183
|
# File 'lib/edi/mapper.rb', line 180
def add_message(msg_type, msg_opts = {})
@message = @ic.new_message( { :msg_type => msg_type, :version => 'D', :release => '96A', :resp_agency => 'UN' }.merge(msg_opts || {}) )
@ic.add(@message,false)
end
|
#apply_mapping(name, value) ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/edi/mapper.rb', line 92
def apply_mapping(name, value)
handler = self.class.find_mapping(name)
if handler.nil?
raise NameError, "Unknown pseudo-segment: '#{name}'"
end
handler[:proc].call(self, name, value)
end
|
#finalize ⇒ Object
199
200
201
202
203
204
|
# File 'lib/edi/mapper.rb', line 199
def finalize
mode = @ic.output_mode
@ic = EDI::E::Interchange.parse(StringIO.new(@ic.to_s))
@ic.output_mode = mode
return self
end
|
#to_s ⇒ Object
206
207
208
|
# File 'lib/edi/mapper.rb', line 206
def to_s
@ic.to_s
end
|