Class: ViewComponent::Template

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

Defined Under Namespace

Classes: DataNoSource, DataWithSource

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.



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
42
43
44
# File 'lib/view_component/template.rb', line 10

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.



8
9
10
# File 'lib/view_component/template.rb', line 8

def this_format
  @this_format
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/view_component/template.rb', line 8

def type
  @type
end

#variantObject (readonly)

Returns the value of attribute variant.



8
9
10
# File 'lib/view_component/template.rb', line 8

def variant
  @variant
end

Instance Method Details

#compile_to_componentObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/view_component/template.rb', line 46

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)


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

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

#defined_on_self?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/view_component/template.rb', line 98

def defined_on_self?
  @defined_on_self
end

#formatObject



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

def format
  @this_format
end

#inline?Boolean

Returns:

  • (Boolean)


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

def inline?
  @type == :inline
end

#inline_call?Boolean

Returns:

  • (Boolean)


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

def inline_call?
  @type == :inline_call
end

#normalized_variant_nameObject



94
95
96
# File 'lib/view_component/template.rb', line 94

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

#requires_compiled_superclass?Boolean

Returns:

  • (Boolean)


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

def requires_compiled_superclass?
  inline_call? && !defined_on_self?
end

#safe_method_nameObject



90
91
92
# File 'lib/view_component/template.rb', line 90

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

#safe_method_name_callObject



62
63
64
65
66
67
68
# File 'lib/view_component/template.rb', line 62

def safe_method_name_call
  return safe_method_name unless inline_call?

  "maybe_escape_html(#{safe_method_name}) " \
  "{ Kernel.warn('WARNING: The #{@component} component rendered HTML-unsafe output. " \
  "The output will be automatically escaped, but you may want to investigate.') } "
end