class SchemaComments::SchemaComment

Constants

COLUMN_KEY
TABLE_KEY

Public Class Methods

activerecord_comments() click to toggle source
# File lib/schema_comments/schema_comment.rb, line 85
def activerecord_comments
  {
    'activerecord' => {
      'models' => model_comments,
      'attributes' => attribute_comments,
    }
  }
end
attribute_comments() click to toggle source
# File lib/schema_comments/schema_comment.rb, line 76
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_cache() click to toggle source
# File lib/schema_comments/schema_comment.rb, line 98
def clear_cache
  @table_names = nil
  @column_names = nil
  self
end
column_comment(table_name, column_name) click to toggle source
# File lib/schema_comments/schema_comment.rb, line 17
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) click to toggle source
# File lib/schema_comments/schema_comment.rb, line 23
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) click to toggle source
# File lib/schema_comments/schema_comment.rb, line 45
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) click to toggle source
# File lib/schema_comments/schema_comment.rb, line 94
def locale_yaml(locale)
  YAML.dump({locale.to_s => activerecord_comments})
end
model_comments() click to toggle source
# File lib/schema_comments/schema_comment.rb, line 71
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) click to toggle source
# File lib/schema_comments/schema_comment.rb, line 37
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) click to toggle source
# File lib/schema_comments/schema_comment.rb, line 30
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) click to toggle source
# File lib/schema_comments/schema_comment.rb, line 12
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) click to toggle source
# File lib/schema_comments/schema_comment.rb, line 61
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) click to toggle source
# File lib/schema_comments/schema_comment.rb, line 53
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() { |yaml_transaction| ... } click to toggle source
# File lib/schema_comments/schema_comment.rb, line 109
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() { |db| ... } click to toggle source
# File lib/schema_comments/schema_comment.rb, line 104
def yaml_read(&block)
  db = YAML.load_file(SchemaComments.yaml_path)
  block_given? ? yield(db) : db
end