Class: JavaClass::Dependencies::YamlSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/javaclass/dependencies/yaml_serializer.rb

Overview

Serializes a Graph of Nodes to YAML.

Author

Peter Kofler

Instance Method Summary collapse

Constructor Details

#initialize(options = {:outgoing => :detailed }) ⇒ YamlSerializer

Create a serializer with options hash:

outgoing

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



15
16
17
# File 'lib/javaclass/dependencies/yaml_serializer.rb', line 15

def initialize(options = {:outgoing => :detailed })
  @options = options
end

Instance Method Details

#graph_to_yaml(graph) ⇒ Object

Return a String of the YAML serialized graph .



30
31
32
33
# 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) ⇒ Boolean

Exists a YAML serialized graph ?

Returns:

  • (Boolean)


20
21
22
# File 'lib/javaclass/dependencies/yaml_serializer.rb', line 20

def has_yaml?(filename)
  File.exist? yaml_file(filename)
end

#load(filename) ⇒ Object

Load the Graph from YAML filename .



73
74
75
76
77
# 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) ⇒ Object

Return a String of the YAML serialized node .



36
37
38
39
40
41
# 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) ⇒ Object

Save the graph to YAML filename .



25
26
27
# 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) ⇒ Object

Return a Graph from the YAML data yaml .



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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