Class: ViewComponent::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/view_component/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component:, type:, this_format: nil, variant: nil, lineno: nil, path: nil, extension: nil, source: nil, method_name: nil, defined_on_self: true) ⇒ Template

Returns a new instance of Template.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/view_component/template.rb', line 7

def initialize(
  component:,
  type:,
  this_format: nil,
  variant: nil,
  lineno: nil,
  path: nil,
  extension: nil,
  source: nil,
  method_name: nil,
  defined_on_self: true
)
  @component = component
  @type = type
  @this_format = this_format
  @variant = variant&.to_sym
  @lineno = lineno
  @path = path
  @extension = extension
  @source = source
  @method_name = method_name
  @defined_on_self = defined_on_self

  @source_originally_nil = @source.nil?

  @call_method_name =
    if @method_name
      @method_name
    else
      out = +"call"
      out << "_#{normalized_variant_name}" if @variant.present?
      out << "_#{@this_format}" if @this_format.present? && @this_format != ViewComponent::Base::VC_INTERNAL_DEFAULT_FORMAT
      out
    end
end

Instance Attribute Details

#this_formatObject (readonly)

Returns the value of attribute this_format.



5
6
7
# File 'lib/view_component/template.rb', line 5

def this_format
  @this_format
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/view_component/template.rb', line 5

def type
  @type
end

#variantObject (readonly)

Returns the value of attribute variant.



5
6
7
# File 'lib/view_component/template.rb', line 5

def variant
  @variant
end

Instance Method Details

#compile_to_componentObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/view_component/template.rb', line 43

def compile_to_component
  if !inline_call?
    @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
  end

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

#default_format?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/view_component/template.rb', line 71

def default_format?
  @this_format == ViewComponent::Base::VC_INTERNAL_DEFAULT_FORMAT
end

#defined_on_self?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/view_component/template.rb', line 87

def defined_on_self?
  @defined_on_self
end

#formatObject



75
76
77
# File 'lib/view_component/template.rb', line 75

def format
  @this_format
end

#inline?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/view_component/template.rb', line 67

def inline?
  @type == :inline
end

#inline_call?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/view_component/template.rb', line 63

def inline_call?
  @type == :inline_call
end

#normalized_variant_nameObject



83
84
85
# File 'lib/view_component/template.rb', line 83

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

#requires_compiled_superclass?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/view_component/template.rb', line 59

def requires_compiled_superclass?
  inline_call? && !defined_on_self?
end

#safe_method_nameObject



79
80
81
# File 'lib/view_component/template.rb', line 79

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