Module: SchemaRD::Metadata

Defined in:
lib/schemard/metadata.rb

Defined Under Namespace

Classes: Parser, Writer

Class Method Summary collapse

Class Method Details

.add_relations(table_name, type, relation_table_names, schema) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/schemard/metadata.rb', line 76

def self.add_relations(table_name, type, relation_table_names, schema)
  return unless relation_table_names
  relation_table_names = relation_table_names.split(",") if relation_table_names.is_a?(String)

  relation_table_names.map{|rel_table_name| schema.table(rel_table_name) }.compact.each do |rel_table|
    parent_table = type == "belongs_to" ? rel_table : schema.table(table_name)
    child_table = type == "belongs_to" ? schema.table(table_name) : rel_table

    if parent_table.relation_to(child_table.name).nil?
      schema.add_relation(TableRelation.new(parent_table: parent_table, child_table: child_table))
    end
    parent_table.relation_to(child_table.name).child_cardinality = "1" if type == "has_one"
  end
end

.load(config: nil, lang: "ja", schema: nil) ⇒ Object

“config”, “lang”, “schema” must be assigned, but ruby2.0 needs default value.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/schemard/metadata.rb', line 8

def self.load(config: nil, lang: "ja", schema: nil)
   = Parser.new(config.output_file, *config.).parse()
  localizer = SchemaRD::Utils::SchemaLocalizer.new(lang, )
  # localized_name を設定
  schema.tables.each do |table|
    table.localized_name = localizer.table_name(table.name)
    table.columns.each do |column|
      column.localized_name = localizer.column_name(table.name, column.name)
    end
  end
  # set position, and relations
  (["tables"] || {}).each do |table_name, hash|
    # skip when table name exists in metadata only.
    next unless schema.table(table_name)
    if hash["position_left"] && hash["position_top"]
      schema.table(table_name).position =
        { "left" => hash["position_left"], "top" => hash["position_top"] }
    end
    self.add_relations(table_name, "belongs_to", hash["belongs_to"], schema)
    self.add_relations(table_name, "has_many",   hash["has_many"], schema)
    self.add_relations(table_name, "has_one",    hash["has_one"], schema)
  end
  # db_comment にメタ情報が含まれる場合に設定
  if config.parse_db_comment?
    schema.tables.each do |table|
      if table.parsed_db_comment && table.parsed_db_comment.strip != ""
        case config.parse_db_comment_as
        when 'name', 'localized_name'
          table.localized_name = table.parsed_db_comment.strip
        when 'description'
          table.description = table.parsed_db_comment.strip
        when 'custom'
          config.db_comment_parser.call(table: table)
        end
      end
      table.columns
      .select{|c| c.parsed_db_comment && c.parsed_db_comment.strip != ""}.each do |column|
        case config.parse_db_comment_as
        when 'name', 'localized_name'
          column.localized_name = column.parsed_db_comment.strip
        when 'description'
          column.description = column.parsed_db_comment.strip
        when 'custom'
          config.db_comment_parser.call(column: column)
        end
      end
    end
  end
  # RDocコメントとしてメタ情報が含まれる場合に設定
  if config.rdoc_enabled
    rdoc = SchemaRD::RDocParser.new(config.input_file)
    schema.tables.select{|t| rdoc.table_comment(t.name) }.each do |table|
      parser = DefaultTableCommentParser.new(rdoc.table_comment(table.name))
      table.localized_name = parser.localized_name if parser.has_localized_name?
      table.description = parser.description if parser.has_description?

      %i(belongs_to has_many has_one).each do |rel_type|
        if parser.has_relation_of?(rel_type)
          self.add_relations(table.name, rel_type.to_s, parser.relation_of(rel_type), schema)
        end
      end
    end
  end
  # output_file がなければ作成
  Writer.new(config.output_file).save_all(schema.tables) unless File.exist?(config.output_file)
  schema
end