Class: Cecil::Node::Template

Inherits:
Cecil::Node show all
Defined in:
lib/cecil/node.rb

Overview

A node that has placeholders but does not yet have values or children. Created with backticks or #`(code_str)

When #with/#[] is called on the node, it will replace itself with a Literal or LiteralWithChildren

Instance Method Summary collapse

Methods inherited from Cecil::Node

#<<, #[]

Instance Method Details

#with(*positional_values, **named_values) ⇒ Object

See Also:



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/cecil/node.rb', line 384

def with(*positional_values, **named_values, &)
  src =
    case [positional_values, named_values, @placeholders]
    in [], {}, [] # TODO: is this case possible? should it be?
      @src
    in [], _, _
      Text.interpolate_named(@src, @placeholders, named_values)
    in _, {}, _
      Text.interpolate_positional(@src, @placeholders, positional_values)
    else
      raise "Method expects to be called with either named arguments or positional arguments but not both"
    end

  Literal
    .build(src:, parent:, &)
    .tap { builder.replace_node self, _1 }
end