Class: Gm::Notepad::InputHandlers::QueryTableHandler
- Inherits:
-
DefaultHandler
- Object
- DefaultHandler
- Gm::Notepad::InputHandlers::QueryTableHandler
- 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>[^\]]+)\])}- WITH_EMPTY_INDEX_REGEX =
%r{(?<declaration>\[\])}- WITH_EMPTY_GREP_REGEX =
%r{(?<declaration>\/\/)}- NON_EXPANDING_CHARATER =
'!'.freeze
Instance Attribute Summary collapse
-
#grep ⇒ Object
Returns the value of attribute grep.
-
#index ⇒ Object
Returns the value of attribute index.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
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
#grep ⇒ Object
Returns the value of attribute grep.
47 48 49 |
# File 'lib/gm/notepad/input_handlers/query_table_handler.rb', line 47 def grep @grep end |
#index ⇒ Object
Returns the value of attribute index.
47 48 49 |
# File 'lib/gm/notepad/input_handlers/query_table_handler.rb', line 47 def index @index end |
#table_name ⇒ Object
Returns the value of attribute table_name.
47 48 49 |
# File 'lib/gm/notepad/input_handlers/query_table_handler.rb', line 47 def table_name @table_name end |
Class Method Details
.handles?(input:) ⇒ 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
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/gm/notepad/input_handlers/query_table_handler.rb', line 22 def after_initialize! self. = false self.to_output = false self.to_interactive = true line = input[1..-1].to_s self. = false if match = WITH_EMPTY_INDEX_REGEX.match(line) line = line.sub(match[:declaration], '') elsif match = WITH_INDEX_REGEXP.match(line) line = line.sub(match[:declaration], '') self.index = match[:index] elsif match = WITH_EMPTY_GREP_REGEX.match(line) line = line.sub(match[:declaration], '') 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 |
#lines ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/gm/notepad/input_handlers/query_table_handler.rb', line 49 def lines begin table = table_registry.fetch_table(name: table_name) rescue KeyError = "Unknown table #{table_name.inspect}. Did you mean: " += table_registry.table_names.grep(/\A#{table_name}/).map(&:inspect).join(", ") return [] end if index begin [table.lookup(index: index)] rescue KeyError [%(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 |