Class: Curlybars::Node::Partial

Inherits:
Struct
  • Object
show all
Defined in:
lib/curlybars/node/partial.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



3
4
5
# File 'lib/curlybars/node/partial.rb', line 3

def options
  @options
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



3
4
5
# File 'lib/curlybars/node/partial.rb', line 3

def path
  @path
end

#positionObject

Returns the value of attribute position

Returns:

  • (Object)

    the current value of position



3
4
5
# File 'lib/curlybars/node/partial.rb', line 3

def position
  @position
end

Instance Method Details

#cache_keyObject



98
99
100
101
102
103
# File 'lib/curlybars/node/partial.rb', line 98

def cache_key
  [
    path,
    options
  ].flatten.map(&:cache_key).push(position&.file_name, self.class.name).join("/")
end

#compileObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/curlybars/node/partial.rb', line 4

def compile
  compiled_options = options.map do |option|
    "options.merge!(#{option.compile})"
  end.join("\n")

  # NOTE: the following is a heredoc string, representing the ruby code fragment
  # outputted by this node.
  <<-RUBY
    options = ::ActiveSupport::HashWithIndifferentAccess.new
    #{compiled_options}

    partial_source = rendering.resolve_partial(#{path.path.inspect})
    if partial_source
      buffer.concat(rendering.render_partial(partial_source, #{path.path.inspect}, options).to_s)
    else
      buffer.concat(rendering.cached_call(#{path.compile}).to_s)
    end
  RUBY
end

#validate(branches, context: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/curlybars/node/partial.rb', line 24

def validate(branches, context: nil)
  # Validate option expressions in current scope
  errors = options.flat_map { |option| option.expression.validate(branches) }

  return errors unless context&.partial_resolver

  partial_source = begin
    context.partial_resolver.call(path.path)
  rescue StandardError
    nil
  end

  if partial_source
    if position.file_name == :"partials/#{path.path}"
      errors << Curlybars::Error::Validate.new(
        'self_referencing_partial',
        "'#{path.path}' cannot reference itself",
        position
      )
      return errors
    end

    options_tree = options.each_with_object({}) do |option, tree|
      expr = option.expression
      tree[option.key.to_sym] = if expr.respond_to?(:resolve)
        begin
          expr.resolve(branches)
        rescue Curlybars::Error::Validate
          nil
        end
      end
    end

    if context.valid?
      partial_errors = Curlybars.validate(
        options_tree,
        partial_source,
        :"partials/#{path.path}",
        validation_context: context.increment_depth,
        run_processors: false
      )
      inclusion_chain_entry = Curlybars::Position.new(
        position.file_name,
        position.line_number,
        position.line_offset,
        position.length
      )
      partial_errors.each do |e|
        e.[:inclusion_chain] = (e.[:inclusion_chain] || []).push(inclusion_chain_entry)
      end
      errors.concat(partial_errors)
    else
      errors << Curlybars::Error::Validate.new(
        'partial_nesting_limit_reached',
        "'#{path.path}' exceeds the partial nesting limit of #{Curlybars.configuration.partial_nesting_limit}",
        position
      )
    end
  else
    path_errors = Array(path.validate(branches, check_type: :partial))
    if path_errors.any? && context&.partial_resolver
      errors << Curlybars::Error::Validate.new(
        'partial_not_found',
        "'#{path.path}' partial could not be found",
        position
      )
    else
      errors.concat(path_errors)
    end
  end

  errors
end