annotator 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +16 -2
- data/lib/annotator.rb +9 -3
- data/lib/annotator/model.rb +7 -2
- data/lib/annotator/model.rb.orig +66 -0
- data/lib/annotator/railtie.rb +6 -4
- data/lib/annotator/version.rb +1 -1
- metadata +20 -3
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= Annotator
|
2
2
|
|
3
|
-
If you just want to have column descriptions in your model file, [
|
3
|
+
If you just want to have column descriptions in your model file, {annotate_models}[https://github.com/ctran/annotate_models] should suite you well. Annotator will help you if you want to also keep your own comments about these columns.
|
4
4
|
|
5
5
|
Ever wondered what the heck is that column used for?
|
6
6
|
|
@@ -65,6 +65,12 @@ Of course similar thing happens when you remove column or change it's type.
|
|
65
65
|
* belongs_to association columns
|
66
66
|
* devise columns
|
67
67
|
* paperclip colums
|
68
|
+
* you can run it without rails (if you use sinatra for example), just add something like this to your Rakefile
|
69
|
+
task :annotate do
|
70
|
+
require 'annotator'
|
71
|
+
Annotator.run(File.join(Dir.pwd, "lib", "models"))
|
72
|
+
end
|
73
|
+
|
68
74
|
|
69
75
|
Contributions are very much welcome.
|
70
76
|
|
@@ -74,4 +80,12 @@ Contributions are very much welcome.
|
|
74
80
|
|
75
81
|
== Authors
|
76
82
|
|
77
|
-
|
83
|
+
Kacper Cieśla @ Tech-Angels http://www.tech-angels.com
|
84
|
+
|
85
|
+
Contributions:
|
86
|
+
|
87
|
+
* {Lucas Florio}[https://github.com/lucasefe] - made it working without rails
|
88
|
+
|
89
|
+
== License
|
90
|
+
|
91
|
+
Annotator released under the MIT license.
|
data/lib/annotator.rb
CHANGED
@@ -5,9 +5,15 @@ require 'annotator/initial_description'
|
|
5
5
|
|
6
6
|
module Annotator
|
7
7
|
|
8
|
-
def self.run
|
9
|
-
|
10
|
-
|
8
|
+
def self.run(models_path = nil)
|
9
|
+
models_path ||= defined?(Rails) ? "#{Rails.root}/app/models" : "."
|
10
|
+
update_models(models_path)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.update_models(models_path)
|
14
|
+
path = "#{models_path}/**/*.rb"
|
15
|
+
Dir.glob(path).sort.map do |filename|
|
16
|
+
Model.new(filename, models_path).update!
|
11
17
|
end
|
12
18
|
end
|
13
19
|
|
data/lib/annotator/model.rb
CHANGED
@@ -3,14 +3,19 @@ module Annotator
|
|
3
3
|
# Represents a single model file and associated class
|
4
4
|
class Model
|
5
5
|
|
6
|
-
def initialize(filename)
|
6
|
+
def initialize(filename, base_path)
|
7
7
|
@filename = filename
|
8
|
+
@base_path = base_path
|
8
9
|
@blocks = Hash.new {[]}
|
9
10
|
end
|
10
11
|
|
11
12
|
# Model class
|
12
13
|
def klass
|
13
|
-
|
14
|
+
begin
|
15
|
+
@filename.split(@base_path).last.split(/\.rb$/).first.camelize.constantize rescue nil
|
16
|
+
rescue Exception
|
17
|
+
nil
|
18
|
+
end
|
14
19
|
end
|
15
20
|
|
16
21
|
# Split file into 3 blocks: before attributes, attributes block, and after
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Annotator
|
2
|
+
|
3
|
+
# Represents a single model file and associated class
|
4
|
+
class Model
|
5
|
+
|
6
|
+
def initialize(filename, base_path)
|
7
|
+
@filename = filename
|
8
|
+
@base_path = base_path
|
9
|
+
@blocks = Hash.new {[]}
|
10
|
+
end
|
11
|
+
|
12
|
+
# Model class
|
13
|
+
def klass
|
14
|
+
<<<<<<< HEAD
|
15
|
+
begin
|
16
|
+
@filename.split('app/models/').last.split(/\.rb$/).first.camelize.constantize
|
17
|
+
rescue Exception
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
=======
|
21
|
+
@filename.split(@base_path).last.split(/\.rb$/).first.camelize.constantize rescue nil
|
22
|
+
>>>>>>> lucasefe/master
|
23
|
+
end
|
24
|
+
|
25
|
+
# Split file into 3 blocks: before attributes, attributes block, and after
|
26
|
+
# If there's no attributes block, content will be in :after part (for easier insertion)
|
27
|
+
def parse
|
28
|
+
@file = File.read(@filename).strip
|
29
|
+
current_block = :before
|
30
|
+
return @nodoc = true if @file.match(/^# Attributes\(nodoc\):$/i)
|
31
|
+
@file.split("\n").each do |line|
|
32
|
+
if line.match(/^#{Regexp.escape Attributes::HEADER} *$/)
|
33
|
+
current_block = :attributes
|
34
|
+
next
|
35
|
+
end
|
36
|
+
current_block = :after if current_block == :attributes && !line.match(Attributes::R_ATTRIBUTE_LINE)
|
37
|
+
@blocks[current_block] += [line]
|
38
|
+
end
|
39
|
+
|
40
|
+
@blocks[:after], @blocks[:before] = @blocks[:before], [] if @blocks[:after].empty?
|
41
|
+
end
|
42
|
+
|
43
|
+
# If this file does not have associated AR class it should be skipped
|
44
|
+
def skipped?
|
45
|
+
!klass || !klass.ancestors.include?(ActiveRecord::Base) || @nodoc
|
46
|
+
end
|
47
|
+
|
48
|
+
# Save changes to file if there were any
|
49
|
+
def update_file
|
50
|
+
output = (@blocks[:before] + @blocks[:attributes] + @blocks[:after]).join("\n").strip + "\n"
|
51
|
+
File.open(@filename,'w') { |f| f.write(output) } if output != @file
|
52
|
+
end
|
53
|
+
|
54
|
+
# Update file with new database information
|
55
|
+
def update!
|
56
|
+
parse
|
57
|
+
return true if skipped?
|
58
|
+
attributes = Attributes.new klass, @blocks[:attributes]
|
59
|
+
attributes.update!
|
60
|
+
@blocks[:attributes] = attributes.lines
|
61
|
+
update_file
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
|
data/lib/annotator/railtie.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
if defined?Rails
|
2
|
+
module Annotate
|
3
|
+
class Railtie < Rails::Railtie
|
4
|
+
rake_tasks do
|
5
|
+
Dir[File.join(File.dirname(__FILE__),'../tasks/*.rake')].each { |f| load f }
|
6
|
+
end
|
5
7
|
end
|
6
8
|
end
|
7
9
|
end
|
data/lib/annotator/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: annotator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,10 +10,10 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-06-
|
13
|
+
date: 2012-06-08 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
16
|
+
name: activerecord
|
17
17
|
requirement: !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
@@ -44,6 +44,22 @@ dependencies:
|
|
44
44
|
- - ! '>='
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rails
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '3.0'
|
47
63
|
description: ''
|
48
64
|
email:
|
49
65
|
- kacper.ciesla@tech-angels.com
|
@@ -59,6 +75,7 @@ files:
|
|
59
75
|
- lib/annotator/initial_description/base.rb
|
60
76
|
- lib/annotator/initial_description/rails.rb
|
61
77
|
- lib/annotator/model.rb
|
78
|
+
- lib/annotator/model.rb.orig
|
62
79
|
- lib/annotator/version.rb
|
63
80
|
- lib/annotator/attributes.rb
|
64
81
|
- lib/annotator/initial_description.rb
|