Class: Samovar::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/samovar/table.rb

Overview

Represents a table of parsing rows for a command.

A table manages the collection of options, arguments, and nested commands that define how to parse a command line.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, name: nil) ⇒ Table

Initialize a new table.



28
29
30
31
32
# File 'lib/samovar/table.rb', line 28

def initialize(parent = nil, name: nil)
	@parent = parent
	@name = name
	@rows = {}
end

Class Method Details

.nested(klass, parent = nil) ⇒ Object

Create a nested table that inherits from the parent class’s table.



16
17
18
19
20
21
22
# File 'lib/samovar/table.rb', line 16

def self.nested(klass, parent = nil)
	if klass.superclass.respond_to?(:table)
		parent = klass.superclass.table
	end
	
	self.new(parent, name: klass.name)
end

Instance Method Details

#<<(row) ⇒ Object

Add a row to the table.



63
64
65
66
67
68
69
70
# File 'lib/samovar/table.rb', line 63

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) ⇒ Object

Get a row by key.



49
50
51
# File 'lib/samovar/table.rb', line 49

def [] key
	@rows[key]
end

#each(&block) ⇒ Object

Iterate over each row.



56
57
58
# File 'lib/samovar/table.rb', line 56

def each(&block)
	@rows.each_value(&block)
end

#empty?Boolean

Check if this table is empty.

Returns:

  • (Boolean)


75
76
77
# File 'lib/samovar/table.rb', line 75

def empty?
	@rows.empty? && @parent&.empty?
end

#freezeObject

Freeze this table.



37
38
39
40
41
42
43
# File 'lib/samovar/table.rb', line 37

def freeze
	return self if frozen?
	
	@rows.freeze
	
	super
end

#merge_into(table) ⇒ Object

Merge this table’s rows into another table.



83
84
85
86
87
88
89
90
91
# File 'lib/samovar/table.rb', line 83

def merge_into(table)
	@parent&.merge_into(table)
	
	@rows.each_value do |row|
		table << row
	end
	
	return table
end

#mergedObject

Get a merged table that includes parent rows.



96
97
98
99
100
101
102
# File 'lib/samovar/table.rb', line 96

def merged
	if @parent.nil? or @parent.empty?
		return self
	else
		merge_into(self.class.new)
	end
end

#parse(input, parent) ⇒ Object

Parse the input according to the rows in this table.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/samovar/table.rb', line 115

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

#usageObject

Generate a usage string from all rows.



107
108
109
# File 'lib/samovar/table.rb', line 107

def usage
	@rows.each_value.collect(&:to_s).reject(&:empty?).join(" ")
end