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

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

Constant Summary collapse

QUERY_TABLE_NAMES_PREFIX =
'+'.freeze
WITH_GREP_REGEXP =
%r{(?<declaration>\/(?<grep>[^\/]+)/)}
WITH_INDEX_REGEXP =
%r{(?<declaration>\[(?<index>[^\]]+)\])}
NON_EXPANDING_CHARATER =
'!'.freeze

Instance Attribute Summary collapse

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

Instance Attribute Details

#grepObject

Returns the value of attribute grep.



42
43
44
# File 'lib/gm/notepad/input_handlers/query_table_handler.rb', line 42

def grep
  @grep
end

#indexObject

Returns the value of attribute index.



42
43
44
# File 'lib/gm/notepad/input_handlers/query_table_handler.rb', line 42

def index
  @index
end

#table_nameObject

Returns the value of attribute table_name.



42
43
44
# File 'lib/gm/notepad/input_handlers/query_table_handler.rb', line 42

def table_name
  @table_name
end

Class Method Details

.handles?(input:) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
# File 'lib/gm/notepad/input_handlers/query_table_handler.rb', line 7

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
29
30
31
32
33
34
35
36
37
38
39
40
# 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
  if match = WITH_INDEX_REGEXP.match(line)
    line = line.sub(match[:declaration], '')
    index = match[:index]
    self.index = index
  elsif match = WITH_GREP_REGEXP.match(line)
    line = line.sub(match[:declaration], '')
    grep = match[:grep]
    self.grep = grep
  end
  if line[-1] == NON_EXPANDING_CHARATER
    line = line[0..-2]
  end
  self.table_name = line.downcase
end

#linesObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gm/notepad/input_handlers/query_table_handler.rb', line 44

def lines
  begin
    table = table_registry.fetch_table(name: table_name)
  rescue KeyError
    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
    [table.lookup(index: index)]
  elsif grep
    regexp = %r{#{grep}}i
    table.grep(regexp)
  else
    table.all
  end
end