Class: Yoga::Node

Inherits:
Object
  • Object
show all
Includes:
Mixture::Model
Defined in:
lib/yoga/node.rb

Overview

A parser node. This can be subclassed, or used as is.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Node

Initialize the node with the given attributes. The :kind and :location attributes are required.

Parameters:

  • attributes (Hash)

    The attributes.

Options Hash (attributes):

  • :kind (::Symbol)

    The kind of the node.

  • :location (Yoga::Location)

    The location of the node.



24
25
26
27
# File 'lib/yoga/node.rb', line 24

def initialize(attributes)
  self.attributes = attributes
  freeze
end

Instance Attribute Details

#locationYoga::Location (readonly)

The location of the node. This is normally dependant on the locations of the tokens that make up this node.

Returns:



16
# File 'lib/yoga/node.rb', line 16

attribute :location, type: Yoga::Location

Instance Method Details

#==(other) ⇒ Boolean

Returns whether or not the other object is equal to this one. If the other object is this opbject, it returns true; otherwise, if the other object is an instance of this object's class, and the attributes are equal, it returns true; otherwise, it returns false.

Parameters:

  • other (::Object)

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/yoga/node.rb', line 46

def ==(other)
  equal?(other) || (other.is_a?(self.class) && \
    attributes == other.attributes)
end

#prevent_updateYoga::Node

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 #update, creates a duplicate node.

Returns:



57
58
59
60
61
# File 'lib/yoga/node.rb', line 57

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

#update(attributes) ⇒ Object

Creates a new node with the updated attributes. If any unknown attributes are used, it fails.

Parameters:

  • attributes (Hash)

    The attributes.

Options Hash (attributes):

  • :kind (::Symbol)

    The kind of the node.

  • :location (Yoga::Location)

    The location of the node.



34
35
36
# File 'lib/yoga/node.rb', line 34

def update(attributes)
  self.class.new(self.attributes.merge(attributes))
end