Class: ViewComponent::Compiler
- Inherits:
-
Object
- Object
- ViewComponent::Compiler
- Defined in:
- lib/view_component/compiler.rb
Constant Summary collapse
- DEVELOPMENT_MODE =
Compiler mode. Can be either:
-
development (a blocking mode which ensures thread safety when redefining the ‘call` method for components,
default in Rails development and test mode)
-
production (a non-blocking mode, default in Rails production mode)
-
:development
- PRODUCTION_MODE =
:production
Instance Method Summary collapse
- #compile(raise_errors: false, force: false) ⇒ Object
- #compiled? ⇒ Boolean
- #development? ⇒ Boolean
-
#initialize(component_class) ⇒ Compiler
constructor
A new instance of Compiler.
- #renders_template_for_variant?(variant) ⇒ Boolean
Constructor Details
#initialize(component_class) ⇒ Compiler
Returns a new instance of Compiler.
16 17 18 19 20 |
# File 'lib/view_component/compiler.rb', line 16 def initialize(component_class) @component_class = component_class @redefinition_lock = Mutex.new @variants_rendering_templates = Set.new end |
Instance Method Details
#compile(raise_errors: false, force: false) ⇒ Object
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/view_component/compiler.rb', line 30 def compile(raise_errors: false, force: false) return if compiled? && !force return if component_class == ViewComponent::Base component_class.superclass.compile(raise_errors: raise_errors) if should_compile_superclass? if template_errors.present? raise TemplateError.new(template_errors) if raise_errors return false end if raise_errors component_class.validate_initialization_parameters! component_class.validate_collection_parameter! end if has_inline_template? template = component_class.inline_template redefinition_lock.synchronize do component_class.silence_redefinition_of_method("call") # rubocop:disable Style/EvalWithLocation component_class.class_eval <<-RUBY, template.path, template.lineno def call #{compiled_inline_template(template)} end RUBY # rubocop:enable Style/EvalWithLocation component_class.define_method(:"_call_#{safe_class_name}", component_class.instance_method(:call)) component_class.silence_redefinition_of_method("render_template_for") component_class.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def render_template_for(variant = nil) _call_#{safe_class_name} end RUBY end else templates.each do |template| method_name = call_method_name(template[:variant]) @variants_rendering_templates << template[:variant] redefinition_lock.synchronize do component_class.silence_redefinition_of_method(method_name) # rubocop:disable Style/EvalWithLocation component_class.class_eval <<-RUBY, template[:path], 0 def #{method_name} #{compiled_template(template[:path])} end RUBY # rubocop:enable Style/EvalWithLocation end end define_render_template_for end component_class.registered_slots.each do |slot_name, config| config[:default_method] = component_class.instance_methods.find { |method_name| method_name == :"default_#{slot_name}" } component_class.registered_slots[slot_name] = config end component_class.build_i18n_backend CompileCache.register(component_class) end |
#compiled? ⇒ Boolean
22 23 24 |
# File 'lib/view_component/compiler.rb', line 22 def compiled? CompileCache.compiled?(component_class) end |
#development? ⇒ Boolean
26 27 28 |
# File 'lib/view_component/compiler.rb', line 26 def development? self.class.mode == DEVELOPMENT_MODE end |
#renders_template_for_variant?(variant) ⇒ Boolean
100 101 102 |
# File 'lib/view_component/compiler.rb', line 100 def renders_template_for_variant?(variant) @variants_rendering_templates.include?(variant) end |