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 Attribute Summary collapse
-
#__vc_compiler_lock ⇒ Object
readonly
Lock required to be obtained before compiling the component.
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.
- #with_read_lock(&block) ⇒ Object
- #with_write_lock(&block) ⇒ Object
Constructor Details
#initialize(component_class) ⇒ Compiler
Returns a new instance of Compiler.
19 20 21 22 |
# File 'lib/view_component/compiler.rb', line 19 def initialize(component_class) @component_class = component_class @__vc_compiler_lock = Concurrent::ReadWriteLock.new end |
Instance Attribute Details
#__vc_compiler_lock ⇒ Object (readonly)
Lock required to be obtained before compiling the component
8 9 10 |
# File 'lib/view_component/compiler.rb', line 8 def __vc_compiler_lock @__vc_compiler_lock end |
Instance Method Details
#compile(raise_errors: false, force: false) ⇒ Object
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 |
# File 'lib/view_component/compiler.rb', line 32 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? with_write_lock do CompileCache.invalidate_class!(component_class) subclass_instance_methods = component_class.instance_methods(false) if subclass_instance_methods.include?(:with_content) && raise_errors raise ViewComponent::ComponentError.new( "#{component_class} implements a reserved method, `#with_content`.\n\n" \ "To fix this issue, change the name of the method." ) end if template_errors.present? raise ViewComponent::TemplateError.new(template_errors) if raise_errors return false end if subclass_instance_methods.include?(:before_render_check) ViewComponent::Deprecation.warn( "`#before_render_check` will be removed in v3.0.0.\n\n" \ "To fix this issue, use `#before_render` instead." ) end if raise_errors component_class.validate_initialization_parameters! component_class.validate_collection_parameter! end templates.each do |template| # Remove existing compiled template methods, # as Ruby warns when redefining a method. method_name = call_method_name(template[:variant]) if component_class.instance_methods.include?(method_name.to_sym) component_class.send(:undef_method, method_name.to_sym) end # 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 define_render_template_for component_class.build_i18n_backend component_class._after_compile CompileCache.register(component_class) end end |
#compiled? ⇒ Boolean
24 25 26 |
# File 'lib/view_component/compiler.rb', line 24 def compiled? CompileCache.compiled?(component_class) end |
#development? ⇒ Boolean
28 29 30 |
# File 'lib/view_component/compiler.rb', line 28 def development? self.class.mode == DEVELOPMENT_MODE end |
#with_read_lock(&block) ⇒ Object
103 104 105 |
# File 'lib/view_component/compiler.rb', line 103 def with_read_lock(&block) __vc_compiler_lock.with_read_lock(&block) end |
#with_write_lock(&block) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/view_component/compiler.rb', line 95 def with_write_lock(&block) if development? __vc_compiler_lock.with_write_lock(&block) else block.call end end |