Class: SchemaComments::SchemaComment

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_comments/schema_comment.rb

Defined Under Namespace

Classes: SortedStore

Constant Summary collapse

TABLE_KEY =
'table_comments'
COLUMN_KEY =
'column_comments'

Class Method Summary collapse

Class Method Details

.activerecord_commentsObject



86
87
88
89
90
91
92
93
# File 'lib/schema_comments/schema_comment.rb', line 86

def activerecord_comments
  {
    'activerecord' => {
      'models' => model_comments,
      'attributes' => attribute_comments,
    }
  }
end

.attribute_commentsObject



77
78
79
80
81
82
83
84
# File 'lib/schema_comments/schema_comment.rb', line 77

def attribute_comments
  yaml_read{|db| db[COLUMN_KEY] }.each_with_object({}) do |(k,v),d|
    d[k.singularize] = v.each_with_object({}) do |(name, comment), dd|
      dd[name.sub(/_id\z/, '')] = comment.sub(/id\z/i, '') if name =~ /_id\z/
      dd[name] = comment
    end
  end
end

.clear_cacheObject



99
100
101
102
103
# File 'lib/schema_comments/schema_comment.rb', line 99

def clear_cache
  @table_names = nil
  @column_names = nil
  self
end

.column_comment(table_name, column_name) ⇒ Object



18
19
20
21
22
# File 'lib/schema_comments/schema_comment.rb', line 18

def column_comment(table_name, column_name)
  @column_names ||= yaml_read{|db| db[COLUMN_KEY] }.dup
  column_hash = @column_names[table_name.to_s] || {}
  column_hash[column_name.to_s]
end

.column_comments(table_name) ⇒ Object



24
25
26
27
28
29
# File 'lib/schema_comments/schema_comment.rb', line 24

def column_comments(table_name)
  result = nil
  @column_names ||= yaml_read{|db| db[COLUMN_KEY] }.dup
  result = @column_names[table_name.to_s]
  result || {}
end

.destroy_of(table_name, column_name) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/schema_comments/schema_comment.rb', line 46

def destroy_of(table_name, column_name)
  yaml_access do |db|
    column_hash = db[COLUMN_KEY][table_name.to_s]
    column_hash.delete(column_name.to_s) if column_hash
  end
  clear_cache
end

.locale_yaml(locale) ⇒ Object



95
96
97
# File 'lib/schema_comments/schema_comment.rb', line 95

def locale_yaml(locale)
  YAML.dump({locale.to_s => activerecord_comments})
end

.model_commentsObject



72
73
74
75
# File 'lib/schema_comments/schema_comment.rb', line 72

def model_comments
  yaml_read{|db| db[TABLE_KEY] }.
    each_with_object({}){|(k,v),d| d[k.singularize] = v }
end

.save_column_comment(table_name, column_name, comment) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/schema_comments/schema_comment.rb', line 38

def save_column_comment(table_name, column_name, comment)
  yaml_access do |db|
    db[COLUMN_KEY][table_name.to_s] ||= {}
    db[COLUMN_KEY][table_name.to_s][column_name.to_s] = comment
  end
  clear_cache
end

.save_table_comment(table_name, comment) ⇒ Object



31
32
33
34
35
36
# File 'lib/schema_comments/schema_comment.rb', line 31

def save_table_comment(table_name, comment)
  yaml_access do |db|
    db[TABLE_KEY][table_name.to_s] = comment
  end
  clear_cache
end

.table_comment(table_name) ⇒ Object



13
14
15
16
# File 'lib/schema_comments/schema_comment.rb', line 13

def table_comment(table_name)
  @table_names ||= yaml_read{|db| db[TABLE_KEY]}.dup
  @table_names[table_name.to_s]
end

.update_column_name(table_name, column_name, new_name) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/schema_comments/schema_comment.rb', line 62

def update_column_name(table_name, column_name, new_name)
  yaml_access do |db|
    table_cols = db[COLUMN_KEY][table_name.to_s]
    if table_cols
      table_cols[new_name.to_s] = table_cols.delete(column_name.to_s)
    end
  end
  clear_cache
end

.update_table_name(table_name, new_name) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/schema_comments/schema_comment.rb', line 54

def update_table_name(table_name, new_name)
  yaml_access do |db|
    db[TABLE_KEY][new_name.to_s] = db[TABLE_KEY].delete(table_name.to_s)
    db[COLUMN_KEY][new_name.to_s] = db[COLUMN_KEY].delete(table_name.to_s)
  end
  clear_cache
end

.yaml_access(&block) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/schema_comments/schema_comment.rb', line 110

def yaml_access(&block)
  if @yaml_transaction
    yield(@yaml_transaction) if block_given?
  else
    db = SortedStore.new(SchemaComments.yaml_path)
    result = nil
    # t = Time.now.to_f
    @yaml_transaction = db
    begin
      db.transaction do
        db[TABLE_KEY] ||= {}
        db[COLUMN_KEY] ||= {}
        SortedStore.validate_yaml!(db)
        result = yield(db) if block_given?
      end
    ensure
      @yaml_transaction = nil
    end
    # puts("SchemaComment#yaml_access %fms from %s" % [Time.now.to_f - t, caller[0].gsub(/^.+:in /, '')])
    result
  end
end

.yaml_read(&block) ⇒ Object



105
106
107
108
# File 'lib/schema_comments/schema_comment.rb', line 105

def yaml_read(&block)
  db = YAML.load_file(SchemaComments.yaml_path)
  block_given? ? yield(db) : db
end