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

Instance Attribute Summary collapse

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

Instance Attribute Details

#line_evaluatorObject (readonly)

Returns the value of attribute line_evaluator.



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

def line_evaluator
  @line_evaluator
end

#registryObject (readonly)

Returns the value of attribute registry.



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

def registry
  @registry
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



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

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



66
67
68
# File 'lib/gm/notepad/table_registry.rb', line 66

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

#fetch_table(name:) ⇒ Object



33
34
35
36
37
# File 'lib/gm/notepad/table_registry.rb', line 33

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

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



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

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



50
51
52
53
# File 'lib/gm/notepad/table_registry.rb', line 50

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



55
56
57
# File 'lib/gm/notepad/table_registry.rb', line 55

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

#table_namesObject



29
30
31
# File 'lib/gm/notepad/table_registry.rb', line 29

def table_names
  registry.keys.sort
end