Class: Verd::Graph

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



13
14
15
# File 'lib/verd.rb', line 13

def initialize
  @domain = RailsERD::Domain.generate
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



11
12
13
# File 'lib/verd.rb', line 11

def domain
  @domain
end

Instance Method Details

#categoriesObject



48
49
50
51
52
# File 'lib/verd.rb', line 48

def categories
  plain_categories.map do |d|
    {name: d}
  end
end


44
45
46
# File 'lib/verd.rb', line 44

def links
  relations.map(&:verd_link)
end

#modelsObject



26
27
28
29
30
# File 'lib/verd.rb', line 26

def models
  @models ||= relations.each_with_object(Set.new) do |rel, s|
    s << rel.destination.model << rel.source.model
  end.sort_by(&:name)
end

#nodesObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/verd.rb', line 32

def nodes
  models.map do |model|
    category = plain_categories.index(model.source_dir)
    {
      name: model.name,
      category: category,
      schema: model.verd_columns,
      indexes: model.verd_indexes
    }
  end
end

#plain_categoriesObject



54
55
56
57
58
# File 'lib/verd.rb', line 54

def plain_categories
  @categories ||= models.each_with_object(Set.new) do |m, s|
    s << m.source_dir
  end.sort
end

#relationsObject



17
18
19
20
21
22
23
24
# File 'lib/verd.rb', line 17

def relations
  @relations ||= domain.relationships.each_with_object({}) do |rel, rs|
    next if rel.destination.model.nil? || rel.source.model.nil?
   rs[[rel.source.model, rel.destination.model]] = rel
  end.values.sort_by do |rel|
    [rel.source.model.name, rel.destination.model.name]
  end
end

#to_hObject



60
61
62
# File 'lib/verd.rb', line 60

def to_h
  {nodes: nodes, links: links, categories: categories}
end

#to_htmlObject



68
69
70
71
# File 'lib/verd.rb', line 68

def to_html
  tmpl = File.join(__dir__, 'verd', 'template.html')
  File.read(tmpl).sub(/\/\/start-sub.*\/\/end-sub/m, to_json)
end

#to_jsonObject



64
65
66
# File 'lib/verd.rb', line 64

def to_json
  JSON.pretty_generate(to_h)
end

#write_htmlObject



73
74
75
76
# File 'lib/verd.rb', line 73

def write_html
  path = File.join Rails.root.to_s, 'verd.html'
  File.open(path, 'w'){ |f| f.puts to_html }
end