Class: ViewComponent::Template

Inherits:
Object
  • Object
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

#detailsObject (readonly)

Returns the value of attribute details.



10
11
12
# File 'lib/view_component/template.rb', line 10

def details
  @details
end

#pathObject (readonly)

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_nameObject



181
182
183
184
185
# File 'lib/view_component/template.rb', line 181

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_componentObject



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/view_component/template.rb', line 145

def compile_to_component
  @component.silence_redefinition_of_method(call_method_name)

  # rubocop:disable Style/EvalWithLocation
  @component.class_eval <<~RUBY, @path, lineno
    def #{call_method_name}
      #{compiled_source}
    end
  RUBY
  # rubocop:enable Style/EvalWithLocation

  @component.define_method(safe_method_name, @component.instance_method(@call_method_name))
end

#coverage_running?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/view_component/template.rb', line 159

def coverage_running?
  defined?(Coverage) && Coverage.running?
end

#default_format?Boolean Also known as: html?

Returns:

  • (Boolean)


176
177
178
# File 'lib/view_component/template.rb', line 176

def default_format?
  format.nil? || format == DEFAULT_FORMAT
end

#inline_call?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/view_component/template.rb', line 172

def inline_call?
  type == :inline_call
end

#normalized_variant_nameObject



191
192
193
# File 'lib/view_component/template.rb', line 191

def normalized_variant_name
  variant.to_s.gsub("-", "__")
end

#requires_compiled_superclass?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/view_component/template.rb', line 168

def requires_compiled_superclass?
  inline_call? && !defined_on_self?
end

#safe_method_nameObject



187
188
189
# File 'lib/view_component/template.rb', line 187

def safe_method_name
  "_#{call_method_name}_#{@component.name.underscore.gsub("/", "__")}"
end

#safe_method_name_callObject



163
164
165
166
# File 'lib/view_component/template.rb', line 163

def safe_method_name_call
  m = safe_method_name
  proc { send(m) }
end