lib/gm/notepad/table.rb in gm-notepad-0.0.6 vs lib/gm/notepad/table.rb in gm-notepad-0.0.8
- old
+ new
@@ -6,19 +6,19 @@
class Table
Configuration.init!(target: self, additional_params: [:table_name, :filename, :lines]) do
process(lines: lines)
end
- def lookup(index: false)
- if index
- begin
- @table.fetch(index.to_s)
- rescue KeyError
- raise MissingTableEntryError.new(table_name: table_name, index: index.to_s)
- end
+ def lookup(index: false, cell: false)
+ if index && cell
+ lookup_entry_by(index: index).lookup(cell: cell)
+ elsif index
+ lookup_entry_by(index: index)
+ elsif cell
+ lookup_random_entry.lookup(cell: cell)
else
- @table.values[random_index]
+ lookup_random_entry
end
end
def all
@table.values.uniq
@@ -43,10 +43,22 @@
end
end
private
+ def lookup_entry_by(index:)
+ begin
+ @table.fetch(index.to_s)
+ rescue KeyError
+ raise MissingTableEntryError.new(table_name: table_name, index: index.to_s)
+ end
+ end
+
+ def lookup_random_entry
+ @table.values[random_index]
+ end
+
attr_accessor :filename, :config
attr_reader :table_name
def table_name=(input)
@table_name = input.downcase
@@ -58,10 +70,10 @@
def process(lines:)
@table = {}
lines.each do |line|
next if line[0] == '#'
- entry = TableEntry.new(line: line, **config)
+ entry = TableEntry.new(line: line, config: config)
entry.lookup_range.each do |i|
key = i.to_s
raise DuplicateKeyError.new(key: table_name, object: self) if @table.key?(key)
@table[key] = entry
end