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
  @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
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/view_component/compiler.rb', line 23

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

  @lock.synchronize do
    # this check is duplicated so that concurrent compile calls can still
    # early exit
    return if compiled? && !force

    gather_templates

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

    if template_errors.present?
      raise TemplateError.new(template_errors) if raise_errors

      # this return is load bearing, and prevents the component from being considered "compiled?"
      return false
    end

    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
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)


59
60
61
# File 'lib/view_component/compiler.rb', line 59

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