Sha256: c0ff0a6ee227d5f6f9ef1db562f6bd65407502d21cf3fc049bc29e21e3d1005f

Contents?: true

Size: 1.9 KB

Versions: 7

Compression:

Stored size: 1.9 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

require "mixture"

module Yoga
  # A parser node.  This can be subclassed, or used as is.
  class Node
    include Mixture::Model

    # @!attribute [r] location
    #   The location of the node.  This is normally dependant on the locations
    #   of the tokens that make up this node.
    #
    #   @return [Yoga::Location]
    attribute :location, type: Yoga::Location

    # Initialize the node with the given attributes.  The `:kind` and
    # `:location` attributes are required.
    #
    # @param attributes [Hash] The attributes.
    # @option attributes [::Symbol] :kind The kind of the node.
    # @option attributes [Yoga::Location] :location The location of the node.
    def initialize(attributes)
      self.attributes = attributes
      freeze
    end

    # Creates a new node with the updated attributes.  If any unknown
    # attributes are used, it fails.
    #
    # @param (see #initialize)
    # @option (see #initialize)
    def update(attributes)
      self.class.new(self.attributes.merge(attributes))
    end

    # 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.
    #
    # @param other [::Object]
    # @return [Boolean]
    def ==(other)
      equal?(other) || (other.is_a?(self.class) && \
        attributes == other.attributes)
    end

    # 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.
    #
    # @return [Yoga::Node]
    def prevent_update
      node = dup
      node.singleton_class.send(:undef_method, :update)
      node
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
yoga-0.5.0 lib/yoga/node.rb
yoga-0.4.2 lib/yoga/node.rb
yoga-0.4.1 lib/yoga/node.rb
yoga-0.3.2 lib/yoga/node.rb
yoga-0.3.0 lib/yoga/node.rb
yoga-0.2.1 lib/yoga/node.rb
yoga-0.2.0 lib/yoga/node.rb