Sha256: 273f8ad9ca34fd439ba4974fc7d130ba2a860c7d53b47bd8208794dbf8663b77

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module SGF

  class Node

    attr_accessor :parent, :children, :properties

    # Creates a new node. Arguments which can be passed in are:
    # :parent => parent_node (nil by default)
    # :children => [list, of, children] (empty array by default)
    # :properties => {hash_of => properties} (empty hash by default)
    def initialize args={}
      @parent = args[:parent]
      @children = []
      add_children args[:children] if args[:children]
      @properties = Hash.new
      @properties.merge! args[:properties] if args[:properties]
    end

    def add_children *nodes
      nodes.flatten!
      raise "Non-node child given!" if nodes.any? { |node| node.class != Node }
      nodes.each { |node| node.parent = self }
      @children.concat nodes
    end

    def add_properties hash
      hash.each do |key, value|
        @properties[key] ||= ""
        @properties[key].concat value
      end
    end

    def each_child
      @children.each { |child| yield child }
    end

    def == other_node
      @properties == other_node.properties
    end

    def comments
      @properties["C"]
    end

    alias :comment :comments

    private

    def method_missing method_name, *args
      property = method_name.to_s.upcase
      if property[/(.*?)=$/]
        @properties[$1] = args[0]
      else
        output = @properties[property]
        super(method_name, args) if output.nil?
        output
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
SgfParser-1.0.0 lib/sgf/node.rb