sequel-annotate 1.0.0 → 1.1.0

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: eb5fcadf130d2d23412b4597e7e0b1fa4c2ef7c0
4
- data.tar.gz: efa509bb8630ad280e036c47105d4f3fd84a8a54
3
+ metadata.gz: 2f316737efff6528dad01a3dd9a142299d89430a
4
+ data.tar.gz: 1cd80a2169fd571993dceac826153c71fcf9e70f
5
5
  SHA512:
6
- metadata.gz: 9ea01f3487452deeb82a3b34b5c2469c47d7e10e843dc66d60b68e4a762e841d21cbe73611ff3f400006dc5acbbc57db1a47598082200f5a132b1c2abd103e42
7
- data.tar.gz: 945371efe3340ddd3a41f887572d6214b6940659a16cb9602cce9cec9cfd0f1ec104ef077a87b4658dff725bbd709cc131d09fda282e655cd441b794b948dffc
6
+ metadata.gz: 3a5d46059231408e13f9fe1752a164a038dfead63b28601184552410982eb04532a8f4083355ac42cf51adab9dfdb4ca95d3c3bed28b0fbf2b45f69a0e9a3dec
7
+ data.tar.gz: 36855434041c486606a055cd42029dec03a8613a62aaeefd4ce8431fce89cd28f8e07398b2d395d5b2ecec0d9a8edad85abae1556f76c0f6e572077285ff8b37
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
- === 1.0.0 (2014-09-15)
1
+ === 1.1.0 (2017-09-06)
2
+
3
+ * Support :position=>:before option to annotate to put annotation at the beginning of the file (a0s) (#4)
4
+
5
+ === 1.0.0 (2015-09-14)
2
6
 
3
7
  * Initial Public Release
@@ -1,4 +1,4 @@
1
- Copyright (c) 2015 Jeremy Evans
1
+ Copyright (c) 2015-2017 Jeremy Evans
2
2
  Copyright (c) 2013-2015 Kenny Meyer
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining
@@ -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
@@ -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 m = current.reverse.match(/#{"#{$/}# Table: ".reverse}/m)
34
- offset = current.length - m.end(0) + 1
35
- unless current[offset..-1].match(/^[^#]/)
36
- # If Table: comment exists, and there are no
37
- # uncommented lines between it and the end of the file
38
- # then replace current comment instead of appending it
39
- current = current[0...offset].rstrip
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
@@ -5,3 +5,5 @@ end
5
5
  # Columns:
6
6
  # id | integer | PRIMARY KEY AUTOINCREMENT
7
7
  # name | varchar(255) | NOT NULL
8
+ # Indexes:
9
+ # categories_name_key | UNIQUE (name)
@@ -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,9 @@
1
+ # Table: categories
2
+ # Columns:
3
+ # id | integer | PRIMARY KEY AUTOINCREMENT
4
+ # name | varchar(255) | NOT NULL
5
+ # Indexes:
6
+ # categories_name_key | UNIQUE (name)
7
+
8
+ class SCategory < Sequel::Model(SDB[:categories])
9
+ 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
@@ -0,0 +1,8 @@
1
+ # Table: manufacturers
2
+ # Primary Key: (name, location)
3
+ # Columns:
4
+ # name | varchar(255) |
5
+ # location | varchar(255) |
6
+
7
+ class SManufacturer < Sequel::Model(SDB[:manufacturers])
8
+ 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
- File.mkdir('spec/tmp') unless File.directory?('spec/tmp')
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
- it "#annotate should annotate the file comment" do
159
- FileUtils.cp(Dir['spec/unannotated/*.rb'], 'spec/tmp')
160
-
161
- [Item, Category, Manufacturer, SItem, SCategory, SManufacturer].each do |model|
162
- filename = model.name.downcase
163
- 2.times do
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
- it ".annotate should annotate all files given" do
171
- FileUtils.cp(Dir['spec/unannotated/*.rb'], 'spec/tmp')
173
+ it ".annotate #{desc} should annotate all files given" do
174
+ FileUtils.cp(Dir['spec/unannotated/*.rb'], 'spec/tmp')
172
175
 
173
- 2.times do
174
- Sequel::Annotate.annotate(Dir["spec/tmp/*.rb"])
175
- [Item, Category, Manufacturer, SItem, SCategory, SManufacturer].each do |model|
176
- filename = model.name.downcase
177
- File.read("spec/tmp/#{filename}.rb").must_equal File.read("spec/annotated/#{filename}.rb")
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.0.0
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: 2015-09-15 00:00:00.000000000 Z
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/annotated/category.rb
91
- - spec/annotated/item.rb
92
- - spec/annotated/manufacturer.rb
93
- - spec/annotated/scategory.rb
94
- - spec/annotated/sitem.rb
95
- - spec/annotated/smanufacturer.rb
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.4.5.1
136
+ rubygems_version: 2.6.13
131
137
  signing_key:
132
138
  specification_version: 4
133
139
  summary: Annotate Sequel models with schema information