Class: ViewComponent::Template
- Inherits:
-
Object
- Object
- ViewComponent::Template
show all
- Defined in:
- lib/view_component/template.rb
Defined Under Namespace
Classes: DataWithSource, File, Inline, InlineCall
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(component:, details:, lineno: nil, path: nil) ⇒ Template
Returns a new instance of Template.
15
16
17
18
19
20
|
# File 'lib/view_component/template.rb', line 15
def initialize(component:, details:, lineno: nil, path: nil)
@component = component
@details = details
@lineno = lineno
@path = path
end
|
Instance Attribute Details
#details ⇒ Object
Returns the value of attribute details.
10
11
12
|
# File 'lib/view_component/template.rb', line 10
def details
@details
end
|
#path ⇒ Object
Returns the value of attribute path.
10
11
12
|
# File 'lib/view_component/template.rb', line 10
def path
@path
end
|
Instance Method Details
#call_method_name ⇒ Object
182
183
184
185
186
|
# File 'lib/view_component/template.rb', line 182
def call_method_name
@call_method_name ||=
["call", (normalized_variant_name if variant.present?), (format unless default_format?)]
.compact.join("_").to_sym
end
|
#compile_to_component ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/view_component/template.rb', line 146
def compile_to_component
@component.silence_redefinition_of_method(call_method_name)
@component.class_eval <<~RUBY, @path, @lineno
def #{call_method_name}
#{compiled_source}
end
RUBY
@component.define_method(safe_method_name, @component.instance_method(@call_method_name))
end
|
#coverage_running? ⇒ Boolean
160
161
162
|
# File 'lib/view_component/template.rb', line 160
def coverage_running?
defined?(Coverage) && Coverage.running?
end
|
177
178
179
|
# File 'lib/view_component/template.rb', line 177
def default_format?
format.nil? || format == DEFAULT_FORMAT
end
|
#inline_call? ⇒ Boolean
173
174
175
|
# File 'lib/view_component/template.rb', line 173
def inline_call?
type == :inline_call
end
|
#normalized_variant_name ⇒ Object
192
193
194
|
# File 'lib/view_component/template.rb', line 192
def normalized_variant_name
variant.to_s.gsub("-", "__")
end
|
#requires_compiled_superclass? ⇒ Boolean
169
170
171
|
# File 'lib/view_component/template.rb', line 169
def requires_compiled_superclass?
inline_call? && !defined_on_self?
end
|
#safe_method_name ⇒ Object
188
189
190
|
# File 'lib/view_component/template.rb', line 188
def safe_method_name
"_#{call_method_name}_#{@component.name.underscore.gsub("/", "__")}"
end
|
#safe_method_name_call ⇒ Object
164
165
166
167
|
# File 'lib/view_component/template.rb', line 164
def safe_method_name_call
m = safe_method_name
proc { send(m) }
end
|