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.
Constructor Details
#initialize(component_class) ⇒ Compiler
Returns a new instance of Compiler.
16 17 18 19 |
# File 'lib/view_component/compiler.rb', line 16 def initialize(component_class) @component_class = component_class @redefinition_lock = Mutex.new end |
Instance Method Details
#compile(raise_errors: false, force: false) ⇒ Object
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 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 |
# File 'lib/view_component/compiler.rb', line 29 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.silence_redefinition_of_method("render_template_for") component_class.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def render_template_for(variant = nil) call end RUBY end else templates.each do |template| # Remove existing compiled template methods, # as Ruby warns when redefining a method. method_name = call_method_name(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.build_i18n_backend CompileCache.register(component_class) end |
#compiled? ⇒ Boolean
21 22 23 |
# File 'lib/view_component/compiler.rb', line 21 def compiled? CompileCache.compiled?(component_class) end |
#development? ⇒ Boolean
25 26 27 |
# File 'lib/view_component/compiler.rb', line 25 def development? self.class.mode == DEVELOPMENT_MODE end |