Class: Revocare::DataWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/revocare/data_writer.rb

Constant Summary collapse

GRAPHVIZ_FORMAT =
:graphviz
DEFAULT_FORMAT =
GRAPHVIZ_FORMAT
SUPPORTED_FORMATS =
[
  GRAPHVIZ_FORMAT,
].freeze
DEFAULT_DIRECTORY =
"./callbacks"
DEFAULT_EXTENSION =
"pdf"

Instance Method Summary collapse

Constructor Details

#initialize(data:, format: DEFAULT_FORMAT) ⇒ DataWriter

Returns a new instance of DataWriter.



13
14
15
16
17
18
# File 'lib/revocare/data_writer.rb', line 13

def initialize(data:, format: DEFAULT_FORMAT)
  @graphs = []
  @data = data
  @format = format
  validate_format!
end

Instance Method Details

#formatObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/revocare/data_writer.rb', line 20

def format
  if @format == GRAPHVIZ_FORMAT
    if Kernel.system("which dot")
      puts "Graphviz libraries found"
    else
      raise "Graphviz libraries are not present"
    end
  end

  @format
end

#write_to_directory(extension: DEFAULT_EXTENSION) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/revocare/data_writer.rb', line 32

def write_to_directory(extension: DEFAULT_EXTENSION)
  FileUtils.mkdir_p(DEFAULT_DIRECTORY)

  if format == GRAPHVIZ_FORMAT
    data.each_with_index do |model_data, index|
      @current_graph = GraphViz.new(:G, type: :digraph, rankdir: "LR")

      model_name = model_data[:model]
      model_node = add_model_node(model_name)

      model_data[:callbacks].each do |callback|
        name, chain = callback.values_at(:callback_name, :callback_chain)
        name_node = add_callback_node(name, index)

        method_nodes = chain.each_with_index.map do |method, order|
          add_callback_chain_node(method, index, order)
        end

        current_graph.add_edge(model_node, name_node)
        current_graph.add_edges(name_node, method_nodes)
      end

      filename = "#{DEFAULT_DIRECTORY}/#{model_name.downcase}.#{extension}"
      current_graph.output(extension => filename)
    end
  end
end