Class: ViewComponent::Compiler
- Inherits:
-
Object
- Object
- ViewComponent::Compiler
- Defined in:
- lib/view_component/compiler.rb
Instance Method Summary collapse
- #compile(raise_errors: false, force: false) ⇒ Object
- #compiled? ⇒ Boolean
-
#find_templates_for(requested_details) ⇒ Object
All matching compiled templates, in priority order based on the requested details from LookupContext.
-
#initialize(component) ⇒ Compiler
constructor
A new instance of Compiler.
Constructor Details
#initialize(component) ⇒ Compiler
Returns a new instance of Compiler.
13 14 15 16 |
# File 'lib/view_component/compiler.rb', line 13 def initialize(component) @component = component @lock = Mutex.new end |
Instance Method Details
#compile(raise_errors: false, force: false) ⇒ Object
22 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 |
# File 'lib/view_component/compiler.rb', line 22 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.__vc_development_mode && @templates.any?(&:requires_compiled_superclass?) @component.superclass.__vc_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.__vc_validate_initialization_parameters! @component.__vc_validate_collection_parameter! end define_render_template_for @component.__vc_register_default_slots @component.__vc_build_i18n_backend CompileCache.register(@component) end end |
#compiled? ⇒ Boolean
18 19 20 |
# File 'lib/view_component/compiler.rb', line 18 def compiled? CompileCache.compiled?(@component) end |
#find_templates_for(requested_details) ⇒ Object
Returns all matching compiled templates, in priority order based on the requested details from LookupContext.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/view_component/compiler.rb', line 61 def find_templates_for(requested_details) filtered_templates = @templates.select do |template| template.details.matches?(requested_details) end if filtered_templates.count > 1 filtered_templates.sort_by! do |template| template.details.sort_key_for(requested_details) end end filtered_templates end |