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

$Id: Base.rb 35 2008-11-13 18:33:44Z ikk $

Base class for Segment, Composite, and Loop. Contains setable segment_separator, field_separator, and composite_separator fields.

Methods

[]   do_repeats   dup   find   has_content?   inspect   method_missing   new   repeat   set_empty!   show   size   to_a   to_s  

Attributes

composite_separator  [W] 
composite_separator  [R] 
field_separator  [W] 
field_separator  [R] 
name  [R] 
next_repeat  [R] 
next_repeat  [W] 
nodes  [R] 
nodes  [W] 
parsed_str  [R] 
parsed_str  [W] 
repeats  [R] 
segment_separator  [R] 
segment_separator  [W] 

Public Class methods

Creates a new base element with a given name, array of sub-elements, and array of repeats if any.

[Source]

    # File lib/X12/Base.rb, line 38
38:     def initialize(name, arr, repeats = nil)
39:       @nodes = arr
40:       @name = name
41:       @repeats = repeats
42:       @next_repeat = nil        # Next repeat of the same element, if any
43:       @parsed_str = nil
44: 
45:       @segment_separator   = '~'
46:       @field_separator     = '*'
47:       @composite_separator = ':'
48: 
49:       #puts "Created #{name} #{object_id} #{self.class}  "
50:     end

Public Instance methods

The main method implementing Ruby-like access methods for repeating elements

[Source]

     # File lib/X12/Base.rb, line 164
164:     def [](*args)
165:       #puts "squares #{args.inspect}"
166:       return self.to_a[args[0]] || EMPTY
167:     end

Try to parse the current element one more time if required. Returns the rest of the string or the same string if no more repeats are found or required.

[Source]

    # File lib/X12/Base.rb, line 75
75:     def do_repeats(s)
76:       if self.repeats.end > 1
77:         possible_repeat = self.dup
78:         p_s = possible_repeat.parse(s)
79:         if p_s
80:           s = p_s
81:           self.next_repeat = possible_repeat
82:         end # if parsed
83:       end # more repeats
84:       s
85:     end

Make a deep copy of the element

[Source]

     # File lib/X12/Base.rb, line 95
 95:     def dup
 96:       n = clone
 97:       n.set_empty!
 98:       n.nodes = n.nodes.dup
 99:       n.nodes.each_index{|i|
100:         n.nodes[i] = n.nodes[i].dup
101:         n.nodes[i].set_empty!
102:       }
103:       #puts "Duped #{self.class} #{self.name} #{self.object_id} #{super.object_id} -> #{n.name} #{n.super.object_id} #{n.object_id} "
104:       n
105:     end

Recursively find a sub-element, which also has to be of type Base.

[Source]

     # File lib/X12/Base.rb, line 108
108:     def find(e)
109:       #puts "Finding [#{e}] in #{self.class} #{name}"
110:       case self
111:         when X12::Loop
112:         nodes.each{|i| 
113:           return i if e==i.name
114:           res = i.find(e) if i.kind_of?(X12::Loop)
115:           return res unless res.nil? or EMPTY==res # otherwise keep looping
116:         }
117:         when X12::Segment
118:         return find_field(e).to_s
119:       end # case
120:       return EMPTY
121:     end

Check if any of the fields has been set yet

[Source]

     # File lib/X12/Base.rb, line 175
175:     def has_content?
176:       self.nodes.find{|i| i.has_content?}
177:     end

Formats a printable string containing the base element‘s content

[Source]

    # File lib/X12/Base.rb, line 53
53:     def inspect
54:       "#{self.class.to_s.sub(/^.*::/, '')} (#{name}) #{repeats} #{super.inspect[1..-2]} =<#{parsed_str}, #{next_repeat.inspect}> ".gsub(/\\*\"/, '"')
55:     end

The main method implementing Ruby-like access methods for nested elements

[Source]

     # File lib/X12/Base.rb, line 140
140:     def method_missing(meth, *args, &block)
141:       str = meth.id2name
142:       if str =~ /=$/
143:         # Assignment
144:         str.chop!
145:         #puts str
146:         case self
147:         when X12::Segment
148:           res = find_field(str)
149:           throw Exception.new("No field '#{str}' in segment '#{self.name}'") if EMPTY == res
150:           res.content = args[0].to_s
151:           #puts res.inspect
152:         else
153:           throw Exception.new("Illegal assignment to #{meth} of #{self.class}")
154:         end # case
155:       else
156:         # Retrieval
157:         res = find(str)
158:         yield res if block_given?
159:         res
160:       end # if assignment
161:     end

Adds a repeat to a segment or loop. Returns a new segment/loop or self if empty.

[Source]

     # File lib/X12/Base.rb, line 180
180:     def repeat
181:       res = if self.has_content? # Do not repeat an empty segment
182:               last_repeat = self.to_a[-1]
183:               last_repeat.next_repeat = last_repeat.dup
184:             else
185:               self
186:             end
187:       yield res if block_given?
188:       res
189:     end

Empty out the current element

[Source]

    # File lib/X12/Base.rb, line 88
88:     def set_empty!
89:       @next_repeat = nil
90:       @parsed_str = nil
91:       self
92:     end

Prints a tree-like representation of the element

[Source]

    # File lib/X12/Base.rb, line 58
58:     def show(ind = '')
59:       count = 0
60:       self.to_a.each{|i|
61:         #puts "#{ind}#{i.name} #{i.object_id} #{i.super.object_id} [#{count}]: #{i.parsed_str} #{i.super.class}"
62:         puts "#{ind}#{i.name} [#{count}]: #{i.to_s.sub(/^(.{30})(.*?)(.{30})$/, '\1...\3')}"
63:         i.nodes.each{|j|
64:           case 
65:           when j.kind_of?(X12::Base)  : j.show(ind+'  ')
66:           when j.kind_of?(X12::Field) : puts "#{ind+'  '}#{j.name} -> '#{j.to_s}'"
67:           end
68:         } 
69:         count += 1
70:       }
71:     end

Returns number of repeats

[Source]

     # File lib/X12/Base.rb, line 170
170:     def size
171:       return self.to_a.size
172:     end

Present self and all repeats as an array with self being 0

[Source]

     # File lib/X12/Base.rb, line 124
124:     def to_a
125:       res = [self]
126:       nr = self.next_repeat
127:       while nr do
128:         res << nr
129:         nr = nr.next_repeat
130:       end
131:       res
132:     end

Returns a parsed string representation of the element

[Source]

     # File lib/X12/Base.rb, line 135
135:     def to_s
136:       @parsed_str || ''
137:     end