Class JavaClass::Dependencies::YamlSerializer
In: lib/javaclass/dependencies/yaml_serializer.rb
Parent: Object

Serializes a Graph of Nodes to YAML.

Author:Peter Kofler

Methods

Public Class methods

Create a serializer with options hash:

outgoing:how to persist outgoing dependencies, either :detailed, :summary or :none

[Source]

# File lib/javaclass/dependencies/yaml_serializer.rb, line 15
      def initialize(options = {:outgoing => :detailed })
        @options = options
      end

Public Instance methods

Return a String of the YAML serialized graph .

[Source]

# File lib/javaclass/dependencies/yaml_serializer.rb, line 30
      def graph_to_yaml(graph)
        "---\n" +
        graph.to_a.map { |node| node_to_yaml(node) }.join("\n")
      end

Exists a YAML serialized graph ?

[Source]

# File lib/javaclass/dependencies/yaml_serializer.rb, line 20
      def has_yaml?(filename)
        File.exist? yaml_file(filename)
      end

Load the Graph from YAML filename .

[Source]

# File lib/javaclass/dependencies/yaml_serializer.rb, line 73
      def load(filename)
        yaml = File.open(yaml_file(filename)) { |f| YAML.load(f) }
        # TODO support compressed yaml files, e.g. inside zip
        yaml_to_graph(yaml)
      end

Return a String of the YAML serialized node .

[Source]

# File lib/javaclass/dependencies/yaml_serializer.rb, line 36
      def node_to_yaml(node)
        node.name + ":\n" +
        node.dependencies.keys.map { |dep|
          '  ' + dep.name + dependencies_to_yaml(node.dependencies[dep])
        }.join("\n")
      end

Save the graph to YAML filename .

[Source]

# File lib/javaclass/dependencies/yaml_serializer.rb, line 25
      def save(filename, graph)
        File.open(yaml_file(filename), 'w') { |f| f.print graph_to_yaml(graph) }
      end

Return a Graph from the YAML data yaml .

[Source]

# File lib/javaclass/dependencies/yaml_serializer.rb, line 80
      def yaml_to_graph(yaml)
        graph = Graph.new
        @nodes_by_name = {}

        yaml.keys.each do |name|
          node = node_with(name)
          graph.add(node)

          dependency_map = yaml[name] || {}
          dependency_map.keys.each do |dependency_name|
            depending_node = node_with(dependency_name)
            dependencies = dependency_map[dependency_name].collect { |d| Edge.new(*d.split(/->/)) }
            node.add_dependencies(dependencies, [depending_node])
          end
        end
        
        @nodes_by_name = {}
        graph
      end

[Validate]