Class: Gm::Notepad::InputHandlers::QueryTableHandler

Inherits:
DefaultHandler show all
Extended by:
Forwardable
Defined in:
lib/gm/notepad/input_handlers/query_table_handler.rb

Constant Summary collapse

QUERY_TABLE_NAMES_PREFIX =
'+'.freeze

Instance Attribute Summary

Attributes inherited from DefaultHandler

#expand_line, #input, #table_registry, #to_filesystem, #to_interactive, #to_output

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DefaultHandler

build_if_handled, #each_line_with_parameters, #initialize

Constructor Details

This class inherits a constructor from Gm::Notepad::InputHandlers::DefaultHandler

Class Method Details

.handles?(input:) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
# File 'lib/gm/notepad/input_handlers/query_table_handler.rb', line 10

def self.handles?(input:)
  # Does not have the table prefix
  return false unless input[0] == QUERY_TABLE_NAMES_PREFIX
  # It is only the table prefix
  return false if input == QUERY_TABLE_NAMES_PREFIX
  # It is querying all tables by way of grep
  return false if input[0..1] == "#{QUERY_TABLE_NAMES_PREFIX}/"
  true
end

Instance Method Details

#after_initialize!Object



20
21
22
23
24
25
26
27
28
# File 'lib/gm/notepad/input_handlers/query_table_handler.rb', line 20

def after_initialize!
  self.expand_line = false
  self.to_output = false
  self.to_interactive = true

  line = input[1..-1].to_s
  self.expand_line = false
  @table_lookup_parameters = Parameters::TableLookup.new(text: line)
end

#linesObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gm/notepad/input_handlers/query_table_handler.rb', line 33

def lines
  begin
    table = table_registry.fetch_table(name: table_name)
  rescue MissingTableError
    message = "Unknown table #{table_name.inspect}. Did you mean: "
    message += table_registry.table_names.grep(/\A#{table_name}/).map(&:inspect).join(", ")
    return [message]
  end
  if index
    begin
      [table.lookup(index: index)]
    rescue MissingTableEntryError
      [%(Entry with index "#{index}" not found in "#{table_name}" table)]
    end
  elsif grep
    regexp = %r{#{grep}}i
    table.grep(regexp)
  else
    table.all
  end
end