annotate_models 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 53b7a0a3bcc8c72bbecf825408c5a3abed24ce81
4
+ data.tar.gz: 144c14334120433ef0a61f6fdee4aa1ee3ebdb67
5
+ SHA512:
6
+ metadata.gz: 6b11348c86f2bac0d9e009649e5c2f95b28b5ec77dbe0db50fd96617e4e962df5da063f17a614389d310cbeb90a348ff7dd962c55e4c1926b9d4b2cd14b5c789
7
+ data.tar.gz: 11faa9d4fcef539177fdba0619ccad7ffb349513152018dc50e2268a565d4faa361d5b5e729fa63018ef1f6aa11ba1e9ff068d67063fafd9b99813200ec1037a
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nathan Brazil
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # annotate_models
2
+
3
+ This is my own re-write of an earlier version [ctran/annoate_models](https://github.com/ctran/annotate_models) when
4
+ work on it waned. This work started out as a Rails plugin; I am now re-bundling it as a gem since plugin support for Rails has
5
+ long been deprecated.
6
+
7
+ ## Installation
8
+
9
+ If you are using Bundler, add this line to your Gemfile:
10
+
11
+ ```ruby
12
+ gem "annotate_models"
13
+ ```
14
+
15
+ Otherwise, run this command:
16
+
17
+ ```
18
+ gem install annotate_models"
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ Run this command from the root folder of your Rails application:
24
+
25
+ ```
26
+ rake annotate:all
27
+ ```
28
+
29
+ For details, run ```rake -T```.
30
+
31
+ ## Credit
32
+
33
+ I first learned to love the functionality of Dave Thomas' annotate_models plugin (you can find a repo for it
34
+ [here](https://github.com/alsemyonov/annotate_models)). Later, when it became un-maintained and broke, I switched over to
35
+ [ctran/annoate](https://github.com/ctran/annotate_models). Then, when work on it waned and broke as well, I decided to write my own
36
+ as an exercise.
37
+
38
+ So thanks go out to Pragmatic Dave as well as to the author and contributors of ctran/annotate_models.
@@ -0,0 +1,108 @@
1
+ module AnnotateModels
2
+
3
+ ##
4
+ # Class contains the Ruby code to generate the annotations
5
+ #
6
+
7
+ class ModelAnnotationGenerator
8
+
9
+ def initialize
10
+ @annotations = {}
11
+ end
12
+
13
+ ##
14
+ # Apply annotations to a file
15
+ #
16
+ # @param path [String] Relative path (from root of application) of directory to apply annotations to.
17
+ # @param suffix [String] Optionally specify suffix of files to apply annotation to (e.g. "<model.name>_<suffix>.rb").
18
+ # @param extension [String] Optionally specify extension of files to apply annotaations to (e.g. "<model.name>_<suffix>.<extension>").
19
+
20
+ def apply_annotation(path, suffix=nil, extension="rb", plural=false)
21
+ pn_models = Pathname.new(path)
22
+ return unless pn_models.exist?
23
+ suffix = "_#{suffix}" unless suffix == nil
24
+ extension = (extension == nil) ? "" : ".#{extension}"
25
+ @annotations.each do |model, annotation|
26
+ prefix = (plural) ? model.name.pluralize : model.name
27
+ pn = pn_models + "#{ActiveSupport::Inflector.underscore(prefix)}#{suffix}#{extension}"
28
+ text = File.open(pn.to_path) { |fp| fp.read }
29
+ re = Regexp.new("^#-(?:--)+-\n# #{model.name}.*\n(?:#.+\n)+#-(?:--)+-\n", Regexp::MULTILINE)
30
+ if re =~ text
31
+ text = text.sub(re, annotation)
32
+ else
33
+ text = "#{text}\n#{annotation}"
34
+ end
35
+ File.open(pn.to_path, "w") { |fp| fp.write(text) }
36
+ puts "Annotated #{pn.to_path}."
37
+ end
38
+ end
39
+
40
+ def apply_to_factories
41
+ self.apply_annotation("test/factories", suffix="factory")
42
+ end
43
+
44
+ def apply_to_fixtures
45
+ self.apply_annotation("test/fixtures", suffix=nil, extension="yml", plural=true)
46
+ end
47
+
48
+ def apply_to_models
49
+ self.apply_annotation("app/models")
50
+ end
51
+
52
+ def apply_to_model_tests
53
+ self.apply_annotation("test/models", suffix="test")
54
+ end
55
+
56
+ ##
57
+ # Gather model classes and generate annotation for each one.
58
+
59
+ def generate
60
+ Dir["app/models/*.rb"].each do |path|
61
+ result = File.basename(path).scan(/^(.+)\.rb/)[0][0]
62
+ model = eval(ActiveSupport::Inflector.camelize(result))
63
+ next unless model < ActiveRecord::Base
64
+ @annotations[model] = generate_annotation(model) unless @annotations.keys.include?(model)
65
+ end
66
+ end
67
+
68
+ ##
69
+ # Print out the annotation text.
70
+
71
+ def print
72
+ @annotations.values.sort.each do |annotation|
73
+ puts annotation
74
+ puts
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ ##
81
+ # Generate annotation text.
82
+ # @param model [Class] An ActiveRecord model class.
83
+
84
+ def generate_annotation(model)
85
+ annotation = []
86
+ annotation << "#-#{'--' * 38}-"
87
+ annotation << "# #{model.name}"
88
+ annotation << "#"
89
+ annotation << "# Name SQL Type Null Default Primary"
90
+ annotation << "# ------------------------------ -------------------- ------- ------- -------"
91
+ model.columns.each do |column|
92
+ annotation << sprintf(
93
+ "# %-30s %-20s %-7s %-7s %-7s",
94
+ column.name,
95
+ column.sql_type,
96
+ column.null,
97
+ (column.default || ""),
98
+ column.name == model.primary_key
99
+ )
100
+ end
101
+ annotation << "#"
102
+ annotation << "#-#{'--' * 38}-"
103
+ annotation.join("\n") + "\n"
104
+ end
105
+
106
+ end
107
+
108
+ end
@@ -0,0 +1,11 @@
1
+ require "annotate_models"
2
+ require "rails"
3
+
4
+ module AnnotateModels
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :annotate_models
7
+ rake_tasks do
8
+ load "tasks/annotate_models.rake"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module AnnotateModels
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,3 @@
1
+ module AnnotateModels
2
+ require "annotate_models/railtie" if defined?(Rails)
3
+ end
@@ -0,0 +1,55 @@
1
+ require "annotate_models/model_annotation_generator"
2
+
3
+ #-----
4
+ # Ruby tasks
5
+ #-----
6
+
7
+ task annotate: "annotate:all"
8
+
9
+ namespace :annotate do
10
+
11
+ desc "Annotate everything"
12
+ task :all => [:factories, :fixtures, :models, "test:models"]
13
+
14
+ desc "Annotate factories"
15
+ task :factories do
16
+ puts "Annotating factories..."
17
+ am = AnnotateModels::ModelAnnotationGenerator.new
18
+ am.generate
19
+ am.apply_to_factories
20
+ end
21
+
22
+ desc "Annotate fixtures"
23
+ task :fixtures do
24
+ puts "Annotating fixtures..."
25
+ am = AnnotateModels::ModelAnnotationGenerator.new
26
+ am.generate
27
+ am.apply_to_fixtures
28
+ end
29
+
30
+ desc "Annotate ActiveRecord models"
31
+ task :models do
32
+ puts "Annotating models..."
33
+ am = AnnotateModels::ModelAnnotationGenerator.new
34
+ am.generate
35
+ am.apply_to_models
36
+ end
37
+
38
+ desc "Print annotations"
39
+ task :print do
40
+ am = AnnotateModels::ModelAnnotationGenerator.new
41
+ am.generate
42
+ am.print
43
+ end
44
+
45
+ namespace :test do
46
+ desc "Annotate model tests"
47
+ task :models do
48
+ puts "Annotating model tests..."
49
+ am = AnnotateModels::ModelAnnotationGenerator.new
50
+ am.generate
51
+ am.apply_to_model_tests
52
+ end
53
+ end
54
+
55
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: annotate_models
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Brazil
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ This is my own re-write of an earlier version [ctran/annoate](https://github.com/ctran/annotate_models) when
15
+ work on it waned. This work started out as a Rails plugin; I am now re-bundling it as a gem since plugin support for Rails has
16
+ long been deprecated.
17
+ email: nb@bitaxis.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - LICENSE
23
+ - README.md
24
+ - lib/annotate_models.rb
25
+ - lib/annotate_models/model_annotation_generator.rb
26
+ - lib/annotate_models/railtie.rb
27
+ - lib/annotate_models/version.rb
28
+ - lib/tasks/annotate_models.rake
29
+ homepage: https://github.com/bitaxis/annotate_models.git
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.4.5
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Simple gem that adds several rake tasks to annotate Rails source files with
53
+ model schema.
54
+ test_files: []
55
+ has_rdoc: