Class: Gm::Notepad::TableRegistry

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/gm/notepad/table_registry.rb

Overview

Responsible for loading and registering all of the named tables

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ TableRegistry

Returns a new instance of TableRegistry.



22
23
24
25
# File 'lib/gm/notepad/table_registry.rb', line 22

def initialize(*args, &block)
  super
  instance_exec(&block) if block_given?
end

Class Method Details

.build_and_loadObject



11
12
13
# File 'lib/gm/notepad/table_registry.rb', line 11

def self.build_and_load
  new { load! }
end

Instance Method Details

#append(table_name:, line:, write:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/gm/notepad/table_registry.rb', line 37

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



64
65
66
# File 'lib/gm/notepad/table_registry.rb', line 64

def evaluate(line:)
  line_evaluator.call(line: line)
end

#fetch_table(name:) ⇒ Object



31
32
33
34
35
# File 'lib/gm/notepad/table_registry.rb', line 31

def fetch_table(name:)
  registry.fetch(name.downcase)
rescue KeyError
  raise MissingTableError.new(name: name.downcase)
end

#lookup(table_name:, **kwargs) ⇒ Object



57
58
59
60
61
62
# File 'lib/gm/notepad/table_registry.rb', line 57

def lookup(table_name:, **kwargs)
  table = fetch_table(name: table_name)
  table.lookup(**kwargs)
rescue MissingTableError, MissingTableEntryError => e
  e.to_buffer_message
end

#register_by_filename(table_name:, filename:) ⇒ Object



48
49
50
51
# File 'lib/gm/notepad/table_registry.rb', line 48

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



53
54
55
# File 'lib/gm/notepad/table_registry.rb', line 53

def register_by_string(table_name:, string:)
  register(table_name: table_name, lines: string.split("\n"))
end

#table_namesObject



27
28
29
# File 'lib/gm/notepad/table_registry.rb', line 27

def table_names
  registry.keys.sort
end