Sha256: 9bb6fc48ee65318b6ccb2c4f629e2d73916393c19b550ab2f6ebb616e373a894

Contents?: true

Size: 695 Bytes

Versions: 1

Compression:

Stored size: 695 Bytes

Contents

module Dotremap::XmlTree
  attr_reader :parent

  def add_child(*objects)
    objects.each do |object|
      childs << object
    end

    childs.each do |child|
      child.parent = self
    end
  end

  def search_childs(klass)
    childs.select { |c| c.is_a?(klass) }
  end

  def to_xml(distance_between_childs = 0)
    tag_name = self.class.to_s.split("::").last.downcase
    newline_count = distance_between_childs + 1

    [
      "<#{tag_name}>",
      childs.map(&:to_xml).join("\n" * newline_count).gsub(/^/, "  "),
      "</#{tag_name}>",
    ].join("\n")
  end

  protected

  attr_writer :parent

  private

  def childs
    @childs ||= []
  end

  def parent
    @parent
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dotremap-0.0.8 lib/dotremap/xml_tree.rb