Class: Curlybars::Node::BlockHelperElse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments

Returns:

  • (Object)

    the current value of arguments



5
6
7
# File 'lib/curlybars/node/block_helper_else.rb', line 5

def arguments
  @arguments
end

#else_templateObject

Returns the value of attribute else_template

Returns:

  • (Object)

    the current value of else_template



5
6
7
# File 'lib/curlybars/node/block_helper_else.rb', line 5

def else_template
  @else_template
end

#helperObject

Returns the value of attribute helper

Returns:

  • (Object)

    the current value of helper



5
6
7
# File 'lib/curlybars/node/block_helper_else.rb', line 5

def helper
  @helper
end

#helper_templateObject

Returns the value of attribute helper_template

Returns:

  • (Object)

    the current value of helper_template



5
6
7
# File 'lib/curlybars/node/block_helper_else.rb', line 5

def helper_template
  @helper_template
end

#helpercloseObject

Returns the value of attribute helperclose

Returns:

  • (Object)

    the current value of helperclose



5
6
7
# File 'lib/curlybars/node/block_helper_else.rb', line 5

def helperclose
  @helperclose
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



5
6
7
# File 'lib/curlybars/node/block_helper_else.rb', line 5

def options
  @options
end

#positionObject

Returns the value of attribute position

Returns:

  • (Object)

    the current value of position



5
6
7
# File 'lib/curlybars/node/block_helper_else.rb', line 5

def position
  @position
end

Instance Method Details

#cache_keyObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/curlybars/node/block_helper_else.rb', line 88

def cache_key
  [
    helper,
    arguments,
    options,
    helper_template,
    else_template,
    helperclose
  ].flatten.map(&:cache_key).push(self.class.name).join("/")
end

#compileObject



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

def compile
  check_open_and_close_elements(helper, helperclose, Curlybars::Error::Compile)

  compiled_arguments = arguments.map do |argument|
    "arguments.push(rendering.cached_call(#{argument.compile}))"
  end.join("\n")

  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}

    arguments = []
    #{compiled_arguments}

    helper = #{helper.compile}
    helper_position = rendering.position(#{helper.position.line_number},
      #{helper.position.line_offset})

    options[:fn] = ->(**vars) do
      variables.push(vars.symbolize_keys)
      outer_buffer = buffer
      begin
        buffer = ::Curlybars::SafeBuffer.new
        #{helper_template.compile}
        buffer
      ensure
        buffer = outer_buffer
        variables.pop
      end
    end

    options[:inverse] = ->(**vars) do
      variables.push(vars.symbolize_keys)
      outer_buffer = buffer
      begin
        buffer = ::Curlybars::SafeBuffer.new
        #{else_template.compile}
        buffer
      ensure
        buffer = outer_buffer
        variables.pop
      end
    end

    options[:this] = contexts.last

    result = rendering.call(helper, #{helper.path.inspect}, helper_position,
      arguments, options, &options[:fn])

    unless rendering.presenter?(result) || rendering.presenter_collection?(result)
      buffer.concat(result.to_s)
    end
  RUBY
end

#validate(branches) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/curlybars/node/block_helper_else.rb', line 67

def validate(branches)
  check_open_and_close_elements(helper, helperclose, Curlybars::Error::Validate)

  if helper.leaf?(branches)
    if arguments.any? || options.any?
      message = "#{helper.path} doesn't accept any arguments or options"
      Curlybars::Error::Validate.new('invalid_signature', message, helper.position)
    end
  elsif helper.helper?(branches)
    [
      helper_template.validate(branches),
      else_template.validate(branches),
      arguments.map { |argument| argument.validate_as_value(branches) },
      options.map { |option| option.validate(branches) }
    ]
  else
    message = "#{helper.path} must be allowed as helper or leaf"
    Curlybars::Error::Validate.new('invalid_block_helper', message, helper.position)
  end
end