Sha256: 7b624d524f5749846814d0c54e1f2234aed9cae3520edd1e1c38e7ec74124d60

Contents?: true

Size: 1.77 KB

Versions: 11

Compression:

Stored size: 1.77 KB

Contents

# encoding: utf-8
module Infoboxer
  module Tree
    # Base class for all nodes with children.
    class Compound < Node
      def initialize(children = Nodes.new, params = {})
        super(params)
        @children = Nodes[*children]
        @children.each{|c| c.parent = self}
      end

      # List of children
      #
      # @return {Nodes}
      attr_reader :children

      # Index of provided node in children list
      #
      # @return [Fixnum] or `nil` if not a child
      def index_of(child)
        children.index(child)
      end

      # @private
      # Internal, used by {Parser}
      def push_children(*nodes)
        nodes.each{|c| c.parent = self}.each do |n|
          @children << n
        end
      end

      # See {Node#text}
      def text
        children.map(&:text).join(children_separator)
      end

      # See {Node#to_tree}
      def to_tree(level = 0)
        if children.count == 1 && children.first.is_a?(Text)
          "#{indent(level)}#{children.first.text} <#{descr}>\n"
        else
          "#{indent(level)}<#{descr}>\n" +
            children.map(&call(to_tree: level+1)).join
        end
      end

      # Kinda "private" methods, used by Parser only -------------------
      
      # @private
      # Internal, used by {Parser}
      def can_merge?(other)
        false
      end

      # @private
      # Internal, used by {Parser}
      def closed!
        @closed = true
      end

      # @private
      # Internal, used by {Parser}
      def closed?
        @closed
      end

      # @private
      # Internal, used by {Parser}
      def empty?
        children.empty?
      end

      protected

      def children_separator
        ''
      end

      private

      def _eq(other)
        children == other.children
      end      
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
infoboxer-0.2.7 lib/infoboxer/tree/compound.rb
infoboxer-0.2.6 lib/infoboxer/tree/compound.rb
infoboxer-0.2.5 lib/infoboxer/tree/compound.rb
infoboxer-0.2.4 lib/infoboxer/tree/compound.rb
infoboxer-0.2.3 lib/infoboxer/tree/compound.rb
infoboxer-0.2.2 lib/infoboxer/tree/compound.rb
infoboxer-0.2.1 lib/infoboxer/tree/compound.rb
infoboxer-0.2.0 lib/infoboxer/tree/compound.rb
infoboxer-0.1.2.1 lib/infoboxer/tree/compound.rb
infoboxer-0.1.2 lib/infoboxer/tree/compound.rb
infoboxer-0.1.1 lib/infoboxer/tree/compound.rb