schema_comments 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21a9d779680d54f251b51ab643ec747878b68924
4
- data.tar.gz: 3ffa036c8dab55d4386d442e60bdf66aa6ff770e
3
+ metadata.gz: dfd122cb2fedd33909167773b879f943f97c74fb
4
+ data.tar.gz: 880e604fc10669a33c3408c936a052c1d54d86fa
5
5
  SHA512:
6
- metadata.gz: 6027c604d17578b22dfb68aa5e0b6a42c184629ae539c2cb79b9c1634ab76dbf4d55a82136d075d1a7be53756d68ee7e7ff2b2fbadc2e935d6108956806cb524
7
- data.tar.gz: e576b7f6de6a7cf31ea2a73413bce1b3929347ac110a2f81c3a84bceab744854d50cf16f73bab85139a8b9f130a74463c17150aced1f21641be640473fa5346c
6
+ metadata.gz: e03c5f4bd9b55bc4aa9e4395eb809da23999739ce10676ad67d0c9ad3d03c6c2dacf0244b6722572cd2128d1d6f572da85f7d47d21995651bed87530f4b1110b
7
+ data.tar.gz: 1210f6fa4a9eccfb61ebfcc7065d9e0265d0d6d67e4b4f7214d9030fe2102562e727d63360c2e4310a245fe53a3fe804b8ba20d2f249330526002cd175f3fb12
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /pkg
9
9
  /selectable_attr_test.sqlite3.db
10
10
  /Gemfile.lock
11
+ /tmp
data/.travis.yml CHANGED
@@ -1,8 +1,7 @@
1
1
  script: "bundle exec rake spec"
2
2
  rvm:
3
- # - 2.0.0 # it can work but spec fails
4
- - 2.1.6
5
- - 2.2.2
3
+ - 2.2.4
4
+ - 2.3.0
6
5
  gemfile:
7
6
  - gemfiles/Gemfile.rails-4.0
8
7
  - gemfiles/Gemfile.rails-4.1
data/Gemfile CHANGED
@@ -2,7 +2,7 @@ source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- group :development do
5
+ group :development, :test do
6
6
  gem "sqlite3"
7
7
  gem "mysql2"
8
8
  gem "rails"
data/README.md CHANGED
@@ -173,8 +173,8 @@ rake db:annotate で以下のようなコメントを、モデル、テスト、
173
173
  annotate_modelsは、達人プログラマーのDave Thomasさんが公開しているプラグインです。
174
174
  http://repo.pragprog.com/svn/Public/plugins/annotate_models/
175
175
 
