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
175
176
177
178
179
|
# File 'lib/view_component/template.rb', line 175
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
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/view_component/template.rb', line 139
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
153
154
155
|
# File 'lib/view_component/template.rb', line 153
def coverage_running?
defined?(Coverage) && Coverage.running?
end
|
170
171
172
|
# File 'lib/view_component/template.rb', line 170
def default_format?
format.nil? || format == DEFAULT_FORMAT
end
|
#inline_call? ⇒ Boolean
166
167
168
|
# File 'lib/view_component/template.rb', line 166
def inline_call?
type == :inline_call
end
|
#normalized_variant_name ⇒ Object
185
186
187
|
# File 'lib/view_component/template.rb', line 185
def normalized_variant_name
variant.to_s.gsub("-", "__")
end
|
#requires_compiled_superclass? ⇒ Boolean
162
163
164
|
# File 'lib/view_component/template.rb', line 162
def requires_compiled_superclass?
inline_call? && !defined_on_self?
end
|
#safe_method_name ⇒ Object
181
182
183
|
# File 'lib/view_component/template.rb', line 181
def safe_method_name
"_#{call_method_name}_#{@component.name.underscore.gsub("/", "__")}"
end
|
#safe_method_name_call ⇒ Object
157
158
159
160
|
# File 'lib/view_component/template.rb', line 157
def safe_method_name_call
m = safe_method_name
proc { send(m) }
end
|