sequel-schema-dot-generator 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -36,3 +36,10 @@ Or install it yourself as:
36
36
  3. Commit your changes (`git commit -am 'Add some feature'`)
37
37
  4. Push to the branch (`git push origin my-new-feature`)
38
38
  5. Create new Pull Request
39
+
40
+ ## Changelog
41
+
42
+ ### 0.0.2
43
+ - optional colored association edges to distinguish them in big intersections (`params[:colored_associations]`)
44
+ - multiple foreign table support
45
+ - columns has written types in diagram
@@ -2,6 +2,7 @@ require 'sequel/schema/dot/generator/version'
2
2
  require 'erb'
3
3
  require 'logger'
4
4
  require 'sequel'
5
+ require 'active_support/inflector'
5
6
 
6
7
  module Sequel
7
8
  module Schema
@@ -9,9 +10,10 @@ module Sequel
9
10
  # Module for generating Sequel schema structure into Dot language format
10
11
  module Generator
11
12
  class Generator
12
- @db = nil
13
- @dot_template_path = nil
13
+ @db = nil
14
+ @dot_template_path = nil
14
15
  @logger = nil
16
+ @colored_associations = nil
15
17
 
16
18
  # @todo Consider write the params as block
17
19
  #
@@ -19,39 +21,85 @@ module Sequel
19
21
  # :db [Sequel::Database] required From this one will be generated the diagram
20
22
  # :logger [Logger] optional Will be used for logging
21
23
  # :dot_template_path [String] optional Template is supposed to be ERB template. Will be used as template for resulting Dot file
24
+ # :colored_associations [Boolean] optional Whether lines representing associations will be draw in color (=False)
22
25
  #
23
26
  def initialize params
24
27
  check_params params
25
28
 
26
29
  @db = params[:db]
27
30
  @logger = params[:logger] || Logger.new(STDERR)
28
- @dot_template_path = params[:dot_template_path] || File.join(File.dirname(__FILE__), 'generator/diagram.dot.erb')
31
+ @dot_template_path = params[:dot_template_path] || File.join(File.dirname(__FILE__), 'generator/diagram.dot.erb')
32
+ @colored_associations = params[:colored_associations]
29
33
  end
30
34
 
31
35
  # @return [String] Content which can be passed to dot parsing tool
32
36
  def generate
33
37
  relations = []
34
38
  tables = []
35
-
39
+
36
40
  @db.tables.each do |table_name|
37
41
  next if table_name == :schema_info
38
- tables << [table_name, @db[table_name]]
42
+ tables << [table_name, @db.schema(table_name)]
39
43
 
40
44
  # foreign keys by <table>_id name format
41
45
  # @todo Read it from model
42
- @db[table_name].columns.each do |column_name|
43
- next unless column_name =~ /_id$/
46
+ @db[table_name].columns.select{|cn|cn=~/_id$/}.each do |column_name|
44
47
  foreign_column = column_name.to_s
45
- foreign_table = foreign_column.gsub(/_id$/, '')
48
+ foreign_table = existing_table_name foreign_column.gsub(/_id$/, '')
46
49
  relations << [foreign_table, 'id', table_name, foreign_column]
47
50
  end
48
51
  end
49
52
 
53
+ if @colored_associations
54
+ edge_colors = random_color_set relations.count
55
+ else
56
+ edge_colors = Array.new relations.count, '000000'
57
+ end
58
+
50
59
  ERB.new( File.read(@dot_template_path),nil,'>' ).result(binding)
51
60
  end
52
61
 
53
62
  private
54
63
 
