Class: MarkdownServer::CsvBrowser::CsvAddonRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_server/csv_browser/addon_registry.rb

Overview

Global registry of CSV add-ons.

Lifecycle:

  1. A Ruby file calls ‘MarkdownServer::CsvAddonRegistry.register(:name) { … }` at load time. That block is `instance_exec`’d against a fresh ‘AddonDefinition`, populating its `actions` and `on` entries.

  2. At startup, ‘load(addons_config, root_dir)` receives the `csv_addons` mapping from .markdownr.yml and `require`s each absolute path; missing files warn but don’t halt startup.

  3. ‘for_table(db, table)` returns the definitions a given table has attached via its `addons:` list; unknown add-on names warn and are dropped.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.definitionsObject (readonly)

Returns the value of attribute definitions.



59
60
61
# File 'lib/markdown_server/csv_browser/addon_registry.rb', line 59

def definitions
  @definitions
end

.loaded_configObject (readonly)

Returns the value of attribute loaded_config.



59
60
61
# File 'lib/markdown_server/csv_browser/addon_registry.rb', line 59

def loaded_config
  @loaded_config
end

Class Method Details

.[](name) ⇒ Object



73
74
75
# File 'lib/markdown_server/csv_browser/addon_registry.rb', line 73

def [](name)
  @definitions[name.to_sym]
end

.for_table(_database, table) ⇒ Object

Returns an array of AddonDefinition instances attached to the table. Unknown names (declared on the table but not registered) produce a one-time warning per (table, name) and are skipped.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/markdown_server/csv_browser/addon_registry.rb', line 98

def for_table(_database, table)
  attached = Array(table.addons).map { |entry| normalize_attachment(entry) }
  attached.filter_map do |att|
    defn = @definitions[att[:name]]
    unless defn
      key = [table.key, att[:name]]
      @warned_missing ||= {}
      unless @warned_missing[key]
        $stderr.puts "\n\e[1;33mWarning: table '#{table.key}' references unknown csv_addon '#{att[:name]}'\e[0m\n\n"
        @warned_missing[key] = true
      end
      next
    end
    { definition: defn, options: att[:options] }
  end
end

.load(addons_config, _root_dir = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/markdown_server/csv_browser/addon_registry.rb', line 77

def load(addons_config, _root_dir = nil)
  @loaded_config = (addons_config || {}).each_with_object({}) do |(k, v), h|
    h[k.to_sym] = v
  end

  @loaded_config.each do |name, path|
    unless File.exist?(path.to_s)
      $stderr.puts "\n\e[1;33mWarning: csv_addon '#{name}' file not found: #{path}\e[0m\n\n"
      next
    end
    # Use Kernel#load (not require) so repeated calls re-evaluate the
    # add-on file — important after `reset!` in tests and to let users
    # refresh their add-on without restarting (the gate is the
    # absolute path in .markdownr.yml).
    Kernel.load(path.to_s)
  end
end

.register(name, &block) ⇒ Object



66
67
68
69
70
71
# File 'lib/markdown_server/csv_browser/addon_registry.rb', line 66

def register(name, &block)
  defn = AddonDefinition.new(name)
  defn.instance_exec(&block) if block
  @definitions[name.to_sym] = defn
  defn
end

.reset!Object



61
62
63
64
# File 'lib/markdown_server/csv_browser/addon_registry.rb', line 61

def reset!
  @definitions = {}
  @loaded_config = {}
end