Class: LevelUp::GraphBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/models/level_up/graph_builder.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, options = {}) ⇒ GraphBuilder

Returns a new instance of GraphBuilder.



38
39
40
41
# File 'app/models/level_up/graph_builder.rb', line 38

def initialize(target, options = {})
  @target = target
  @configuration = self.class.default_configuration.deep_merge(options)
end

Class Attribute Details

.default_configurationObject (readonly)

Returns the value of attribute default_configuration.



35
36
37
# File 'app/models/level_up/graph_builder.rb', line 35

def default_configuration
  @default_configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



4
5
6
# File 'app/models/level_up/graph_builder.rb', line 4

def configuration
  @configuration
end

#targetObject

Returns the value of attribute target.



3
4
5
# File 'app/models/level_up/graph_builder.rb', line 3

def target
  @target
end

Instance Method Details

#graphObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/level_up/graph_builder.rb', line 43

def graph
  graph = GraphViz.new(:G, type: :digraph)

  graph[:bgcolor] = @configuration[:bgcolor]

  @configuration[:node].each do |k, v|
    graph.node[k] = v unless v.is_a? Hash
  end

  @configuration[:edge].each do |k, v|
    graph.edge[k] = v unless v.is_a? Hash
  end

  tasks = {}
  @target.tasks.each do |task|
    tasks[task] = graph.add_nodes(task.to_s.humanize.downcase)
    if @configuration[:node].has_key? task
      @configuration[:node][task].each do |k, v|
        tasks[task][k] = v
      end
    end
  end

  @target.tasks.each do |task|
    @target.transitions(task).each do |transition|
      edge = graph.add_edges(tasks[task], tasks[transition])
      if @configuration[:edge].has_key? task
        @configuration[:edge][task].each do |k, v|
          edge[k] = v
        end
      end
    end
  end

  graph
end