lib/ymldot.rb in ymldot-0.0.2 vs lib/ymldot.rb in ymldot-0.0.3
- old
+ new
@@ -20,39 +20,63 @@
@foreignkeys.each{|f| res << "#{f}\\l"}
@columns.each{|c| res << "#{c}\\l"}
res << '}"]'
end
+ def to_csv
+ res = ""
+ res << "#{@name}\n"
+ @foreignkeys.each{|f| res << "#{f}\n"}
+ @columns.each do |c|
+ if c.include? ":"
+ res << "#{c.split(':').join(',')}\n"
+ else
+ res << "#{c}\n"
+ end
+ end
+ res
+ end
+
attr_accessor :name, :dependent, :columns, :foreignkeys
end
class Tables
def initialize(table_node, label=nil, category=false)
@label = label
@category = category
@table_node = table_node
@entity_dict = {}
+ @natural_order_entity = []
table_node.each {|t| eval_entity(t) }
end
attr_accessor :entity_dict, :label, :table_node, :category
- def entity_dict_to_dot
+ def to_dot
res = ""
- s = []
- @entity_dict.each_value{|e| s << e }
+ s = @natural_order_entity
if @category
res << "label=#{@label}\n" if @label
res << "style=invis\n" unless @label
end
- s.sort{|a, b| a.name <=> b.name}.each{|e| res << e.to_dot << "\n"}
+ s.each{|e| res << e.to_dot << "\n"}
res
end
+ def to_csv
+ res = ""
+ @natural_order_entity.each{|e| res << e.to_csv << "\n"}
+ res
+ end
+
private
def eval_entity(table)
columns = table["columns"]; columns ||= []
- @entity_dict[table["name"]] ||= Entity.new(table["name"], table["dependent"], columns)
+ unless @entity_dict[table["name"]]
+ e = Entity.new(table["name"], table["dependent"], columns)
+ @entity_dict[table["name"]] = e
+ @natural_order_entity << e
+ end
end
end
@@ -70,21 +94,29 @@
@one_relations = []
@many_relations = []
eval_yml
end
- def dot_generate
+ def to_dot
code = ""
code += <<"EOS"
digraph #{@file_name} {
#{add_2_tab(config_to_dot)}
-#{add_2_tab(entity_dict_to_dot)}
+#{add_2_tab(entity_to_dot)}
#{add_2_tab(relations_to_dot)}
}
EOS
end
+ def to_csv
+ csv = ""
+ @category.each do |e|
+ csv << e.to_csv
+ end
+ csv
+ end
+
private
def add_2_tab(str)
res = ""
str.each_line{|s| res << " " << s}
res
@@ -95,19 +127,19 @@
res << "graph[overlap=false, splines=true]\n"
res << %Q!node [fontname="#{@config["font"]}"]! if @config["font"]
res
end
- def entity_dict_to_dot
+ def entity_to_dot
res = ""
unless @category.empty?
@category.each_with_index do |c, i|
unless c.category
- res << c.entity_dict_to_dot
+ res << c.to_dot
else
res << "subgraph cluster#{i} {\n"
- res << add_2_tab(c.entity_dict_to_dot)
+ res << add_2_tab(c.to_dot)
res << "}\n"
end
end
end
res
@@ -142,16 +174,18 @@
eval_relation_polymorphic(foreignkeys["polymorphic"], tname) if foreignkeys["polymorphic"]
end
def eval_relation_has_many(keys, tname)
keys.each do |rel|
+ raise "Error Relation! has many #{tname} to #{rel}" if @entity_dict[rel] == nil
@entity_dict[rel].foreignkeys << "#{singularize(tname)}ID(FK)"
@many_relations << {:self => @entity_dict[tname], :have => @entity_dict[rel]}
end
end
def eval_relation_has_one(keys, tname)
keys.each do |rel|
+ raise "Error Relation! has one #{tname} to #{rel}" if @entity_dict[rel] == nil
@entity_dict[rel].foreignkeys << "#{singularize(tname)}ID(FK)"
@one_relations << {:self => @entity_dict[tname], :have => @entity_dict[rel]}
end
end