Class: Curlybars::Node::WithElse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#else_templateObject

Returns the value of attribute else_template

Returns:

  • (Object)

    the current value of else_template



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

def position
  @position
end

#with_templateObject

Returns the value of attribute with_template

Returns:

  • (Object)

    the current value of with_template



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

def with_template
  @with_template
end

Instance Method Details

#cache_keyObject



49
50
51
52
53
54
55
# File 'lib/curlybars/node/with_else.rb', line 49

def cache_key
  [
    path,
    with_template,
    else_template
  ].map(&:cache_key).push(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
23
24
# File 'lib/curlybars/node/with_else.rb', line 4

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

    if rendering.to_bool(compiled_path)
      position = rendering.position(#{position.line_number}, #{position.line_offset})
      rendering.check_context_is_presenter(compiled_path, #{presenter_path.path.inspect}, position)

      contexts.push(compiled_path)
      begin
        #{with_template.compile}
      ensure
        contexts.pop
      end
    else
      #{else_template.compile}
    end
  RUBY
end

#presenter_pathObject



45
46
47
# File 'lib/curlybars/node/with_else.rb', line 45

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

#validate(branches) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/curlybars/node/with_else.rb', line 26

def validate(branches)
  sub_tree = path.resolve_and_check!(branches, check_type: :presenterlike)
  with_template_errors = begin
    branches.push(sub_tree)
    with_template.validate(branches)
  ensure
    branches.pop
  end

  else_template_errors = else_template.validate(branches)

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