Class: Gm::Notepad::TableRegistry
- Inherits:
-
Object
- Object
- Gm::Notepad::TableRegistry
- Defined in:
- lib/gm/notepad/table_registry.rb
Overview
Responsible for loading and registering all of the named tables
Instance Attribute Summary collapse
-
#line_evaluator ⇒ Object
readonly
Returns the value of attribute line_evaluator.
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Class Method Summary collapse
Instance Method Summary collapse
- #append(table_name:, line:, write:) ⇒ Object
- #evaluate(line:) ⇒ Object
- #fetch_table(name:) ⇒ Object
- #load! ⇒ Object
- #lookup(table_name:, **kwargs) ⇒ Object
- #register_by_filename(table_name:, filename:) ⇒ Object
- #register_by_string(table_name:, string:) ⇒ Object
- #table_names ⇒ Object
Instance Attribute Details
#line_evaluator ⇒ Object (readonly)
Returns the value of attribute line_evaluator.
29 30 31 |
# File 'lib/gm/notepad/table_registry.rb', line 29 def line_evaluator @line_evaluator end |
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
29 30 31 |
# File 'lib/gm/notepad/table_registry.rb', line 29 def registry @registry end |
Class Method Details
.load_for(config:) ⇒ Object
9 10 11 12 13 |
# File 'lib/gm/notepad/table_registry.rb', line 9 def self.load_for(config:) table_registry = new(config: config) table_registry.load! table_registry end |
Instance Method Details
#append(table_name:, line:, write:) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gm/notepad/table_registry.rb', line 41 def append(table_name:, line:, write:) table = nil begin table = fetch_table(name: table_name) rescue MissingTableError filename = File.join(filesystem_directory, "#{table_name}#{table_extension}") table = register(table_name: table_name, lines: [], filename: filename) end table.append(line: line, write: write) end |
#evaluate(line:) ⇒ Object
73 74 75 |
# File 'lib/gm/notepad/table_registry.rb', line 73 def evaluate(line:) line_evaluator.call(line: line) end |
#fetch_table(name:) ⇒ Object
35 36 37 38 39 |
# File 'lib/gm/notepad/table_registry.rb', line 35 def fetch_table(name:) registry.fetch(name.downcase) rescue KeyError raise MissingTableError.new(name: name.downcase) end |
#load! ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/gm/notepad/table_registry.rb', line 20 def load! paths.each do |path| Dir.glob(File.join(path, "**/*#{table_extension}")).each do |filename| table_name = File.basename(filename, table_extension) register_by_filename(table_name: table_name, filename: filename) end end end |
#lookup(table_name:, **kwargs) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/gm/notepad/table_registry.rb', line 61 def lookup(table_name:, **kwargs) # TODO: Push this onto the table, as it removes nosy neighbor syndrom begin table = fetch_table(name: table_name) table.lookup(**kwargs) rescue MissingTableError "(undefined table_name: #{table_name.inspect})" rescue KeyError "(missing entry for #{kwargs.inspect})" end end |
#register_by_filename(table_name:, filename:) ⇒ Object
52 53 54 55 |
# File 'lib/gm/notepad/table_registry.rb', line 52 def register_by_filename(table_name:, filename:) content = File.read(filename) register(table_name: table_name, lines: content.split("\n"), filename: filename) end |
#register_by_string(table_name:, string:) ⇒ Object
57 58 59 |
# File 'lib/gm/notepad/table_registry.rb', line 57 def register_by_string(table_name:, string:) register(table_name: table_name, lines: string.split("\n")) end |
#table_names ⇒ Object
31 32 33 |
# File 'lib/gm/notepad/table_registry.rb', line 31 def table_names registry.keys.sort end |