176
- 本プラグインでは、それを更に拡張したDave Boltonさんのプラグイン(
177
- http://github.com/rotuka/annotate_models )をベースに拡張を加えています。
176
+ 本プラグインでは、それを更に拡張したDave Boltonさんのプラグインannotate_models
177
+ ( リポジトリは削除された様子・・・ )をベースに拡張を加えています。
178
178
 
179
179
  ## License
180
180
  Copyright (c) 2008 Takeshi AKIMA, released under the Ruby License
@@ -5,7 +5,7 @@ gem "activerecord" , "~> 4.0.0"
5
5
 
6
6
  group :test do
7
7
  gem "sqlite3", :platform => :ruby
8
- gem "mysql2" , :platform => :ruby
8
+ gem "mysql2" , "~> 0.3.13", :platform => :ruby
9
9
  gem "rails"
10
10
 
11
11
  # gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
@@ -3,9 +3,9 @@ source "http://rubygems.org"
3
3
  gem "activesupport", "~> 4.1.0"
4
4
  gem "activerecord" , "~> 4.1.0"
5
5
 
6
- group :test do
6
+ group :development, :test do
7
7
  gem "sqlite3", :platform => :ruby
8
- gem "mysql2" , :platform => :ruby
8
+ gem "mysql2" , "~> 0.3.13", :platform => :ruby
9
9
  gem "rails"
10
10
  end
11
11
 
@@ -1,11 +1,7 @@
1
1
  module SchemaComments
2
2
  module Base
3
- def self.included(mod)
4
- mod.extend ClassMethods
5
- mod.instance_eval do
6
- alias :columns_without_schema_comments :columns
7
- alias :columns :columns_with_schema_comments
8
- end
3
+ def self.prepended(mod)
4
+ mod.singleton_class.prepend ClassMethods
9
5
  mod.ignore_pattern_to_export_i18n = /\[.*\]/
10
6
  end
11
7
 
@@ -14,8 +10,8 @@ module SchemaComments
14
10
  @table_comment ||= connection.table_comment(table_name)
15
11
  end
16
12
 
17
- def columns_with_schema_comments
18
- result = columns_without_schema_comments
13
+ def columns
14
+ result = super
19
15
  unless @column_comments_loaded
20
16
  column_comment_hash = connection.column_comments(table_name)
21
17
  result.each do |column|
@@ -11,18 +11,13 @@ module SchemaComments
11
11
  end
12
12
 
13
13
  module TableDefinition
14
- def self.included(mod)
15
- mod.module_eval do
16
- alias_method_chain(:column, :schema_comments)
17
- end
18
- end
19
14
  attr_accessor :comment
20
15
 
21
- def column_with_schema_comments(name, type, options = {})
22
- column_without_schema_comments(name, type, options)
16
+ def column(name, type, options = {})
17
+ result = super(name, type, options)
23
18
  column = self[name]
24
19
  column.comment = options[:comment]
25
- self
20
+ result
26
21
  end
27
22
  end
28
23
 
@@ -81,22 +76,9 @@ module SchemaComments
81
76
  end
82
77
 
83
78
  module ConcreteAdapter
84
- def self.included(mod)
85
- mod.module_eval do
86
- alias_method_chain :columns , :schema_comments
87
- alias_method_chain :create_table , :schema_comments
88
- alias_method_chain :drop_table , :schema_comments
89
- alias_method_chain :rename_table , :schema_comments
90
- alias_method_chain :remove_column, :schema_comments
91
- alias_method_chain :add_column , :schema_comments
92
- alias_method_chain :change_column, :schema_comments
93
- alias_method_chain :rename_column, :schema_comments
94
- end
95
- end
96
-
97
79
  #TODO: columnsメソッドに第二引数移行がないので本来は消すべき?
98
- def columns_with_schema_comments(table_name, name = nil, &block)
99
- result = columns_without_schema_comments(table_name)
80
+ def columns(table_name, name = nil, &block)
81
+ result = super(table_name)
100
82
  column_comment_hash = column_comments(table_name)
101
83
  result.each do |column|
102
84
  column.comment = column_comment_hash[column.name]
@@ -104,9 +86,9 @@ module SchemaComments
104
86
  result
105
87
  end
106
88
 
107
- def create_table_with_schema_comments(table_name, options = {}, &block)
89
+ def create_table(table_name, options = {}, &block)
108
90
  table_def = nil
109
- result = create_table_without_schema_comments(table_name, options) do |t|
91
+ result = super(table_name, options) do |t|
110
92
  table_def = t
111
93
  yield(t)
112
94
  end
@@ -117,19 +99,19 @@ module SchemaComments
117
99
  result
118
100
  end
119
101
 
120
- def drop_table_with_schema_comments(table_name, *args, &block)
121
- result = drop_table_without_schema_comments(table_name, *args)
102
+ def drop_table(table_name, *args, &block)
103
+ result = super(table_name, *args)
122
104
  delete_schema_comments(table_name) unless @ignore_drop_table
123
105
  result
124
106
  end
125
107
 
126
- def rename_table_with_schema_comments(table_name, new_name)
127
- result = rename_table_without_schema_comments(table_name, new_name)
108
+ def rename_table(table_name, new_name)
109
+ result = super(table_name, new_name)
128
110
  update_schema_comments_table_name(table_name, new_name)
129
111
  result
130
112
  end
131
113
 
132
- def remove_column_with_schema_comments(table_name, *column_names)
114
+ def remove_column(table_name, *column_names)
133
115
  # sqlite3ではremove_columnがないので、以下のフローでスキーマ更新します。
134
116
  # 1. CREATE TEMPORARY TABLE "altered_xxxxxx" (・・・)
135
117
  # 2. PRAGMA index_list("xxxxxx")
@@ -140,7 +122,7 @@ module SchemaComments
140
122
  #
141
123
  # このdrop tableの際に、schema_commentsを変更しないようにフラグを立てています。
142
124
  @ignore_drop_table = true
143
- remove_column_without_schema_comments(table_name, *column_names)
125
+ super(table_name, *column_names)
144
126
  column_names.each do |column_name|
145
127
  delete_schema_comments(table_name, column_name)
146
128
  end
@@ -148,25 +130,25 @@ module SchemaComments
148
130
  @ignore_drop_table = false
149
131
  end
150
132
 
151
- def add_column_with_schema_comments(table_name, column_name, type, options = {})
133
+ def add_column(table_name, column_name, type, options = {})
152
134
  comment = options.delete(:comment)
153
- result = add_column_without_schema_comments(table_name, column_name, type, options)
135
+ result = super(table_name, column_name, type, options)
154
136
  column_comment(table_name, column_name, comment) if comment
155
137
  result
156
138
  end
157
139
 
158
- def change_column_with_schema_comments(table_name, column_name, type, options = {})
140
+ def change_column(table_name, column_name, type, options = {})
159
141
  comment = options.delete(:comment)
160
142
  @ignore_drop_table = true
161
- result = change_column_without_schema_comments(table_name, column_name, type, options)
143
+ result = super(table_name, column_name, type, options)
162
144
  column_comment(table_name, column_name, comment) if comment
163
145
  result
164
146
  ensure
165
147
  @ignore_drop_table = false
166
148
  end
167
149
 
168
- def rename_column_with_schema_comments(table_name, column_name, new_column_name)
169
- result = rename_column_without_schema_comments(table_name, column_name, new_column_name)
150
+ def rename_column(table_name, column_name, new_column_name)
151
+ result = super(table_name, column_name, new_column_name)
170
152
  comment = update_schema_comments_column_name(table_name, column_name, new_column_name)
171
153
  result
172
154
  end
@@ -1,17 +1,13 @@
1
1
  module SchemaComments
2
2
  module Migration
3
- def self.included(mod)
4
- mod.extend(ClassMethods)
5
- mod.instance_eval do
6
- alias :migrate_without_schema_comments :migrate
7
- alias :migrate :migrate_with_schema_comments
8
- end
3
+ def self.prepended(mod)
4
+ mod.singleton_class.prepend(ClassMethods)
9
5
  end
10
6
 
11
7
  module ClassMethods
12
- def migrate_with_schema_comments(*args, &block)
8
+ def migrate(*args, &block)
13
9
  SchemaComments::SchemaComment.yaml_access do
14
- migrate_without_schema_comments(*args, &block)
10
+ super(*args, &block)
15
11
  end
16
12
  end
17
13
  end
@@ -1,17 +1,13 @@
1
1
  module SchemaComments
2
2
  module Migrator
3
- def self.included(mod)
4
- mod.extend(ClassMethods)
5
- mod.instance_eval do
6
- alias :migrate_without_schema_comments :migrate
7
- alias :migrate :migrate_with_schema_comments
8
- end
3
+ def self.prepend(mod)
4
+ mod.singleton_class.prepend(ClassMethods)
9
5
  end
10
6
 
11
7
  module ClassMethods
12
- def migrate_with_schema_comments(*args, &block)
8
+ def migrate(*args, &block)
13
9
  SchemaComments::SchemaComment.yaml_access do
14
- migrate_without_schema_comments(*args, &block)
10
+ super(*args, &block)
15
11
  end
16
12
  end
17
13
  end
@@ -1,17 +1,13 @@
1
1
  module SchemaComments
2
2
  module Schema
3
- def self.included(mod)
4
- mod.extend(ClassMethods)
5
- mod.instance_eval do
6
- alias :define_without_schema_comments :define
7
- alias :define :define_with_schema_comments
8
- end
3
+ def self.prepended(mod)
4
+ mod.singleton_class.prepend(ClassMethods)
9
5
  end
10
6
 
11
7
  module ClassMethods
12
- def define_with_schema_comments(*args, &block)
8
+ def define(*args, &block)
13
9
  SchemaComments::SchemaComment.yaml_access do
14
- define_without_schema_comments(*args, &block)
10
+ super(*args, &block)
15
11
  end
16
12
  end
17
13
  end
@@ -82,15 +82,16 @@ module SchemaComments
82
82
  db = SortedStore.new(SchemaComments.yaml_path)
83
83
  result = nil
84
84
  # t = Time.now.to_f
85
- db.transaction do
86
- @yaml_transaction = db
87
- begin
85
+ @yaml_transaction = db
86
+ begin
87
+ db.transaction do
88
88
  db[TABLE_KEY] ||= {}
89
89
  db[COLUMN_KEY] ||= {}
90
+ SortedStore.validate_yaml!(db)
90
91
  result = yield(db) if block_given?
91
- ensure
92
- @yaml_transaction = nil
93
92
  end
93
+ ensure
94
+ @yaml_transaction = nil
94
95
  end
95
96
  # puts("SchemaComment#yaml_access %fms from %s" % [Time.now.to_f - t, caller[0].gsub(/^.+:in /, '')])
96
97
  result
@@ -110,7 +111,20 @@ module SchemaComments
110
111
  root.to_yaml(@opt)
111
112
  end
112
113
 
114
+ def self.validate_yaml!(root)
115
+ table_comments = (root['table_comments'] ||= {})
116
+ column_comments = (root['column_comments'] ||= {})
117
+ # raise YamlError, "Broken schame_comments.yml by invalid root: #{root.inspect}" unless root.is_a?(Hash)
118
+ raise YamlError, "Broken schame_comments.yml by invalid table_comments" unless table_comments.is_a?(Hash)
119
+ raise YamlError, "Broken schame_comments.yml by invalid_column_comments" unless column_comments.is_a?(Hash)
120
+ column_comments.each do |table_name, value|
121
+ next if value.nil?
122
+ raise YamlError, "Broken schame_comments.yml by invalid column_comments for #{table_name}" unless value.is_a?(Hash)
123
+ end
124
+ end
125
+
113
126
  def self.sort_yaml_content!(root)
127
+ self.validate_yaml!(root)
114
128
  table_comments = (root['table_comments'] ||= {})
115
129
  column_comments = (root['column_comments'] ||= {})
116
130
  # 大元は
@@ -121,16 +135,13 @@ module SchemaComments
121
135
  # その他
122
136
  # ...
123
137
  # の順番です。
124
- raise YamlError, "Broken schame_comments.yml" unless root.is_a?(Hash)
125
138
  root.extend(HashKeyOrderable)
126
139
  root.key_order = %w(table_comments column_comments)
127
140
  # table_comments はテーブル名のアルファベット順
128
141
  table_names = ActiveRecord::Base.connection.tables.sort - ['schema_migrations']
129
- raise YamlError, "Broken schame_comments.yml" unless table_comments.is_a?(Hash)
130
142
  table_comments.extend(HashKeyOrderable)
131
143
  table_comments.key_order = table_names
132
144
  # column_comments もテーブル名のアルファベット順
133
- raise YamlError, "Broken schame_comments.yml" unless column_comments.is_a?(Hash)
134
145
  column_comments.extend(HashKeyOrderable)
135
146
  column_comments.key_order = table_names
136
147
  # column_comments の各値はテーブルのカラム順
@@ -1,3 +1,3 @@
1
1
  module SchemaComments
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -28,13 +28,13 @@ module SchemaComments
28
28
  ar_class = "ActiveRecord::#{base_name}".constantize
29
29
  sc_class = "SchemaComments::#{base_name}".constantize
30
30
  unless ar_class.ancestors.include?(sc_class)
31
- ar_class.__send__(:include, sc_class)
31
+ ar_class.__send__(:prepend, sc_class)
32
32
  end
33
33
  end
34
34
 
35
35
  unless ActiveRecord::ConnectionAdapters::AbstractAdapter.ancestors.include?(SchemaComments::ConnectionAdapters::Adapter)
36
36
  ActiveRecord::ConnectionAdapters::AbstractAdapter.module_eval do
37
- include SchemaComments::ConnectionAdapters::Adapter
37
+ prepend SchemaComments::ConnectionAdapters::Adapter
38
38
  end
39
39
  end
40
40
 
@@ -44,7 +44,7 @@ module SchemaComments
44
44
  require("active_record/connection_adapters/#{adapter.downcase}_adapter")
45
45
  adapter_class = ('ActiveRecord::ConnectionAdapters::' << "#{adapter}Adapter").constantize
46
46
  adapter_class.module_eval do
47
- include SchemaComments::ConnectionAdapters::ConcreteAdapter
47
+ prepend SchemaComments::ConnectionAdapters::ConcreteAdapter
48
48
  end
49
49
  rescue Exception => e
50
50
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_comments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - akm
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-28 00:00:00.000000000 Z
11
+ date: 2016-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  version: '0'
142
142
  requirements: []
143
143
  rubyforge_project:
144
- rubygems_version: 2.4.6
144
+ rubygems_version: 2.5.1
145
145
  signing_key:
146
146
  specification_version: 4
147
147
  summary: schema_comments generates extra methods dynamically for attribute which has