Class: Cecil::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/cecil/node.rb

Direct Known Subclasses

Deferred, Literal, Template

Defined Under Namespace

Classes: Deferred, Detached, Literal, LiteralWithChildren, SameLineContainer, Template

Instance Method Summary collapse

Instance Method Details

#<<(string_or_node) ⇒ Node

Append a string or node to the node, without making a new line.

Examples:

Append a string to close brackets that aren't closed automatically

`test("quacks like a duck", () => {`[] do
  `expect(duck)`
end << ')' # closes open bracket from "test("

# test("quacks like a duck", () => {
#   expect(duck)
# })

Use backticks to append brackets

`test("quacks like a duck", () => {`[] do
  `expect(duck)`
end << `)` # closes open bracket from "test("

# test("quacks like a duck", () => {`
#   expect(duck)
# })

Parameters:

  • string_or_node (String, Node)

Returns:



137
138
139
140
141
142
# File 'lib/cecil/node.rb', line 137

def <<(string_or_node)
  SameLineContainer.new(parent:).tap do |container|
    container.add_child self
    replace_with container
  end << string_or_node
end

#[]Node

Alias of #with

Returns:



110
111
112
113
# File 'lib/cecil/node.rb', line 110

def [](...)
  # don't use alias/alias_method b/c subclasses overriding `with` need `[]` to call `self.with`
  with(...)
end

#with(*positional_values) ⇒ Node #with(**named_values) ⇒ Node #with(*positional_values, &) ⇒ Node #with(**named_values, &) ⇒ Node #with(&) ⇒ Node

Provide values for placeholders and/or nest a block of code. When called, will replace this node with a Literal or LiteralWithChildren.

Placeholder values can be given as positional arguments or named values, but not both.

When called with a block, the block is called immediately and any source code emitted is nested under the current block.

Examples:

Positional values are replaced in the order given

`const $field = $value`["user", "Alice".to_json]
# const user = "Alice"

Positional values replace all placeholders with the same name

`const $field: $Namespace.$Class = new $Namespace.$Class()`["user", "Models", "User"]
# const user: Models.User = new Models.User()

Named values replace all placeholders with the given name

`const $field = $value`[field: "user", value: "Alice".to_json]
# const user = "Alice"

`const $field: $Class = new $Class()`[field: "user", Class: "User"]
# const user: User = new User()

Blocks indent their emitted contents (see Code#indent_chars)

`class $Class {`["User"] do
  `public $field: $type`["name", "string"]

  # multiline nodes still get indented correctly
  `get id() {
    return this.name
  }`

  # nodes can nest arbitrarily deep
  `get $field() {`["upperCaseName"] do
    `return this.name.toUpperCase()`
  end
end

# class User {
#   public name: string
#   get id() {
#     return this.name
#   }
#   get upperCaseName() {
#     return this.name.toUpperCase()
#   }
# }

Blocks close trailing open brackets (defined in Code#block_ending_pairs)

`ids = new Set([`[] do
  `1, 2, 3`
end
# ids = new Set([
#   1, 2, 3
# ])

Returns:

See Also:



106
# File 'lib/cecil/node.rb', line 106

def with(*positional_values, **named_values, &) = raise "Not implemented" # rubocop:disable Lint/UnusedMethodArgument