Class: CompEx::ComponentRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/compex/component_registry.rb

Defined Under Namespace

Classes: Node

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.componentsObject (readonly)

Returns the value of attribute components.



15
16
17
# File 'lib/compex/component_registry.rb', line 15

def components
  @components
end

.treeObject (readonly)

Returns the value of attribute tree.



15
16
17
# File 'lib/compex/component_registry.rb', line 15

def tree
  @tree
end

Class Method Details

.find_by_path(path, base = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/compex/component_registry.rb', line 46

def find_by_path(path, base = nil)
  base ||= @tree
  return base if path.empty?

  ret = base.children.find { it.name == path.first }
  return nil unless ret

  find_by_path(path[1...], ret)
end

.flush!Object



17
18
19
20
# File 'lib/compex/component_registry.rb', line 17

def flush!
  @components = {}
  @tree = Struct.new(:children).new(children: [])
end

.gather_scriptsObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/compex/component_registry.rb', line 98

def gather_scripts
  scripts = []
  walk(@tree) do |comp|
    next unless comp.has_js?
    scripts << {
      component: comp,
      source: comp.js,
      hash: comp.js_hash
    }
  end

  scripts
end

.gather_stylesObject



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/compex/component_registry.rb', line 112

def gather_styles
  styles = []
  walk(@tree) do |comp|
    next unless comp.has_style?
    styles << {
      component: comp,
      source: comp.css,
      hash: comp.css_hash
    }
  end

  styles
end

.register(component) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/compex/component_registry.rb', line 22

def register(component)
  @components ||= {}
  @tree ||= Struct.new(:children).new(children: [])

  stable_id = component.stable_class_id
  @components[stable_id] = component

  names = stable_id.split("::")
  node = @tree
  until names.empty?
    name = names.shift
    target = node.children.find { it.name == name }
    unless target
      target = Node.new(name)
      node.children << target
    end
    if names.empty?
      warn "[CompEx] Component name collision: #{component.name} vs #{target.component.name}" if target.component
      target.component = component
    end
    node = target
  end
end

.resolve(name, context: nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/compex/component_registry.rb', line 56

def resolve(name, context: nil)
  puts "resolve #{name}, #{context.inspect}"
  return nil if name.nil? || name.empty?

  # Normalize
  absolute = name.start_with?("::")
  path = name.gsub(/^::/, "").split("::")

  # Absolute lookup always from root
  if absolute
    node = find_by_path(path, @tree)
    return node&.component
  end

  # Relative lookup: walk up context nesting
  if context
    namespaces = context.name.split("::")
    until namespaces.empty?
      base = find_by_path(namespaces, @tree)
      if base
        node = find_by_path(path, base)
        return node&.component if node&.component
      end
      namespaces.pop
    end
  end

  # Fallback: top-level
  node = find_by_path(path, @tree)
  node&.component
end

.walk(base) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/compex/component_registry.rb', line 88

def walk(base, &)
  base.children.each do |v|
    if v.respond_to?(:component) && !v.component.nil?
      yield v.component.descriptor
    end

    walk(v, &) if v.children && v.children.length > 0
  end
end