64
+ # @param [String] name
65
+ #
66
+ # @return [String] form of name param in which it is name of existing table
67
+ def existing_table_name name
68
+ tables = @db.tables.map(&:to_s)
69
+ table_name = name if tables.include? name
70
+ table_name ||= name.pluralize if tables.include? name.pluralize
71
+ table_name ||= name.singularize if tables.include? name.singularize
72
+ table_name ||= 'association_table_not_found'
73
+
74
+ table_name
75
+ end
76
+
77
+ # @param [Integer] number of colors we want to generate
78
+ #
79
+ # @return [Array] of random and unique colors
80
+ def random_color_set number
81
+ raise 'Number of colors must be greater than 0' if number < 1
82
+ colors = []
83
+ (1..number).each do
84
+ colors << random_unique_hex_color(colors)
85
+ end
86
+
87
+ colors
88
+ end
89
+
90
+
91
+ # @param [Array] existing colors which should be avoided
92
+ #
93
+ # @return [String] Random color which is not presented in existing param
94
+ def random_unique_hex_color existing
95
+ color = '%06x' % (rand * 0xffffff)
96
+ if existing.include?(color)
97
+ random_unique_hex_color existing
98
+ else
99
+ color
100
+ end
101
+ end
102
+
55
103
  # Checks if all parameters for new Object are alright
56
104
  def check_params params
57
105
  unless params[:db].is_a?Sequel::Database
@@ -63,6 +111,9 @@ module Sequel
63
111
  unless params[:logger].nil? || params[:logger].is_a?(Logger)
64
112
  raise 'Logger is supposed to be... Logger, know. %s given'%params[:logger].inspect
65
113
  end
114
+ unless params[:colored_associations].nil? || !!params[:colored_associations] == params[:colored_associations]
115
+ raise 'Colored association is supposed to be boolean. %s given'%params[:colored_associations].inspect
116
+ end
66
117
  end
67
118
  end
68
119
  end
@@ -1,18 +1,18 @@
1
1
  digraph G {
2
2
  node [shape=none,fontname="DejaVu Sans"];
3
- <% tables.each do |table_name, table| %>
3
+ <% tables.each do |table_name, table_schema| %>
4
4
  <%= table_name %> [label=<
5
5
  <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
6
6
  <tr><td bgcolor="lightgrey" PORT="users"><b><%= table_name %></b></td></tr>
7
- <% table.columns.each do |column_name| %>
8
- <tr><td PORT="<%= column_name %>"><%= column_name %></td></tr>
7
+ <% table_schema.each do |column_name, column_schema| %>
8
+ <tr><td PORT="<%= column_name %>"><%= column_name %><font color="gray">(<%= column_schema[:type] %>)</font></td></tr>
9
9
  <% end %>
10
10
  </table>
11
11
  >]
12
12
  <% end %>
13
13
 
14
- <% relations.each do |from_tab, from_col, to_tab, to_col| %>
15
- <%= '"%s":"%s" -> "%s":"%s"'%[from_tab, from_col, to_tab, to_col] %>
14
+ <% relations.each_with_index do |(from_tab, from_col, to_tab, to_col), index| %>
15
+ <%= '"%s":"%s" -> "%s":"%s" [color="#%s"];'%[from_tab, from_col, to_tab, to_col, edge_colors[index]] %>
16
16
 
17
17
  <% end %>
18
18
  }
@@ -2,7 +2,7 @@ module Sequel
2
2
  module Schema
3
3
  module Dot
4
4
  module Generator
5
- VERSION = '0.0.1'
5
+ VERSION = '0.0.2'
6
6
  end
7
7
  end
8
8
  end
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency 'rake'
23
23
 
24
24
  spec.add_dependency 'sequel'
25
+ spec.add_dependency 'active_support'
25
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-schema-dot-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-03 00:00:00.000000000 Z
12
+ date: 2013-03-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: active_support
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
62
78
  description: This gem makes it easier to generate database schema overview image
63
79
  email:
64
80
  - development@rooland.cz
@@ -91,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
107
  version: '0'
92
108
  segments:
93
109
  - 0
94
- hash: 2048490745226108490
110
+ hash: -3561675813469084963
95
111
  required_rubygems_version: !ruby/object:Gem::Requirement
96
112
  none: false
97
113
  requirements:
@@ -100,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
116
  version: '0'
101
117
  segments:
102
118
  - 0
103
- hash: 2048490745226108490
119
+ hash: -3561675813469084963
104
120
  requirements: []
105
121
  rubyforge_project:
106
122
  rubygems_version: 1.8.25