class Samovar::Table

Public Class Methods

nested(klass, parent = nil) click to toggle source
# File lib/samovar/table.rb, line 8
def self.nested(klass, parent = nil)
        if klass.superclass.respond_to?(:table)
                parent = klass.superclass.table
        end
        
        self.new(parent, name: klass.name)
end
new(parent = nil, name: nil) click to toggle source
# File lib/samovar/table.rb, line 16
def initialize(parent = nil, name: nil)
        @parent = parent
        @name = name
        @rows = {}
end

Public Instance Methods

<<(row) click to toggle source
# File lib/samovar/table.rb, line 38
def << row
        if existing_row = @rows[row.key] and existing_row.respond_to?(:merge!)
                existing_row.merge!(row)
        else
                # In the above case where there is an existing row, but it doensn't support being merged, we overwrite it. This preserves order.
                @rows[row.key] = row.dup
        end
end
[](key) click to toggle source
# File lib/samovar/table.rb, line 30
def [] key
        @rows[key]
end
each(&block) click to toggle source
# File lib/samovar/table.rb, line 34
def each(&block)
        @rows.each_value(&block)
end
empty?() click to toggle source
# File lib/samovar/table.rb, line 47
def empty?
        @rows.empty? && @parent&.empty?
end
freeze() click to toggle source
Calls superclass method
# File lib/samovar/table.rb, line 22
def freeze
        return self if frozen?
        
        @rows.freeze
        
        super
end
merge_into(table) click to toggle source
# File lib/samovar/table.rb, line 51
def merge_into(table)
        @parent&.merge_into(table)
        
        @rows.each_value do |row|
                table << row
        end
        
        return table
end
merged() click to toggle source
# File lib/samovar/table.rb, line 61
def merged
        if @parent.nil? or @parent.empty?
                return self
        else
                merge_into(self.class.new)
        end
end
parse(input, parent) click to toggle source
# File lib/samovar/table.rb, line 73
def parse(input, parent)
        @rows.each do |key, row|
                next unless row.respond_to?(:parse)
                
                current = parent.send(key)
                
                result = row.parse(input, parent, current)
                if result != nil
                        parent.public_send("#{row.key}=", result)
                end
        end
end
usage() click to toggle source
# File lib/samovar/table.rb, line 69
def usage
        @rows.each_value.collect(&:to_s).reject(&:empty?).join(' ')
end