lib/ymldot.rb in ymldot-0.0.5 vs lib/ymldot.rb in ymldot-0.0.6

- old
+ new

@@ -65,10 +65,14 @@ res = "" @natural_order_entity.each{|e| res << e.to_csv << "\n"} res end + def add_entity(table) + eval_entity(table) + end + private def eval_entity(table) columns = table["columns"]; columns ||= [] unless @entity_dict[table["name"]] e = Entity.new(table["name"], table["dependent"], columns) @@ -126,12 +130,18 @@ res = "" res << "graph [overlap=false, splines=true]\n" if @config["size"] && @config["size"]["x"] && @config["size"]["y"] res << %Q!graph [size="#{(@config["size"]["x"]/2.54).round},#{(@config["size"]["y"]/2.54).round}"]\n! end - raise "Error: please seto 'font'" if @config["ja"] && @config["font"] - res << %Q!node [fontname="#{@config["font"]}"]! if @config["font"] + raise "Error: please set 'font'" if @config["ja"] && @config["font"] + + if @config["font"] || @config["fontsize"] + res << "node [" + res << %Q!fontname="#{@config["font"]}"! if @config["font"] + res << %Q!, fontsize=#{@config["fontsize"]}! if @config["fontsize"] + res << "]" + end res end def entity_to_dot res = "" @@ -163,20 +173,20 @@ def eval_yml @config = @node["config"]? @node["config"] : {} @category << Tables.new(@node["tables"]) if @node["tables"] and !@node["tables"].empty? @node["category"].each{|c| @category << Tables.new(c["tables"], c["label"], true) } if @node["category"] @category.each{|c| c.entity_dict.each_pair{|k, v| @entity_dict[k] = v}} - @category.each{|c| c.table_node.each{|e| eval_relation(e) } } + @category.each{|c| c.table_node.each{|e| eval_relation(e, c) } } end - def eval_relation(table) + def eval_relation(table, category) foreignkeys = table["foreignkeys"] tname = table["name"] return unless foreignkeys eval_relation_has_many(foreignkeys["has_many"], tname) if foreignkeys["has_many"] eval_relation_has_one(foreignkeys["has_one"], tname) if foreignkeys["has_one"] - eval_relation_hmabt(foreignkeys["has_many_and_belongs_to"], tname) if foreignkeys["has_many_and_belongs_to"] + eval_relation_habtm(foreignkeys["has_and_belongs_to_many"], tname, category) if foreignkeys["has_and_belongs_to_many"] eval_relation_polymorphic(foreignkeys["polymorphic"], tname) if foreignkeys["polymorphic"] end def eval_relation_has_many(keys, tname) keys.each do |rel| @@ -192,26 +202,25 @@ @entity_dict[rel].foreignkeys << append_fk_str(singularize(tname)) @one_relations << {:self => @entity_dict[tname], :have => @entity_dict[rel]} end end - def eval_relation_hmabt(keys, tname) + def eval_relation_habtm(keys, tname, category) keys.each do |rel| - join_tname = "#{rel}_#{tname}" - return if @entity_dict.has_key? join_tname - join_tname = "#{tname}_#{rel}" - @entity_dict[join_tname] ||= Entity.new(join_tname, true) - @entity_dict[join_tname].foreignkeys << append_fk_str(singularize(tname)) - @entity_dict[join_tname].foreignkeys << append_fk_str(singularize(rel)) + join_tname = [rel, tname].sort.join("_") + return if category.entity_dict.has_key? join_tname # make new category keys = [] keys << append_fk_str(tname) keys << append_fk_str(rel) - @category << Tables.new([{"name" => join_tname, "columns" => keys}]) - - @many_relations << {:self => @entity_dict[tname], :have => @entity_dict[join_tname]} - @many_relations << {:self => @entity_dict[rel], :have => @entity_dict[join_tname]} + + category.add_entity("name" => join_tname) + category.entity_dict[join_tname].foreignkeys << append_fk_str(singularize(tname)) + category.entity_dict[join_tname].foreignkeys << append_fk_str(singularize(rel)) + + @many_relations << {:self => category.entity_dict[tname], :have => category.entity_dict[join_tname]} + @many_relations << {:self => category.entity_dict[rel], :have => category.entity_dict[join_tname]} end end def eval_relation_polymorphic(keys, tname) keys.each do |rel|