Class: Curlybars::Node::EachElse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#each_templateObject

Returns the value of attribute each_template

Returns:

  • (Object)

    the current value of each_template



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

def each_template
  @each_template
end

#else_templateObject

Returns the value of attribute else_template

Returns:

  • (Object)

    the current value of else_template



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

def else_template
  @else_template
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



3
4
5
# File 'lib/curlybars/node/each_else.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/each_else.rb', line 3

def position
  @position
end

Instance Method Details

#cache_keyObject



64
65
66
67
68
69
70
# File 'lib/curlybars/node/each_else.rb', line 64

def cache_key
  [
    path,
    each_template,
    else_template
  ].map(&:cache_key).push(self.class.name).join("/")
end

#collection_pathObject



60
61
62
# File 'lib/curlybars/node/each_else.rb', line 60

def collection_path
  path.subexpression? ? path.helper : path
end

#compileObject



4
5
6
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
# File 'lib/curlybars/node/each_else.rb', line 4

def compile
  # NOTE: the following is a heredoc string, representing the ruby code fragment
  # outputted by this node.
  <<-RUBY
    collection = rendering.cached_call(#{path.compile})

    if rendering.to_bool(collection)
      position = rendering.position(#{position.line_number}, #{position.line_offset})
      template_cache_key = '#{each_template.cache_key}'

      collection = rendering.coerce_to_hash!(collection, #{collection_path.path.inspect}, position)
      collection.each.with_index.map do |key_and_presenter, index|
        rendering.check_timeout!
        rendering.optional_presenter_cache(key_and_presenter[1], template_cache_key, buffer) do |buffer|
          begin
            contexts.push(key_and_presenter[1])
            variables.push({
              index: index,
              key: key_and_presenter[0],
              first: index == 0,
              last: index == (collection.length - 1),
            })
            #{each_template.compile}
          ensure
            variables.pop
            contexts.pop
          end
        end
      end
    else
      #{else_template.compile}
    end
  RUBY
end

#validate(branches) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/curlybars/node/each_else.rb', line 39

def validate(branches)
  resolved = path.resolve_and_check!(branches, check_type: :collectionlike)
  sub_tree = resolved.first

  each_template_errors = begin
    branches.push(sub_tree)
    each_template.validate(branches)
  ensure
    branches.pop
  end

  else_template_errors = else_template.validate(branches)

  [
    each_template_errors,
    else_template_errors
  ]
rescue Curlybars::Error::Validate => path_error
  path_error
end