Module: Emjay::Registry

Defined in:
lib/emjay/registry.rb

Overview

Lazy autoloader for MJML components. Scans lib/emjay/components/**/*.rb once at load time, then derives both the MJML tag name and the Ruby class from each filename:

components/body/mj_button.rb  →  tag "mj-button"  →  Emjay::Components::MjButton

Files are required and constants resolved on first find.

Defined Under Namespace

Classes: LazyComponents, UnknownComponent

Constant Summary collapse

COMPONENTS_ROOT =
File.expand_path("components", __dir__)
PATHS =
Dir.glob("{head,body}/*.rb", base: COMPONENTS_ROOT).sort.each_with_object({}) do |rel, h|
  basename = File.basename(rel, ".rb") # e.g. "mj_html_attributes"
  tag = basename.tr("_", "-")           # → "mj-html-attributes"
  h[tag] = File.join(COMPONENTS_ROOT, rel)
end.freeze

Class Method Summary collapse

Class Method Details

.componentsObject



29
30
31
# File 'lib/emjay/registry.rb', line 29

def self.components
  @components ||= LazyComponents.new
end

.find(tag_name) ⇒ Object



25
26
27
# File 'lib/emjay/registry.rb', line 25

def self.find(tag_name)
  @loaded[tag_name] ||= load_component(tag_name)
end

.load_component(tag_name) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/emjay/registry.rb', line 38

def self.load_component(tag_name)
  path = PATHS[tag_name] or return nil
  @mutex.synchronize do
    return @loaded[tag_name] if @loaded[tag_name]
    require path
    const_name = File.basename(path, ".rb").split("_").map(&:capitalize).join
    Emjay::Components.const_get(const_name)
  end
end

.register(component_class) ⇒ Object

Kept for backwards compatibility with callers that eagerly register.



34
35
36
# File 'lib/emjay/registry.rb', line 34

def self.register(component_class)
  @loaded[component_class.component_name] = component_class
end