lib/x12/base.rb in tcd_x12-1.6.2 vs lib/x12/base.rb in tcd_x12-1.6.3
- old
+ new
@@ -1,15 +1,18 @@
module X12
- # Base class for Segment, Composite, and Loop.
+ # Base class for {Segment}, {Composite}, and {Loop}.
# Contains setable segment_separator, field_separator, and composite_separator fields.
class Base
attr_reader :name, :repeats
- attr_accessor :segment_separator, :field_separator, :composite_separator, :next_repeat, :parsed_str, :nodes
+ attr_accessor :segment_separator, :field_separator, :composite_separator
+ attr_accessor :next_repeat, :parsed_str, :nodes
# Creates a new base element with a given name, array of sub-elements, and array of repeats if any.
+ #
+ # @return [void]
def initialize(name, arr, repeats = nil)
@nodes = arr
@name = name
@repeats = repeats
@next_repeat = nil # Next repeat of the same element, if any
@@ -21,16 +24,19 @@
# puts "Created #{name} #{object_id} #{self.class} "
end
# Formats a printable string containing the base element's content.
+ #
# @return [String]
def inspect
"#{self.class.to_s.sub(/^.*::/, '')} (#{name}) #{repeats} #{super.inspect[1..-2]} =<#{parsed_str}, #{next_repeat.inspect}> ".gsub(/\\*\"/, '"')
end
# Prints a tree-like representation of the element.
+ #
+ # @return [void]
def show(ind = '')
count = 0
self.to_a.each{|i|
# puts "#{ind}#{i.name} #{i.object_id} #{i.super.object_id} [#{count}]: #{i.parsed_str} #{i.super.class}"
puts "#{ind}#{i.name} [#{count}]: #{i.to_s.sub(/^(.{30})(.*?)(.{30})$/, '\1...\3')}"
@@ -46,12 +52,12 @@
}
count += 1
}
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.
+ # 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.
def do_repeats(s)
if self.repeats.end > 1
possible_repeat = self.dup
p_s = possible_repeat.parse(s)
if p_s
@@ -61,10 +67,12 @@
end
s
end
# Empty out the current element.
+ #
+ # @return [void]
def set_empty!
@next_repeat = nil
@parsed_str = nil
self
end
@@ -168,10 +176,11 @@
# Check if any of the fields has been set yet.
def has_content?
self.nodes.find{ |i| i.has_content? }
end
- # Adds a repeat to a segment or loop. Returns a new segment/loop or self if empty.
+ # Adds a repeat to a segment or loop.
+ # Returns a new segment/loop or self if empty.
def repeat
res = if self.has_content? # Do not repeat an empty segment
last_repeat = self.to_a[-1]
last_repeat.next_repeat = last_repeat.dup
else