Class: Gm::Notepad::TableRegistry

Inherits:
Object
  • Object
show all
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

Instance Attribute Details

#line_evaluatorObject (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

#registryObject (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



68
69
70
# File 'lib/gm/notepad/table_registry.rb', line 68

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
# File 'lib/gm/notepad/table_registry.rb', line 61

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



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_namesObject



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

def table_names
  registry.keys.sort
end