Class: ViewComponentReflex::ReflexFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/view_component_reflex/reflex_factory.rb

Instance Method Summary collapse

Constructor Details

#initialize(component) ⇒ ReflexFactory

Returns a new instance of ReflexFactory.



3
4
5
6
7
# File 'lib/view_component_reflex/reflex_factory.rb', line 3

def initialize(component)
  @component = component
  @new = false
  reflex.component_class = component
end

Instance Method Details

#nested?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/view_component_reflex/reflex_factory.rb', line 9

def nested?
  @nested ||= @component.name.include?("::")
end

#new?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/view_component_reflex/reflex_factory.rb', line 21

def new?
  @new
end

#reflexObject



25
26
27
28
29
30
31
# File 'lib/view_component_reflex/reflex_factory.rb', line 25

def reflex
  @reflex ||= if nested?
    reflex_from_nested_component
  else
    reflex_from_component
  end
end

#reflex_from_componentObject



52
53
54
55
56
57
58
59
# File 'lib/view_component_reflex/reflex_factory.rb', line 52

def reflex_from_component
  if Object.const_defined?(reflex_name)
    Object.const_get(reflex_name)
  else
    @new = true
    Object.const_set(reflex_name, reflex_instance)
  end
end

#reflex_from_nested_componentObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/view_component_reflex/reflex_factory.rb', line 37

def reflex_from_nested_component
  parent = if @component.respond_to? :module_parent
    @component.module_parent
  else
    @component.parent
  end

  if parent.const_defined?(reflex_name)
    parent.const_get(reflex_name)
  else
    @new = true
    parent.const_set(reflex_name, reflex_instance)
  end
end

#reflex_instanceObject



33
34
35
# File 'lib/view_component_reflex/reflex_factory.rb', line 33

def reflex_instance
  @reflex_instance ||= Class.new(@component.reflex_base_class)
end

#reflex_nameObject



13
14
15
16
17
18
19
# File 'lib/view_component_reflex/reflex_factory.rb', line 13

def reflex_name
  @reflex_name ||= if nested?
    @component.name.split("::").last
  else
    @component.name
  end + "Reflex"
end