sequel-annotate 1.0.0 → 1.1.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 +4 -4
- data/CHANGELOG +5 -1
- data/MIT-LICENSE +1 -1
- data/README.rdoc +21 -1
- data/lib/sequel/annotate.rb +17 -13
- data/spec/{annotated → annotated_after}/category.rb +0 -0
- data/spec/{annotated → annotated_after}/item.rb +0 -0
- data/spec/{annotated → annotated_after}/manufacturer.rb +0 -0
- data/spec/{annotated → annotated_after}/scategory.rb +2 -0
- data/spec/{annotated → annotated_after}/sitem.rb +0 -0
- data/spec/{annotated → annotated_after}/smanufacturer.rb +0 -0
- data/spec/annotated_before/category.rb +12 -0
- data/spec/annotated_before/item.rb +23 -0
- data/spec/annotated_before/manufacturer.rb +12 -0
- data/spec/annotated_before/scategory.rb +9 -0
- data/spec/annotated_before/sitem.rb +18 -0
- data/spec/annotated_before/smanufacturer.rb +8 -0
- data/spec/sequel-annotate_spec.rb +22 -18
- metadata +15 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f316737efff6528dad01a3dd9a142299d89430a
|
4
|
+
data.tar.gz: 1cd80a2169fd571993dceac826153c71fcf9e70f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a5d46059231408e13f9fe1752a164a038dfead63b28601184552410982eb04532a8f4083355ac42cf51adab9dfdb4ca95d3c3bed28b0fbf2b45f69a0e9a3dec
|
7
|
+
data.tar.gz: 36855434041c486606a055cd42029dec03a8613a62aaeefd4ce8431fce89cd28f8e07398b2d395d5b2ecec0d9a8edad85abae1556f76c0f6e572077285ff8b37
|
data/CHANGELOG
CHANGED
data/MIT-LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -10,7 +10,7 @@ tables that reference the current table.
|
|
10
10
|
|
11
11
|
== Example
|
12
12
|
|
13
|
-
The schema comments are kept at the end of the file, using a format similar to:
|
13
|
+
The schema comments are kept at the end of the file by default, using a format similar to:
|
14
14
|
|
15
15
|
class Item < Sequel::Model
|
16
16
|
end
|
@@ -62,6 +62,26 @@ a file:
|
|
62
62
|
|
63
63
|
sa.schema_comment
|
64
64
|
|
65
|
+
If you want to put the schema comment at the beginning of the file you can use
|
66
|
+
the :position option:
|
67
|
+
|
68
|
+
Sequel::Annotate.annotate(Dir['models/*.rb'], position: :before)
|
69
|
+
|
70
|
+
or
|
71
|
+
|
72
|
+
sa = Sequel::Annotate.new(Item)
|
73
|
+
sa.annotate('models/item.rb', position: :before)
|
74
|
+
|
75
|
+
=== Rake Task
|
76
|
+
|
77
|
+
Here's an example rake task for sequel-annotate:
|
78
|
+
|
79
|
+
desc "Update model annotations"
|
80
|
+
task :annotate do
|
81
|
+
require 'sequel/annotate'
|
82
|
+
Sequel::Annotate.annotate(Dir['models/*.rb'])
|
83
|
+
end
|
84
|
+
|
65
85
|
== License
|
66
86
|
|
67
87
|
MIT
|
data/lib/sequel/annotate.rb
CHANGED
@@ -8,11 +8,11 @@ module Sequel
|
|
8
8
|
# an instance manually and pass in the model and path. Example:
|
9
9
|
#
|
10
10
|
# Sequel::Annotate.annotate(Dir['models/*.rb'])
|
11
|
-
def self.annotate(paths)
|
11
|
+
def self.annotate(paths, options = {})
|
12
12
|
Sequel.extension :inflector
|
13
13
|
paths.each do |path|
|
14
14
|
if match = File.read(path).match(/class (\S+)\s*<\s*Sequel::Model/)
|
15
|
-
new(match[1].constantize).annotate(path)
|
15
|
+
new(match[1].constantize).annotate(path, options)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -22,26 +22,30 @@ module Sequel
|
|
22
22
|
|
23
23
|
# Store the model to annotate
|
24
24
|
def initialize(model)
|
25
|
-
@model = model
|
25
|
+
@model = model
|
26
26
|
end
|
27
27
|
|
28
28
|
# Append the schema comment (or replace it if one already exists) to
|
29
29
|
# the file at the given path.
|
30
|
-
def annotate(path)
|
30
|
+
def annotate(path, options = {})
|
31
31
|
orig = current = File.read(path).rstrip
|
32
32
|
|
33
|
-
if
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
33
|
+
if options[:position] == :before
|
34
|
+
current = current.gsub(/\A#\sTable[^\n\r]+\r?\n(?:#[^\n\r]*\r?\n)*/m, '').lstrip
|
35
|
+
current = "#{schema_comment}#{$/}#{$/}#{current}"
|
36
|
+
else
|
37
|
+
if m = current.reverse.match(/#{"#{$/}# Table: ".reverse}/m)
|
38
|
+
offset = current.length - m.end(0) + 1
|
39
|
+
unless current[offset..-1].match(/^[^#]/)
|
40
|
+
# If Table: comment exists, and there are no
|
41
|
+
# uncommented lines between it and the end of the file
|
42
|
+
# then replace current comment instead of appending it
|
43
|
+
current = current[0...offset].rstrip
|
44
|
+
end
|
40
45
|
end
|
46
|
+
current += "#{$/}#{$/}#{schema_comment}"
|
41
47
|
end
|
42
48
|
|
43
|
-
current += "#{$/}#{$/}#{schema_comment}"
|
44
|
-
|
45
49
|
if orig != current
|
46
50
|
File.open(path, "wb") do |f|
|
47
51
|
f.puts current
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# Table: categories
|
2
|
+
# Columns:
|
3
|
+
# id | integer | PRIMARY KEY DEFAULT nextval('categories_id_seq'::regclass)
|
4
|
+
# name | text | NOT NULL
|
5
|
+
# Indexes:
|
6
|
+
# categories_pkey | PRIMARY KEY btree (id)
|
7
|
+
# categories_name_key | UNIQUE btree (name)
|
8
|
+
# Referenced By:
|
9
|
+
# items | items_category_id_fkey | (category_id) REFERENCES categories(id)
|
10
|
+
|
11
|
+
class Category < Sequel::Model
|
12
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# Table: items
|
2
|
+
# Columns:
|
3
|
+
# id | integer | PRIMARY KEY DEFAULT nextval('items_id_seq'::regclass)
|
4
|
+
# category_id | integer | NOT NULL
|
5
|
+
# manufacturer_name | character varying(50) |
|
6
|
+
# manufacturer_location | text |
|
7
|
+
# in_stock | boolean | DEFAULT false
|
8
|
+
# name | text | DEFAULT 'John'::text
|
9
|
+
# price | double precision | DEFAULT 0
|
10
|
+
# Indexes:
|
11
|
+
# items_pkey | PRIMARY KEY btree (id)
|
12
|
+
# name | UNIQUE btree (manufacturer_name, manufacturer_location)
|
13
|
+
# manufacturer_name | btree (manufacturer_name)
|
14
|
+
# Check constraints:
|
15
|
+
# pos_id | (id > 0)
|
16
|
+
# Foreign key constraints:
|
17
|
+
# items_category_id_fkey | (category_id) REFERENCES categories(id)
|
18
|
+
# items_manufacturer_name_fkey | (manufacturer_name, manufacturer_location) REFERENCES manufacturers(name, location)
|
19
|
+
# Triggers:
|
20
|
+
# valid_price | BEFORE INSERT OR UPDATE ON items FOR EACH ROW EXECUTE PROCEDURE valid_price()
|
21
|
+
|
22
|
+
class Item < Sequel::Model
|
23
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# Table: manufacturers
|
2
|
+
# Primary Key: (name, location)
|
3
|
+
# Columns:
|
4
|
+
# name | text |
|
5
|
+
# location | text |
|
6
|
+
# Indexes:
|
7
|
+
# manufacturers_pkey | PRIMARY KEY btree (name, location)
|
8
|
+
# Referenced By:
|
9
|
+
# items | items_manufacturer_name_fkey | (manufacturer_name, manufacturer_location) REFERENCES manufacturers(name, location)
|
10
|
+
|
11
|
+
class Manufacturer < Sequel::Model
|
12
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Table: items
|
2
|
+
# Columns:
|
3
|
+
# id | integer | PRIMARY KEY AUTOINCREMENT
|
4
|
+
# category_id | integer | NOT NULL
|
5
|
+
# manufacturer_name | varchar(50) |
|
6
|
+
# manufacturer_location | varchar(255) |
|
7
|
+
# in_stock | boolean | DEFAULT 0
|
8
|
+
# name | varchar(255) | DEFAULT 'John'
|
9
|
+
# price | double precision | DEFAULT 0
|
10
|
+
# Indexes:
|
11
|
+
# manufacturer_name | (manufacturer_name)
|
12
|
+
# name | UNIQUE (manufacturer_name, manufacturer_location)
|
13
|
+
# Foreign key constraints:
|
14
|
+
# (category_id) REFERENCES categories
|
15
|
+
# (manufacturer_name, manufacturer_location) REFERENCES manufacturers
|
16
|
+
|
17
|
+
class SItem < Sequel::Model(SDB[:items])
|
18
|
+
end
|
@@ -13,7 +13,7 @@ SDB = Sequel.sqlite
|
|
13
13
|
[DB, SDB].each do |db|
|
14
14
|
db.create_table :categories do
|
15
15
|
primary_key :id
|
16
|
-
String :name, :unique=>true, :null=>false
|
16
|
+
String :name, :index=>{:unique=>true, :name=>'categories_name_key'}, :null=>false
|
17
17
|
end
|
18
18
|
|
19
19
|
db.create_table :manufacturers do
|
@@ -67,7 +67,7 @@ class ::SManufacturer < Sequel::Model(SDB[:manufacturers]); end
|
|
67
67
|
|
68
68
|
describe Sequel::Annotate do
|
69
69
|
before do
|
70
|
-
|
70
|
+
Dir.mkdir('spec/tmp') unless File.directory?('spec/tmp')
|
71
71
|
end
|
72
72
|
after do
|
73
73
|
Dir['spec/tmp/*.rb'].each{|f| File.delete(f)}
|
@@ -144,6 +144,8 @@ OUTPUT
|
|
144
144
|
# Columns:
|
145
145
|
# id | integer | PRIMARY KEY AUTOINCREMENT
|
146
146
|
# name | varchar(255) | NOT NULL
|
147
|
+
# Indexes:
|
148
|
+
# categories_name_key | UNIQUE (name)
|
147
149
|
OUTPUT
|
148
150
|
|
149
151
|
Sequel::Annotate.new(SManufacturer).schema_comment.must_equal((<<OUTPUT).chomp)
|
@@ -154,27 +156,29 @@ OUTPUT
|
|
154
156
|
# location | varchar(255) |
|
155
157
|
OUTPUT
|
156
158
|
end
|
159
|
+
|
160
|
+
[['without options', 'after', []], ['with :position=>:before option', 'before', [{:position=>:before}]]].each do |desc, pos, args|
|
161
|
+
it "#annotate #{desc} should annotate the file comment" do
|
162
|
+
FileUtils.cp(Dir['spec/unannotated/*.rb'], 'spec/tmp')
|
157
163
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
Sequel::Annotate.new(model).annotate("spec/tmp/#{filename}.rb")
|
165
|
-
File.read("spec/tmp/#{filename}.rb").must_equal File.read("spec/annotated/#{filename}.rb")
|
164
|
+
[Item, Category, Manufacturer, SItem, SCategory, SManufacturer].each do |model|
|
165
|
+
filename = model.name.downcase
|
166
|
+
2.times do
|
167
|
+
Sequel::Annotate.new(model).annotate("spec/tmp/#{filename}.rb", *args)
|
168
|
+
File.read("spec/tmp/#{filename}.rb").must_equal File.read("spec/annotated_#{pos}/#{filename}.rb")
|
169
|
+
end
|
166
170
|
end
|
167
171
|
end
|
168
|
-
end
|
169
172
|
|
170
|
-
|
171
|
-
|
173
|
+
it ".annotate #{desc} should annotate all files given" do
|
174
|
+
FileUtils.cp(Dir['spec/unannotated/*.rb'], 'spec/tmp')
|
172
175
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
176
|
+
2.times do
|
177
|
+
Sequel::Annotate.annotate(Dir["spec/tmp/*.rb"], *args)
|
178
|
+
[Item, Category, Manufacturer, SItem, SCategory, SManufacturer].each do |model|
|
179
|
+
filename = model.name.downcase
|
180
|
+
File.read("spec/tmp/#{filename}.rb").must_equal File.read("spec/annotated_#{pos}/#{filename}.rb")
|
181
|
+
end
|
178
182
|
end
|
179
183
|
end
|
180
184
|
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.
|
4
|
+
version: 1.1.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:
|
11
|
+
date: 2017-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -87,12 +87,18 @@ files:
|
|
87
87
|
- README.rdoc
|
88
88
|
- Rakefile
|
89
89
|
- lib/sequel/annotate.rb
|
90
|
-
- spec/
|
91
|
-
- spec/
|
92
|
-
- spec/
|
93
|
-
- spec/
|
94
|
-
- spec/
|
95
|
-
- spec/
|
90
|
+
- spec/annotated_after/category.rb
|
91
|
+
- spec/annotated_after/item.rb
|
92
|
+
- spec/annotated_after/manufacturer.rb
|
93
|
+
- spec/annotated_after/scategory.rb
|
94
|
+
- spec/annotated_after/sitem.rb
|
95
|
+
- spec/annotated_after/smanufacturer.rb
|
96
|
+
- spec/annotated_before/category.rb
|
97
|
+
- spec/annotated_before/item.rb
|
98
|
+
- spec/annotated_before/manufacturer.rb
|
99
|
+
- spec/annotated_before/scategory.rb
|
100
|
+
- spec/annotated_before/sitem.rb
|
101
|
+
- spec/annotated_before/smanufacturer.rb
|
96
102
|
- spec/sequel-annotate_spec.rb
|
97
103
|
- spec/unannotated/category.rb
|
98
104
|
- spec/unannotated/item.rb
|
@@ -127,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
127
133
|
version: '0'
|
128
134
|
requirements: []
|
129
135
|
rubyforge_project:
|
130
|
-
rubygems_version: 2.
|
136
|
+
rubygems_version: 2.6.13
|
131
137
|
signing_key:
|
132
138
|
specification_version: 4
|
133
139
|
summary: Annotate Sequel models with schema information
|