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
129
130
131
132
133
|
# File 'lib/view_component/template.rb', line 129
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
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/view_component/template.rb', line 97
def compile_to_component
@component.silence_redefinition_of_method(call_method_name)
@component.class_eval " def \#{call_method_name}\n \#{compiled_source}\n end\n RUBY\n # rubocop:enable Style/EvalWithLocation\n\n @component.define_method(safe_method_name, @component.instance_method(@call_method_name))\nend\n", @path, @lineno
|
124
125
126
|
# File 'lib/view_component/template.rb', line 124
def default_format?
format.nil? || format == DEFAULT_FORMAT
end
|
#inline_call? ⇒ Boolean
120
121
122
|
# File 'lib/view_component/template.rb', line 120
def inline_call?
type == :inline_call
end
|
#normalized_variant_name ⇒ Object
139
140
141
|
# File 'lib/view_component/template.rb', line 139
def normalized_variant_name
variant.to_s.gsub("-", "__")
end
|
#requires_compiled_superclass? ⇒ Boolean
116
117
118
|
# File 'lib/view_component/template.rb', line 116
def requires_compiled_superclass?
inline_call? && !defined_on_self?
end
|
#safe_method_name ⇒ Object
135
136
137
|
# File 'lib/view_component/template.rb', line 135
def safe_method_name
"_#{call_method_name}_#{@component.name.underscore.gsub("/", "__")}"
end
|
#safe_method_name_call ⇒ Object
111
112
113
114
|
# File 'lib/view_component/template.rb', line 111
def safe_method_name_call
m = safe_method_name
proc { send(m) }
end
|