sequel-annotate 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cbe18cf12eced99b6453d25c4e7194e807ac4e9aad1e21d0371c72452dd6d2f4
4
- data.tar.gz: 442da3e19cf3a387f0a73aede9f6c16219d7984338b777a85a2d4d04053a1f28
3
+ metadata.gz: 42122bfc730bb5345c90160c5a17b0861c3a5a8308dfc6a008c1fccc0b73fe3e
4
+ data.tar.gz: 7bbb33c84261d97a66c17b942c19fc0b432d4029b8e384f148ec79ddf96c196e
5
5
  SHA512:
6
- metadata.gz: 2c01cfbc83195c28c29c458517ef9ea54ee9b40e5c3ac9faa70bb97db1c458a65e82496772d43faa89289da9f0f22ede013169a7a37da24721c1de4b82b046db
7
- data.tar.gz: 566f31d7ebdc2e86dce167327c0ec4ead26d5755ff5621334421f6fef54eba16d6b84c1e7e6e5b44fe42f9bb29c78b2482cbba0eda38c730bc28574e08e731cd
6
+ metadata.gz: 4ee1e34b6317be4e57f113a33e47c83190157ee3cbbae9cfd209c8806895aacc5e44604bbf2f0cfcbee1114a8822ed0c36a9ccdad628cb66daab58873d9020b8
7
+ data.tar.gz: d27e5074fb1c7642993647185605d0d714482d1f9ce5bc9e7b58817872f69674d0e803fa68210d0d73b2c176a479a44224a3678c602b1681116acae8dbf16d8c
data/CHANGELOG CHANGED
@@ -1,3 +1,13 @@
1
+ === 1.6.0 (2020-10-05)
2
+
3
+ * Add support for :namespace=>true option to attempt to automatically determine namespace (adam12) (#20)
4
+
5
+ * Skip models that select from a subquery (adam12, jeremyevans) (#17)
6
+
7
+ * Allow files to be skipped with sequel-annotate: false magic comment (adam12) (#18)
8
+
9
+ * Ignore opening of singleton classes with class << when looking for models (adam12) (#16)
10
+
1
11
  === 1.5.0 (2020-05-04)
2
12
 
3
13
  * Add support for magic comments in model files when using the :position=>:before option (qortex, jeremyevans) (#15)
@@ -93,6 +93,11 @@ The columns section can have a border on the top and bottom for easier visual di
93
93
 
94
94
  Sequel::Annotate.annotate(Dir['models/*.rb'], border: true)
95
95
 
96
+ Models that use datasets that select from subqueries are automatically skipped,
97
+ as annotations don't really make sense for them since they are not backed by a
98
+ table. You can skip other models by using a <tt># sequel-annotate: false</tt>
99
+ magic comment somewhere in the file.
100
+
96
101
  === Rake Task
97
102
 
98
103
  Here's an example rake task for sequel-annotate:
data/Rakefile CHANGED
@@ -28,19 +28,11 @@ begin
28
28
  rescue Gem::LoadError
29
29
  end
30
30
 
31
- rdoc_task_class = begin
32
- require "rdoc/task"
33
- RDoc::Task
34
- rescue LoadError
35
- require "rake/rdoctask"
36
- Rake::RDocTask
37
- end
38
-
39
31
  RDOC_OPTS = RDOC_DEFAULT_OPTS + ['--main', 'README.rdoc']
40
32
 
41
- rdoc_task_class.new do |rdoc|
33
+ require "rdoc/task"
34
+ RDoc::Task.new do |rdoc|
42
35
  rdoc.rdoc_dir = "rdoc"
43
36
  rdoc.options += RDOC_OPTS
44
37
  rdoc.rdoc_files.add %w"README.rdoc CHANGELOG MIT-LICENSE lib/**/*.rb"
45
38
  end
46
-
@@ -13,11 +13,22 @@ module Sequel
13
13
  namespace = options[:namespace]
14
14
 
15
15
  paths.each do |path|
16
- if match = File.read(path).match(/class (\S+)\s*</)
16
+ text = File.read(path)
17
+
18
+ next if text.match(/^#\s+sequel-annotate:\s+false$/i)
19
+
20
+ name = nil
21
+ if namespace == true
22
+ constants = text.scan(/\bmodule ([^\s]+)|class ([^\s<]+)\s*</).flatten.compact
23
+ name = constants.join("::") if constants.any?
24
+ elsif match = text.match(/class ([^\s<]+)\s*</)
17
25
  name = match[1]
18
26
  if namespace
19
27
  name = "#{namespace}::#{name}"
20
28
  end
29
+ end
30
+
31
+ if name
21
32
  klass = name.constantize
22
33
  if klass.ancestors.include?(Sequel::Model)
23
34
  new(klass).annotate(path, options)
@@ -37,6 +48,8 @@ module Sequel
37
48
  # Append the schema comment (or replace it if one already exists) to
38
49
  # the file at the given path.
39
50
  def annotate(path, options = {})
51
+ return if skip_model?
52
+
40
53
  orig = current = File.read(path).rstrip
41
54
 
42
55
  if options[:position] == :before
@@ -84,6 +97,8 @@ module Sequel
84
97
  # this table if set to +false+.
85
98
  # :triggers :: Do not include triggers in annotation if set to +false+.
86
99
  def schema_comment(options = {})
100
+ return "" if skip_model?
101
+
87
102
  output = []
88
103
  output << "# Table: #{model.dataset.with_quote_identifiers(false).literal(model.table_name)}"
89
104
 
@@ -120,12 +135,12 @@ module Sequel
120
135
 
121
136
  cols.times do |i|
122
137
  rows.each do |r|
123
- lengths[i] = r[i].length if r[i].length > lengths[i]
138
+ lengths[i] = r[i].length if r[i] && r[i].length > lengths[i]
124
139
  end
125
140
  end
126
141
 
127
142
  rows.map do |r|
128
- "# #{r.zip(lengths).map{|c, l| c.ljust(l).gsub("\n", "\n# ")}.join(' | ')}".strip
143
+ "# #{r.zip(lengths).map{|c, l| c.to_s.ljust(l).gsub("\n", "\n# ")}.join(' | ')}".strip
129
144
  end
130
145
  end
131
146
 
@@ -277,5 +292,13 @@ SQL
277
292
  output.concat(align(rows).sort)
278
293
  end
279
294
  end
295
+
296
+ # Whether we should skip annotations for the model.
297
+ # True if the model selects from a dataset.
298
+ def skip_model?
299
+ model.dataset.joined_dataset? || model.dataset.first_source_table.is_a?(Dataset)
300
+ rescue Sequel::Error
301
+ true
302
+ end
280
303
  end
281
304
  end
@@ -0,0 +1,7 @@
1
+ dataset =
2
+ SDB[:items]
3
+ .left_join(:categories, { id: :category_id })
4
+ .select { Sequel[:items][:name] }
5
+
6
+ class SComplexDataset < Sequel::Model(dataset)
7
+ end
@@ -0,0 +1,7 @@
1
+ dataset =
2
+ SDB[:items]
3
+ .left_join(:categories, { id: :category_id })
4
+ .select { Sequel[:items][:name] }
5
+
6
+ class SComplexDataset < Sequel::Model(dataset)
7
+ end
@@ -101,10 +101,12 @@ class ::SItemWithCoding < Sequel::Model(SDB[:items]); end
101
101
  class ::SItemWithEncoding < Sequel::Model(SDB[:items]); end
102
102
  class ::SItemWithWarnIndent < Sequel::Model(SDB[:items]); end
103
103
  class ::SItemWithWarnPastScope < Sequel::Model(SDB[:items]); end
104
+ class ::SItemWithMagicComment < Sequel::Model(SDB[:items]); end
104
105
  class ::SCategory < Sequel::Model(SDB[:categories]); end
105
106
  class ::SManufacturer < Sequel::Model(SDB[:manufacturers]); end
106
107
  class ::NewlineTest < Sequel::Model; end
107
108
  class ::QualifiedTableNameTest < Sequel::Model(Sequel.qualify(:public, :categories)); end
109
+ class SComplexDataset < Sequel::Model(SDB[:items].left_join(:categories, :id => :category_id).select{items[:name]}); end
108
110
 
109
111
  # Abstract Base Class
110
112
  ABC = Class.new(Sequel::Model)
@@ -133,6 +135,12 @@ describe Sequel::Annotate do
133
135
  Dir['spec/tmp/*.rb'].each{|f| File.delete(f)}
134
136
  end
135
137
 
138
+ it "skips files with the sequel-annotate magic comment set to false" do
139
+ FileUtils.cp('spec/unannotated/sitemwithmagiccomment.rb', 'spec/tmp/')
140
+ Sequel::Annotate.annotate(['spec/tmp/sitemwithmagiccomment.rb'])
141
+ File.read('spec/tmp/sitemwithmagiccomment.rb').must_equal File.read('spec/unannotated/sitemwithmagiccomment.rb')
142
+ end
143
+
136
144
  it "#schema_info should not return sections we set to false" do
137
145
  Sequel::Annotate.new(Item).schema_comment(:indexes => false, :constraints => false, :foreign_keys => false, :triggers => false).must_equal(fix_pg_comment((<<OUTPUT).chomp))
138
146
  # Table: items
@@ -197,6 +205,7 @@ OUTPUT
197
205
  # valid_price | BEFORE INSERT OR UPDATE ON items FOR EACH ROW EXECUTE PROCEDURE valid_price()
198
206
  OUTPUT
199
207
 
208
+ Sequel::Annotate.new(SComplexDataset).schema_comment.must_equal("")
200
209
  Sequel::Annotate.new(Category).schema_comment.must_equal(fix_pg_comment((<<OUTPUT).chomp))
201
210
  # Table: categories
202
211
  # Columns:
@@ -304,7 +313,7 @@ OUTPUT
304
313
  it "#annotate #{desc} should annotate the file comment" do
305
314
  FileUtils.cp(Dir['spec/unannotated/*.rb'], 'spec/tmp')
306
315
 
307
- [Item, Category, Manufacturer, SItem, SCategory, SManufacturer, SItemWithFrozenLiteral, SItemWithCoding, SItemWithEncoding, SItemWithWarnIndent, SItemWithWarnPastScope].each do |model|
316
+ [Item, Category, Manufacturer, SItem, SCategory, SManufacturer, SItemWithFrozenLiteral, SItemWithCoding, SItemWithEncoding, SItemWithWarnIndent, SItemWithWarnPastScope, SComplexDataset].each do |model|
308
317
  filename = model.name.downcase
309
318
  2.times do
310
319
  Sequel::Annotate.new(model).annotate("spec/tmp/#{filename}.rb", *args)
@@ -320,7 +329,7 @@ OUTPUT
320
329
 
321
330
  2.times do
322
331
  Sequel::Annotate.annotate(Dir["spec/tmp/*.rb"], *args)
323
- [Item, Category, Manufacturer, SItem, SCategory, SManufacturer, SItemWithFrozenLiteral, SItemWithCoding, SItemWithEncoding, SItemWithWarnIndent, SItemWithWarnPastScope].each do |model|
332
+ [Item, Category, Manufacturer, SItem, SCategory, SManufacturer, SItemWithFrozenLiteral, SItemWithCoding, SItemWithEncoding, SItemWithWarnIndent, SItemWithWarnPastScope, SComplexDataset].each do |model|
324
333
  filename = model.name.downcase
325
334
  expected = File.read("spec/annotated_#{pos}/#{filename}.rb")
326
335
  expected = fix_pg_comment(expected) if model.db == DB
@@ -338,4 +347,10 @@ OUTPUT
338
347
  Sequel::Annotate.annotate(["spec/tmp/itm_unannotated.rb"], :namespace=>'ModelNamespace')
339
348
  File.read("spec/tmp/itm_unannotated.rb").must_equal fix_pg_comment(File.read('spec/namespaced/itm_annotated.rb'))
340
349
  end
350
+
351
+ it ".annotate #{desc} should handle :namespace => true option" do
352
+ FileUtils.cp('spec/namespaced/itm_unannotated.rb', 'spec/tmp')
353
+ Sequel::Annotate.annotate(["spec/tmp/itm_unannotated.rb"], :namespace=>true)
354
+ File.read("spec/tmp/itm_unannotated.rb").must_equal fix_pg_comment(File.read('spec/namespaced/itm_annotated.rb'))
355
+ end
341
356
  end
@@ -0,0 +1,4 @@
1
+ class NotModel
2
+ class << self
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ dataset =
2
+ SDB[:items]
3
+ .left_join(:categories, { id: :category_id })
4
+ .select { Sequel[:items][:name] }
5
+
6
+ class SComplexDataset < Sequel::Model(dataset)
7
+ end
@@ -0,0 +1,4 @@
1
+ # sequel-annotate: false
2
+
3
+ class SItemWithMagicComment < Sequel::Model(SDB[:items])
4
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-annotate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-04 00:00:00.000000000 Z
11
+ date: 2020-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -105,6 +105,7 @@ files:
105
105
  - spec/annotated_after/item.rb
106
106
  - spec/annotated_after/manufacturer.rb
107
107
  - spec/annotated_after/scategory.rb
108
+ - spec/annotated_after/scomplexdataset.rb
108
109
  - spec/annotated_after/sitem.rb
109
110
  - spec/annotated_after/sitemwithcoding.rb
110
111
  - spec/annotated_after/sitemwithencoding.rb
@@ -116,6 +117,7 @@ files:
116
117
  - spec/annotated_before/item.rb
117
118
  - spec/annotated_before/manufacturer.rb
118
119
  - spec/annotated_before/scategory.rb
120
+ - spec/annotated_before/scomplexdataset.rb
119
121
  - spec/annotated_before/sitem.rb
120
122
  - spec/annotated_before/sitemwithcoding.rb
121
123
  - spec/annotated_before/sitemwithencoding.rb
@@ -129,11 +131,14 @@ files:
129
131
  - spec/unannotated/category.rb
130
132
  - spec/unannotated/item.rb
131
133
  - spec/unannotated/manufacturer.rb
134
+ - spec/unannotated/not_model.rb
132
135
  - spec/unannotated/scategory.rb
136
+ - spec/unannotated/scomplexdataset.rb
133
137
  - spec/unannotated/sitem.rb
134
138
  - spec/unannotated/sitemwithcoding.rb
135
139
  - spec/unannotated/sitemwithencoding.rb
136
140
  - spec/unannotated/sitemwithfrozenliteral.rb
141
+ - spec/unannotated/sitemwithmagiccomment.rb
137
142
  - spec/unannotated/sitemwithwarnindent.rb
138
143
  - spec/unannotated/sitemwithwarnpastscope.rb
139
144
  - spec/unannotated/smanufacturer.rb
@@ -166,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
171
  - !ruby/object:Gem::Version
167
172
  version: '0'
168
173
  requirements: []
169
- rubygems_version: 3.1.2
174
+ rubygems_version: 3.1.4
170
175
  signing_key:
171
176
  specification_version: 4
172
177
  summary: Annotate Sequel models with schema information