Class: Rhales::HydrationRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/rhales/hydration/hydration_registry.rb

Overview

Registry to track window attributes used in hydration blocks within a single request. Prevents silent data overwrites.

Class Method Summary collapse

Class Method Details

.clear!Object



30
31
32
# File 'lib/rhales/hydration/hydration_registry.rb', line 30

def clear!
  Thread.current[:rhales_hydration_registry] = {}
end

.register(window_attr, template_path, merge_strategy = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rhales/hydration/hydration_registry.rb', line 10

def register(window_attr, template_path, merge_strategy = nil)
  validate_inputs(window_attr, template_path)

  registry = thread_local_registry

  if registry[window_attr] && merge_strategy.nil?
    existing = registry[window_attr]
    raise HydrationCollisionError.new(
      window_attr,
      existing[:path],
      template_path,
    )
  end

  registry[window_attr] = {
    path: template_path,
    merge_strategy: merge_strategy,
  }
end

.registryObject

Expose registry for testing purposes



35
36
37
# File 'lib/rhales/hydration/hydration_registry.rb', line 35

def registry
  thread_local_registry
end

.thread_local_registryObject (private)



41
42
43
# File 'lib/rhales/hydration/hydration_registry.rb', line 41

def thread_local_registry
  Thread.current[:rhales_hydration_registry] ||= {}
end

.validate_inputs(window_attr, template_path) ⇒ Object (private)



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rhales/hydration/hydration_registry.rb', line 45

def validate_inputs(window_attr, template_path)
  if window_attr.nil?
    raise ArgumentError, 'window attribute cannot be nil'
  end

  if window_attr.empty?
    raise ArgumentError, 'window attribute cannot be empty'
  end

  if template_path.nil?
    raise ArgumentError, 'template path cannot be nil'
  end
end