class Gm::Notepad::Table
Constants
- STARTS_WITH_COMMENT_REGEXP
Public Class Methods
new(*args)
click to toggle source
Calls superclass method
# File lib/gm/notepad/table.rb, line 16 def initialize(*args) super @table = {} @or_less = [] @or_more = [] set_null_table_column_set! process(lines: lines) end
Public Instance Methods
all()
click to toggle source
# File lib/gm/notepad/table.rb, line 37 def all @table.values.uniq end
append(line:, write: false)
click to toggle source
# File lib/gm/notepad/table.rb, line 59 def append(line:, write: false) process(lines: [line]) return unless filename return unless write File.open(filename, "a") do |file| file.puts(line) end end
column_index_for(cell:)
click to toggle source
# File lib/gm/notepad/table.rb, line 45 def column_index_for(cell:) @table_column_set.column_index_for(cell: cell) end
column_names()
click to toggle source
# File lib/gm/notepad/table.rb, line 41 def column_names @table_column_set.names end
grep(expression)
click to toggle source
# File lib/gm/notepad/table.rb, line 49 def grep(expression) returning_value = [] @table.each_value do |entry| if expression.match(entry.entry_column) returning_value << entry end end returning_value end
lookup(index: false, cell: false)
click to toggle source
# File lib/gm/notepad/table.rb, line 25 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 lookup_random_entry end end
set_or_less_entry(table_entry)
click to toggle source
# File lib/gm/notepad/table.rb, line 68 def set_or_less_entry(table_entry) @or_less = table_entry end
set_or_more_entry(table_entry)
click to toggle source
# File lib/gm/notepad/table.rb, line 72 def set_or_more_entry(table_entry) @or_more = table_entry end
Private Instance Methods
lookup_entry_by(index:)
click to toggle source
# File lib/gm/notepad/table.rb, line 78 def lookup_entry_by(index:) begin @table.fetch(index.to_s) rescue KeyError if @or_less.include?(index.to_s) @or_less elsif @or_more.include?(index.to_s) @or_more else raise MissingTableEntryError.new(table_name: table_name, index: index.to_s) end end end
lookup_random_entry()
click to toggle source
# File lib/gm/notepad/table.rb, line 92 def lookup_random_entry @table.values[random_index] end
make_entry!(line:)
click to toggle source
# File lib/gm/notepad/table.rb, line 124 def make_entry!(line:) entry = TableEntry.new(table: self, line: line) 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 end
process(lines:)
click to toggle source
# File lib/gm/notepad/table.rb, line 101 def process(lines:) lines.each do |line| line = line.strip # Handle Comment case line when STARTS_WITH_COMMENT_REGEXP next when @index_entry_prefix_regexp register_index_declaration!(line: line) else make_entry!(line: line) end end end
random_index()
click to toggle source
# File lib/gm/notepad/table.rb, line 96 def random_index rand(@table.size) end
register_index_declaration!(line:)
click to toggle source
# File lib/gm/notepad/table.rb, line 120 def register_index_declaration!(line:) @table_column_set = TableColumnSet.new(table: self, line: line) end
set_null_table_column_set!()
click to toggle source
# File lib/gm/notepad/table.rb, line 116 def set_null_table_column_set! @table_column_set = TableColumnSet::Null.new end