Class: Brandish::Parser::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/brandish/parser/node.rb,
lib/brandish/parser/node/pair.rb,
lib/brandish/parser/node/root.rb,
lib/brandish/parser/node/text.rb,
lib/brandish/parser/node/block.rb,
lib/brandish/parser/node/string.rb,
lib/brandish/parser/node/command.rb

Overview

A parser node. This is the base for all parser nodes.b All parser nodes are frozen, and so cannot be mutated; all mutations need to be set up as a new node, that is then returned.

Direct Known Subclasses

Block, Command, Pair, Root, Text

Defined Under Namespace

Classes: Block, Command, Pair, Root, String, Text

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location:) ⇒ Node

Initialize the node with the given location. This should be overwritten by a decending node, but all decending nodes should include a location.

Parameters:

  • location (Location)

    The location. See #location.



28
29
30
# File 'lib/brandish/parser/node.rb', line 28

def initialize(location:)
  @location = location
end

Instance Attribute Details

#locationLocation (readonly)

The location of the node.

Returns:

  • (Location)


20
21
22
# File 'lib/brandish/parser/node.rb', line 20

def location
  @location
end

Instance Method Details

#inspect::String

Pretty inspect.

Returns:

  • (::String)


35
36
37
# File 'lib/brandish/parser/node.rb', line 35

def inspect
  "#<#{self.class} location=#{@location}>"
end

#prevent_updateNode

Prevents all calls to #update. This is used on nodes that should never be updated. This is a stop-gap measure for incorrectly configured projects. This, in line with all other methods, creates a duplicate node.

Returns:



65
66
67
68
69
# File 'lib/brandish/parser/node.rb', line 65

def prevent_update
  node = dup
  node.singleton_class.send(:undef_method, :update)
  node
end

#update(attributes) ⇒ Node

"Updates" the node with the given attributes. All of the key-value pairs in the attributes are updated on the node by sending an update to the node.

Examples:

node.update(some: true, value: true)
# this sends `update_some` and `update_value`, each with `true` as
# the argument.  These functions return a node similar to the
# original, but with the requested change.  This is functionally
# similar to this (with some minor differences):
node.update_some(true).update_value(true)

Parameters:

  • attributes ({::Symbol, ::String => ::Object})

    The attributes to update, and the new values for them.

Returns:

  • (Node)

    The updated node, or self if no attributes are given.

Raises:

  • (NodeError)

    if an attribute key is passed that isn't updatable for the node.



55
56
57
# File 'lib/brandish/parser/node.rb', line 55

def update(attributes)
  attributes.inject(self) { |a, (n, v)| a.update_attribute(n, v) }
end

#update_prevented?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/brandish/parser/node.rb', line 71

def update_prevented?
  !respond_to?(:update)
end