class JavaClass::Dependencies::YamlSerializer

Serializes a Graph of Nodes to YAML.

Author

Peter Kofler

Public Class Methods

new(options = {:outgoing => :detailed }) click to toggle source

Create a serializer with options hash:

outgoing

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

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

Public Instance Methods

graph_to_yaml(graph) click to toggle source

Return a String of the YAML serialized graph .

# 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
has_yaml?(filename) click to toggle source

Exists a YAML serialized graph ?

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

Load the Graph from YAML filename .

# 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
node_to_yaml(node) click to toggle source

Return a String of the YAML serialized node .

# 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(filename, graph) click to toggle source

Save the graph to YAML filename .

# 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
yaml_to_graph(yaml) click to toggle source

Return a Graph from the YAML data yaml .

# 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

Private Instance Methods

dependencies_to_yaml(dependencies) click to toggle source
# File lib/javaclass/dependencies/yaml_serializer.rb, line 49
def dependencies_to_yaml(dependencies)
  if @options[:outgoing] == :detailed 
    ":\n" + dependencies.map { |dep| "    - #{dep.source}->#{dep.target}" }.join("\n")
  elsif @options[:outgoing] == :summary
    ":\n" + dependencies.map { |dep| dep.target }.uniq.sort.map { |dep| "    - #{dep}" }.join("\n")
  elsif @options[:outgoing] == :none
    ''
  else
    raise "unknown option for outgoing dependencies #{@options[:outgoing]}"
  end
end
node_with(name) click to toggle source
# File lib/javaclass/dependencies/yaml_serializer.rb, line 61
def node_with(name)
  node = @nodes_by_name[name]
  if node
    node
  else
    @nodes_by_name[name] = Node.new(name)
  end
end
yaml_file(filename) click to toggle source
# File lib/javaclass/dependencies/yaml_serializer.rb, line 45
def yaml_file(filename)
  filename + '.yaml'
end