Class: Curlybars::Node::BlockHelperElse
- Inherits:
-
Struct
- Object
- Struct
- Curlybars::Node::BlockHelperElse
- Defined in:
- lib/curlybars/node/block_helper_else.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#else_template ⇒ Object
Returns the value of attribute else_template.
-
#helper ⇒ Object
Returns the value of attribute helper.
-
#helper_template ⇒ Object
Returns the value of attribute helper_template.
-
#helperclose ⇒ Object
Returns the value of attribute helperclose.
-
#options ⇒ Object
Returns the value of attribute options.
-
#position ⇒ Object
Returns the value of attribute position.
Instance Method Summary collapse
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments
5 6 7 |
# File 'lib/curlybars/node/block_helper_else.rb', line 5 def arguments @arguments end |
#else_template ⇒ Object
Returns the value of attribute else_template
5 6 7 |
# File 'lib/curlybars/node/block_helper_else.rb', line 5 def else_template @else_template end |
#helper ⇒ Object
Returns the value of attribute helper
5 6 7 |
# File 'lib/curlybars/node/block_helper_else.rb', line 5 def helper @helper end |
#helper_template ⇒ Object
Returns the value of attribute helper_template
5 6 7 |
# File 'lib/curlybars/node/block_helper_else.rb', line 5 def helper_template @helper_template end |
#helperclose ⇒ Object
Returns the value of attribute helperclose
5 6 7 |
# File 'lib/curlybars/node/block_helper_else.rb', line 5 def helperclose @helperclose end |
#options ⇒ Object
Returns the value of attribute options
5 6 7 |
# File 'lib/curlybars/node/block_helper_else.rb', line 5 def @options end |
#position ⇒ Object
Returns the value of attribute position
5 6 7 |
# File 'lib/curlybars/node/block_helper_else.rb', line 5 def position @position end |
Instance Method Details
#cache_key ⇒ Object
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, , helper_template, else_template, helperclose ].flatten.map(&:cache_key).push(self.class.name).join("/") end |
#compile ⇒ Object
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") = .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 #{} 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? || .any? = "#{helper.path} doesn't accept any arguments or options" Curlybars::Error::Validate.new('invalid_signature', , helper.position) end elsif helper.helper?(branches) [ helper_template.validate(branches), else_template.validate(branches), arguments.map { |argument| argument.validate_as_value(branches) }, .map { |option| option.validate(branches) } ] else = "#{helper.path} must be allowed as helper or leaf" Curlybars::Error::Validate.new('invalid_block_helper', , helper.position) end end |