active_record-annotate 0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7da2641db8b7f89154f1822775ec8d236e36651f
4
+ data.tar.gz: 9c51bd0f547609d06fa6542d36172f9dfc8aa680
5
+ SHA512:
6
+ metadata.gz: 7ff59459899aa01aa6571380e614727e1d64b22e60312426ce7aa399b99eb90bb14598fb1f432edd55a6d72745456f36d3e46437ef5e7a0d3ac18fca2696b408
7
+ data.tar.gz: 0ceb493a98b0c698db070b29d3781964f545bd638a7e327e3e28a0938269bbf1c5c44a9b5350fde0fc5eb68c7bcf9b3ed8ac5c02a202838562a3ff3cf9cd22b4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Vsevolod Romashov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # ActiveRecord::Annotate
2
+
3
+ `ActiveRecord::Annotate` is a simple `ActiveRecord` plugin for annotating your rails models. It is based on `ActiveRecord::SchemaDumper` so the annotation format is very close to what you see in `db/schema.rb`.
4
+
5
+ ## Installation
6
+
7
+ Trivial.
8
+
9
+ ``` ruby
10
+ # Gemfile
11
+ gem 'active_record-annotate'
12
+ ```
13
+
14
+ ``` sh
15
+ $ bundle
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ Gem adds a simple `db:annotate` rake task - it just writes the annotation to the top of each model file in a comment block. Magic encoding comment is preserved.
21
+
22
+ This is what it looks like:
23
+
24
+ ``` ruby
25
+ # create_table :documents, force: true do |t|
26
+ # t.string :title
27
+ # t.text :content
28
+ # t.integer :category_ids, array: true
29
+ # t.datetime :created_at
30
+ # t.datetime :updated_at
31
+ # end
32
+ #
33
+ # add_index :documents, [:category_ids], name: :index_documents_on_category_ids, using: :gin
34
+
35
+ class Document < ActiveRecord::Base
36
+ # ...
37
+ ```
38
+
39
+ ## Roadmap
40
+
41
+ * Cover everything with tests
42
+ * Write YARD docs
43
+ * Add some means to configure the annotation process (annotation format, a place to put it)
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/annotate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'active_record-annotate'
8
+ spec.version = ActiveRecord::Annotate::VERSION
9
+ spec.authors = ['Vsevolod Romashov']
10
+ spec.email = ['7@7vn.ru']
11
+ spec.summary = %q{ActiveRecord models annotator based on rails' schema dumper}
12
+ spec.description = %q{Adds a rake task which prepends each model file with an excerpt about the corresponding table from db/schema.rb}
13
+ spec.homepage = 'https://github.com/7even/active_record-annotate'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^spec/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'rails', '>= 3.2'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ end
@@ -0,0 +1,34 @@
1
+ require 'active_record/annotate/dumper'
2
+ require 'active_record/annotate/writer'
3
+ require 'active_record/annotate/version'
4
+ require 'active_record/annotate/railtie'
5
+
6
+ module ActiveRecord
7
+ module Annotate
8
+ class << self
9
+ def annotate
10
+ models.each do |table_name, file_path|
11
+ annotation = Dumper.dump(table_name)
12
+ Writer.write(annotation, file_path)
13
+ end
14
+ end
15
+
16
+ def models
17
+ models_dir = Rails.root.join('app/models')
18
+ files_mask = models_dir.join('**', '*.rb')
19
+
20
+ Dir.glob(files_mask).each_with_object(Hash.new) do |path, models|
21
+ # .../app/models/car/hatchback.rb -> car/hatchback
22
+ short_path = path.sub(models_dir.to_s + '/', '').sub(/\.rb$/, '')
23
+ # skip any app/models/concerns files
24
+ next if short_path.starts_with?('concerns')
25
+
26
+ # car/hatchback -> Car::Hatchback
27
+ klass = short_path.camelize.constantize
28
+ # collect only AR::Base descendants
29
+ models[klass.table_name] = path if klass < ActiveRecord::Base
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ module ActiveRecord
2
+ module Annotate
3
+ module Dumper
4
+ class << self
5
+ def dump(table_name, connection = ActiveRecord::Base.connection)
6
+ string_io = StringIO.new
7
+ dumper(connection).send(:table, table_name, string_io)
8
+
9
+ process_annotation(string_io)
10
+ end
11
+
12
+ private
13
+ def dumper(connection)
14
+ ActiveRecord::SchemaDumper.send(:new, connection)
15
+ end
16
+
17
+ def process_annotation(string_io)
18
+ string_io.string.split("\n").map do |line|
19
+ line.tap do |line|
20
+ # commenting out the line
21
+ line[0] = '#'
22
+ # replacing strings with symbols
23
+ line.gsub!(/"(\w+)"/, ':\1')
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails'
2
+
3
+ module ActiveRecord
4
+ module Annotate
5
+ class Railtie < Rails::Railtie
6
+ rake_tasks do
7
+ load 'tasks/annotate.rake'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module Annotate
3
+ VERSION = '0.1'
4
+ end
5
+ end
@@ -0,0 +1,38 @@
1
+ module ActiveRecord
2
+ module Annotate
3
+ module Writer
4
+ class << self
5
+ def write(annotation, path)
6
+ new_file_content = assemble(annotation, path)
7
+
8
+ temp_path = "#{path}.annotated"
9
+ File.open(temp_path, 'w') do |temp_file|
10
+ temp_file.puts(new_file_content)
11
+ end
12
+
13
+ File.delete(path)
14
+ File.rename(temp_path, path)
15
+ end
16
+
17
+ def assemble(annotation, path)
18
+ lines = File.read(path).split("\n")
19
+
20
+ if lines.first =~ /^\s*#.*coding/
21
+ # encoding: utf-8 encountered on the first line
22
+ encoding_line = lines.shift
23
+ end
24
+
25
+ while lines.first.starts_with?('#') || lines.first.blank?
26
+ # throw out comments and empty lines in the beginning of the file (old annotation)
27
+ lines.shift
28
+ end
29
+
30
+ lines.unshift(*annotation, nil)
31
+ lines.unshift(encoding_line) unless encoding_line.nil?
32
+
33
+ lines.join("\n")
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,6 @@
1
+ namespace :db do
2
+ desc 'Annotate the models'
3
+ task annotate: :environment do
4
+ ActiveRecord::Annotate.annotate
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record-annotate
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Vsevolod Romashov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Adds a rake task which prepends each model file with an excerpt about
56
+ the corresponding table from db/schema.rb
57
+ email:
58
+ - 7@7vn.ru
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - active_record-annotate.gemspec
69
+ - lib/active_record/annotate.rb
70
+ - lib/active_record/annotate/dumper.rb
71
+ - lib/active_record/annotate/railtie.rb
72
+ - lib/active_record/annotate/version.rb
73
+ - lib/active_record/annotate/writer.rb
74
+ - lib/tasks/annotate.rake
75
+ homepage: https://github.com/7even/active_record-annotate
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.0.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: ActiveRecord models annotator based on rails' schema dumper
99
+ test_files: []