Class: ViewComponent::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/view_component/compiler.rb

Instance Method Summary collapse

Constructor Details

#initialize(component) ⇒ Compiler

Returns a new instance of Compiler.



13
14
15
16
17
# File 'lib/view_component/compiler.rb', line 13

def initialize(component)
  @component = component
  @redefinition_lock = Mutex.new
  @rendered_templates = Set.new
end

Instance Method Details

#compile(raise_errors: false, force: false) ⇒ Object



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

def compile(raise_errors: false, force: false)
  return if compiled? && !force
  return if @component == ViewComponent::Base

  gather_templates

  if self.class.development_mode && @templates.any?(&:requires_compiled_superclass?)
    @component.superclass.compile(raise_errors: raise_errors)
  end

  return if gather_template_errors(raise_errors).any?

  if raise_errors
    @component.validate_initialization_parameters!
    @component.validate_collection_parameter!
  end

  define_render_template_for

  @component.register_default_slots
  @component.build_i18n_backend

  CompileCache.register(@component)
end

#compiled?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/view_component/compiler.rb', line 19

def compiled?
  CompileCache.compiled?(@component)
end

#renders_template_for?(variant, format) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/view_component/compiler.rb', line 48

def renders_template_for?(variant, format)
  @rendered_templates.include?([variant, format])
end