Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed wrong written specs, added abstract class handling

  • Loading branch information...
commit 0eea89fa33550478dab674375d71ee9989f82900 1 parent e3f8a29
Dmitrij Mjakotnyi authored
Showing with 57 additions and 25 deletions.
  1. +31 −13 lib/annotated/models.rb
  2. +26 −12 spec/annotated/models_spec.rb
44 lib/annotated/models.rb
View
@@ -33,11 +33,37 @@ class Annotator
def initialize model_const, model_file_full_path
@model, @path = model_const, model_file_full_path
+ @annotatable = true
end
attr_reader :path, :model
+ def annotatable? # I'm not sure is this a right word...
+ make_annotation!
+ @annotatable
+ end
+
def annotation
+ make_annotation!
+ end
+
+ def add_regexp
+ /\z/
+ end
+
+ def remove_regexp
+ border = '# '+STRONG_ANNOTATION
+ border.gsub!(' ', '\s')
+ border.gsub!('#', '\#')
+ %r{\n(\#--\n)?#{border}.+#{border}.*\n?(\#++)?}m
+ end
+
+ private
+
+ def make_annotation!
+ return @annotatable=false if @model.abstract_class?
+ return @annotation if defined?(@annotation)
+
# TODO add exception handling inside this method to notify about broken models
table_name = @model.table_name.to_s
annotation_header = <<annheader
@@ -56,7 +82,6 @@ def annotation
# append table name:
annotation_table << "# Table name: #{table_name}"
annotation_table << '#'
- # TODO check if abstract class (http://apidock.com/rails/ActiveRecord/Base/inspect/class)
column_with_maximum_name_length = @model.columns.map(&:name).max_by { |c| c.length }.length
@model.columns.each do |column|
@@ -96,18 +121,7 @@ def annotation
end
end
- annotation_header + annotation_table.join("\n") + annotation_index.join("\n") + annotation_footer
- end
-
- def add_regexp
- /\z/
- end
-
- def remove_regexp
- border = '# '+STRONG_ANNOTATION
- border.gsub!(' ', '\s')
- border.gsub!('#', '\#')
- %r{\n(\#--\n)?#{border}.+#{border}.*\n?(\#++)?}m
+ @annotation = annotation_header + annotation_table.join("\n") + annotation_index.join("\n") + annotation_footer
end
end # Annotator
@@ -123,12 +137,16 @@ def remove_regexp
def annotate! options = {}
print_message "Annotating "
+
annotators.each do |annotator|
+ next unless annotator.annotatable?
+
# TODO remove old annotation
print_message annotator.model.to_s+' '
content = File.read(annotator.path).gsub(annotator.add_regexp, annotator.annotation)
write_file annotator.path, content
end
+
puts_message "\n"
end
38 spec/annotated/models_spec.rb
View
@@ -7,20 +7,25 @@
let(:annotator) { Annotated::Models::Annotator }
+ before :all do
+ User = Class.new
+ Avatar = Class.new
+ end
+
before :each do
StubFile.clear!
end
context "#annotate!" do
- it "should annotate all models" do
- model1 = mock_model("User")
- model2 = mock_model("Avatar")
+ it "must annotate all models" do
+ annotator.any_instance.stub(:make_annotation!)
- subject.should_receive(:annotators).and_return(annotators=[annotator.new(model1,'path1'),annotator.new(model2,'path2')])
- annotators.each do |a|
- a.should_receive(:annotation).and_return('String')
- end
+ subject.should_receive(:annotators).
+ and_return(
+ annotators=[annotator.new(User,'path1'),annotator.new(Avatar,'path2')]
+ )
+ annotators.each { |a| a.should_receive(:annotation).and_return('String') }
File.should_receive(:read).with('path1').and_return((user_file=StubFile.new(<<-USER,'path1')).content)
class User < ActiveRecord::Base
@@ -48,15 +53,24 @@ class Avatar < ActiveRecord::Base
TEXT
end
+ it "must not annotate abstract AR classes" do
+ SomeBase = Class.new
+
+ SomeBase.should_receive(:abstract_class?) { true }
+
+ subject.should_receive(:annotators).and_return(annotators=[annotator.new(SomeBase,'some_base_model_path')])
+
+ File.should_not_receive(:read).with('some_base_model_path')
+
+ subject.annotate!
+ end
+
end
context "#denotate!" do
- it "should denotate all models" do
- model1 = mock_model("User")
- model2 = mock_model("Avatar")
-
- subject.should_receive(:annotators).and_return(annotators=[annotator.new(model1,'path1'),annotator.new(model2,'path2')])
+ it "must denotate all models" do
+ subject.should_receive(:annotators).and_return(annotators=[annotator.new(User,'path1'),annotator.new(Avatar,'path2')])
File.should_receive(:read).with('path1').and_return((user_file=StubFile.new(<<-USER,'path1')).content)
class User < ActiveRecord::Base
Please sign in to comment.
Something went wrong with that request. Please try again.