class XMLConverter

Constants

XML_TEMPLATE

Attributes

doc[R]
tree_root[R]

Public Class Methods

dump(tree, with_path=false) click to toggle source
# File lib/dumb_down_viewer/visitor.rb, line 143
def self.dump(tree, with_path=false)
  visitor = new.tap do |v|
    v.create_doc
    v.visit(tree, with_path)
  end

  visitor.dump(tree, with_path)
end

Public Instance Methods

create_doc() click to toggle source
# File lib/dumb_down_viewer/visitor.rb, line 165
def create_doc
  @doc = Nokogiri::XML(XML_TEMPLATE).tap do |doc|
    @tree_root = Nokogiri::XML::Node.new('tree'.freeze, doc)
    @tree_root.parent = doc
  end
end
dump(tree, with_path) click to toggle source
# File lib/dumb_down_viewer/visitor.rb, line 172
def dump(tree, with_path)
  file_regexp = /#{Regexp.escape("> <\/file>")}/
  visit(tree, with_path).parent = @tree_root
  @doc.to_xml.gsub(file_regexp, '></file>')
end
visit(node, with_path) click to toggle source
# File lib/dumb_down_viewer/visitor.rb, line 152
def visit(node, with_path)
  case node
  when DirNode
    create_dir_element(node, with_path).tap do |elm|
      node.sub_nodes.each do |n|
        n.accept(self, with_path).parent = elm
      end
    end
  when FileNode
    create_file_element(node, with_path)
  end
end

Private Instance Methods

create_dir_element(node, with_path) click to toggle source
# File lib/dumb_down_viewer/visitor.rb, line 180
def create_dir_element(node, with_path)
  Nokogiri::XML::Node.new('directory'.freeze, @doc).tap do |elm|
    elm['name'.freeze] = name_value(node, with_path)
  end
end
create_file_element(node, with_path) click to toggle source
# File lib/dumb_down_viewer/visitor.rb, line 186
def create_file_element(node, with_path)
  Nokogiri::XML::Node.new('file'.freeze, @doc).tap do |elm|
    elm['name'.freeze] = name_value(node, with_path)
    elm.content = ' '.freeze
  end
end
name_value(node, with_path) click to toggle source
# File lib/dumb_down_viewer/visitor.rb, line 193
def name_value(node, with_path)
  if with_path
    File.join(node.directory, node.name)
  else
    node.name
  end
end