class EDI::Collection
An EDI
collection instance behaves like a simplified array. In addition, it permits access to its elements through their names. This implies that only objects with a name
may be stored, i.e. derivatives of EDI::Object
.
Public Class Methods
EDI::Object::new
# File lib/edi4r.rb, line 217 def initialize( parent, root, name ) super @a = [] end
Public Instance Methods
# File lib/edi4r.rb, line 251 def ==(obj) self.object_id == obj.object_id end
The element reference operator [] supports two access modes:
- Array-like
-
Return indexed element when passing an integer
- By regexp
-
Return array of element(s) whose name(s) match given regexp
- By name
-
Return array of element(s) whose name(s) match given string
# File lib/edi4r.rb, line 270 def [](i) lookup(i) end
Similar to Array#push(), but automatically setting obj’s parent and root to self and self’s root. Returns obj.
# File lib/edi4r.rb, line 241 def add( obj ) push obj obj.parent = self obj.root = self.root obj end
TO-DO: Experimental add-on Inefficient, brute-force implementation - use sparingly
# File lib/edi4r.rb, line 230 def deep_clone Marshal.restore(Marshal.dump(self)) # TO DO: Make more efficient # c = Marshal.restore(Marshal.dump(self)) # TO DO: Make more efficient # c.each {|obj| obj.parent = c } # c.root = c if c.is_a? EDI::Interchange # c end
# File lib/edi4r.rb, line 258 def each(&b); @a.each(&b); end
# File lib/edi4r.rb, line 259 def find(&b); @a.find(&b); end
# File lib/edi4r.rb, line 260 def find_all(&b); @a.find_all(&b); end
# File lib/edi4r.rb, line 263 def first; @a.first; end
Delegate to array:
index, each, find_all, length, size, first, last
# File lib/edi4r.rb, line 257 def index(obj); @a.index(obj); end
This implementation of +inspect()+ is very verbose in that it inspects all contained objects in a recursive manner.
- indent
-
String offset to use for indentation / pretty-printing
- symlist
-
Array of getter names (passed as symbols) whose values are to be listed. Note that :name is included automatically.
# File lib/edi4r.rb, line 282 def inspect( indent='', symlist=[] ) headline = indent + self.name+': ' + symlist.map do |sym| "#{sym} = #{(s=send(sym)).nil? ? 'nil' : s.to_s}" end.join(', ') + "\n" if self.is_a? Collection_HT headline << @header.inspect(indent+' ') if @header str = @a.inject( headline ){|s,obj| s << obj.inspect(indent+' ')} @trailer ? str << @trailer.inspect(indent+' ') : str else @a.inject( headline ){|s,obj| s << obj.inspect(indent+' ')} end end
# File lib/edi4r.rb, line 264 def last; @a.last; end
# File lib/edi4r.rb, line 262 def length; @a.length; end
Returns an array of names of all included objects in proper sequence; primarily for internal use.
# File lib/edi4r.rb, line 299 def names @a.collect {|e| e.name} end
# File lib/edi4r.rb, line 223 def root= (rt) super( rt ) each {|obj| obj.root = rt } end
# File lib/edi4r.rb, line 261 def size; @a.size; end
Private Instance Methods
# File lib/edi4r.rb, line 330 def lookup(i) if i.is_a?(Integer) @a[i] elsif i.is_a?(Regexp) @a.find_all {|x| x.name =~ i} else @a.find_all {|x| x.name == i} end end
Here we perform the “magic” that provides us with dynamically “generated” getters and setters for just those DE
and CDE
available in the given Collection_S
instance.
UN/EDIFACT examples:
d3055, d1004=(value), cC105, a7174[1].value
ANSI X.12 examples:
d305 = "C" # in segment BPR r01 = "C" # equivalent expression, 01 indicating the first DE
# File lib/edi4r.rb, line 351 def method_missing(sym, *par) if sym.id2name =~ /^([acdrs])(\w+)(=)?/ rc = $1=='r' ? lookup($2.to_i - 1) : lookup($2) if rc.is_a? Array if rc.size==1 rc = rc.first elsif rc.size==0 return super end end if $3 # Setter raise TypeError, "Can't assign to array #$2" if rc.is_a? Array raise TypeError, "Can only assign to a DE value" unless rc.respond_to?(:value) # if $1 != 'd' rc.value = par[0] else # Getter return rc.value if rc.is_a? DE and ($1 == 'd' || $1 == 'r') return rc if rc.is_a? CDE and ($1 == 'c' || $1 == 'r') return rc if rc.is_a? Segment and $1 == 's' err_msg = "Method prefix '#$1' not matching result '#{rc.class}'!" raise TypeError, err_msg unless rc.is_a? Array # Don't let whole DEs be overwritten - enforce usage of "value": rc.freeze return rc if $1 == 'a' raise TypeError,"Array found - use 'a#$2[i]' to access component i" end else super end end
push: Similar to Array#push, except that it requires objects
with getter :name Low-level method, avoid. Use "add" instead.
# File lib/edi4r.rb, line 321 def push( obj ) raise TypeError unless obj.is_a? EDI::Object # obj.respond_to? :name @a << obj end