ymldot 0.0.2 → 0.0.3

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.
Files changed (3) hide show
  1. data/bin/ymldot +6 -2
  2. data/lib/ymldot.rb +44 -10
  3. metadata +2 -2
data/bin/ymldot CHANGED
@@ -11,17 +11,21 @@ require 'optparse'
11
11
 
12
12
  Version = "0.0.2"
13
13
  is_output = false
14
+ is_csv = false
14
15
  opt = OptionParser.new
15
16
  opt.version = Version
16
17
  opt.release = "0.0.1"
17
18
  opt.on('-o', 'output file to current directory. name is "#{input_filename}.dot"') {|v| is_output = true }
19
+ opt.on('-c', '--csv', 'csv format output') {|v| is_csv = true }
18
20
  opt.parse!(ARGV)
19
21
 
20
22
  if is_output
21
23
  y = Ymldot.new(ARGV[0])
22
24
  open("./#{y.file_name}.dot", "w") do |f|
23
- f.write(y.dot_generate)
25
+ f.write(y.to_dot)
24
26
  end
27
+ elsif is_csv
28
+ puts Ymldot.new(ARGV[0]).to_csv
25
29
  else
26
- puts Ymldot.new(ARGV[0]).dot_generate
30
+ puts Ymldot.new(ARGV[0]).to_dot
27
31
  end
data/lib/ymldot.rb CHANGED
@@ -22,6 +22,20 @@ class Entity
22
22
  res << '}"]'
23
23
  end
24
24
 
25
+ def to_csv
26
+ res = ""
27
+ res << "#{@name}\n"
28
+ @foreignkeys.each{|f| res << "#{f}\n"}
29
+ @columns.each do |c|
30
+ if c.include? ":"
31
+ res << "#{c.split(':').join(',')}\n"
32
+ else
33
+ res << "#{c}\n"
34
+ end
35
+ end
36
+ res
37
+ end
38
+
25
39
  attr_accessor :name, :dependent, :columns, :foreignkeys
26
40
  end
27
41
 
@@ -31,26 +45,36 @@ class Tables
31
45
  @category = category
32
46
  @table_node = table_node
33
47
  @entity_dict = {}
48
+ @natural_order_entity = []
34
49
  table_node.each {|t| eval_entity(t) }
35
50
  end
36
51
  attr_accessor :entity_dict, :label, :table_node, :category
37
52
 
38
- def entity_dict_to_dot
53
+ def to_dot
39
54
  res = ""
40
- s = []
41
- @entity_dict.each_value{|e| s << e }
55
+ s = @natural_order_entity
42
56
  if @category
43
57
  res << "label=#{@label}\n" if @label
44
58
  res << "style=invis\n" unless @label
45
59
  end
46
- s.sort{|a, b| a.name <=> b.name}.each{|e| res << e.to_dot << "\n"}
60
+ s.each{|e| res << e.to_dot << "\n"}
61
+ res
62
+ end
63
+
64
+ def to_csv
65
+ res = ""
66
+ @natural_order_entity.each{|e| res << e.to_csv << "\n"}
47
67
  res
48
68
  end
49
69
 
50
70
  private
51
71
  def eval_entity(table)
52
72
  columns = table["columns"]; columns ||= []
53
- @entity_dict[table["name"]] ||= Entity.new(table["name"], table["dependent"], columns)
73
+ unless @entity_dict[table["name"]]
74
+ e = Entity.new(table["name"], table["dependent"], columns)
75
+ @entity_dict[table["name"]] = e
76
+ @natural_order_entity << e
77
+ end
54
78
  end
55
79
 
56
80
  end
@@ -72,17 +96,25 @@ class Ymldot
72
96
  eval_yml
73
97
  end
74
98
 
75
- def dot_generate
99
+ def to_dot
76
100
  code = ""
77
101
  code += <<"EOS"
78
102
  digraph #{@file_name} {
79
103
  #{add_2_tab(config_to_dot)}
80
- #{add_2_tab(entity_dict_to_dot)}
104
+ #{add_2_tab(entity_to_dot)}
81
105
  #{add_2_tab(relations_to_dot)}
82
106
  }
83
107
  EOS
84
108
  end
85
109
 
110
+ def to_csv
111
+ csv = ""
112
+ @category.each do |e|
113
+ csv << e.to_csv
114
+ end
115
+ csv
116
+ end
117
+
86
118
  private
87
119
  def add_2_tab(str)
88
120
  res = ""
@@ -97,15 +129,15 @@ EOS
97
129
  res
98
130
  end
99
131
 
100
- def entity_dict_to_dot
132
+ def entity_to_dot
101
133
  res = ""
102
134
  unless @category.empty?
103
135
  @category.each_with_index do |c, i|
104
136
  unless c.category
105
- res << c.entity_dict_to_dot
137
+ res << c.to_dot
106
138
  else
107
139
  res << "subgraph cluster#{i} {\n"
108
- res << add_2_tab(c.entity_dict_to_dot)
140
+ res << add_2_tab(c.to_dot)
109
141
  res << "}\n"
110
142
  end
111
143
  end
@@ -144,6 +176,7 @@ EOS
144
176
 
145
177
  def eval_relation_has_many(keys, tname)
146
178
  keys.each do |rel|
179
+ raise "Error Relation! has many #{tname} to #{rel}" if @entity_dict[rel] == nil
147
180
  @entity_dict[rel].foreignkeys << "#{singularize(tname)}ID(FK)"
148
181
  @many_relations << {:self => @entity_dict[tname], :have => @entity_dict[rel]}
149
182
  end
@@ -151,6 +184,7 @@ EOS
151
184
 
152
185
  def eval_relation_has_one(keys, tname)
153
186
  keys.each do |rel|
187
+ raise "Error Relation! has one #{tname} to #{rel}" if @entity_dict[rel] == nil
154
188
  @entity_dict[rel].foreignkeys << "#{singularize(tname)}ID(FK)"
155
189
  @one_relations << {:self => @entity_dict[tname], :have => @entity_dict[rel]}
156
190
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ymldot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - nari
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-09-29 00:00:00 +09:00
12
+ date: 2008-10-21 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15