Class: AnnotateModels::ModelAnnotationGenerator
- Inherits:
-
Object
- Object
- AnnotateModels::ModelAnnotationGenerator
- Defined in:
- lib/annotate_models/model_annotation_generator.rb
Overview
Class contains the Ruby code to generate the annotations
Instance Method Summary collapse
-
#apply_annotation(path, suffix = nil, extension = "rb", plural = false) ⇒ Object
Apply annotations to a file.
- #apply_to_factories ⇒ Object
- #apply_to_fixtures ⇒ Object
- #apply_to_model_tests ⇒ Object
- #apply_to_models ⇒ Object
-
#generate ⇒ Object
Gather model classes and generate annotation for each one.
-
#initialize ⇒ ModelAnnotationGenerator
constructor
A new instance of ModelAnnotationGenerator.
-
#print ⇒ Object
Print out the annotation text.
Constructor Details
#initialize ⇒ ModelAnnotationGenerator
Returns a new instance of ModelAnnotationGenerator.
9 10 11 |
# File 'lib/annotate_models/model_annotation_generator.rb', line 9 def initialize @annotations = {} end |
Instance Method Details
#apply_annotation(path, suffix = nil, extension = "rb", plural = false) ⇒ Object
Apply annotations to a file
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/annotate_models/model_annotation_generator.rb', line 20 def apply_annotation(path, suffix=nil, extension="rb", plural=false) pn_models = Pathname.new(path) return unless pn_models.exist? suffix = "_#{suffix}" unless suffix == nil extension = (extension == nil) ? "" : ".#{extension}" @annotations.each do |model, annotation| prefix = (plural) ? model.name.pluralize : model.name pn = pn_models + "#{ActiveSupport::Inflector.underscore(prefix)}#{suffix}#{extension}" text = File.open(pn.to_path) { |fp| fp.read } re = Regexp.new("^#-(?:--)+-\n# #{model.name}.*\n(?:#.+\n)+#-(?:--)+-\n", Regexp::MULTILINE) if re =~ text text = text.sub(re, annotation) else text = "#{text}\n#{annotation}" end File.open(pn.to_path, "w") { |fp| fp.write(text) } puts " Annotated #{pn.to_path}." end end |
#apply_to_factories ⇒ Object
40 41 42 |
# File 'lib/annotate_models/model_annotation_generator.rb', line 40 def apply_to_factories self.apply_annotation("test/factories", suffix="factory") end |
#apply_to_fixtures ⇒ Object
44 45 46 |
# File 'lib/annotate_models/model_annotation_generator.rb', line 44 def apply_to_fixtures self.apply_annotation("test/fixtures", suffix=nil, extension="yml", plural=true) end |
#apply_to_model_tests ⇒ Object
52 53 54 |
# File 'lib/annotate_models/model_annotation_generator.rb', line 52 def apply_to_model_tests self.apply_annotation("test/models", suffix="test") end |
#apply_to_models ⇒ Object
48 49 50 |
# File 'lib/annotate_models/model_annotation_generator.rb', line 48 def apply_to_models self.apply_annotation("app/models") end |
#generate ⇒ Object
Gather model classes and generate annotation for each one.
59 60 61 62 63 64 65 66 67 |
# File 'lib/annotate_models/model_annotation_generator.rb', line 59 def generate Dir["app/models/*.rb"].each do |path| result = File.basename(path).scan(/^(.+)\.rb/)[0][0] model = eval(ActiveSupport::Inflector.camelize(result)) next if model.respond_to?(:abstract_class) && model.abstract_class next unless model < ActiveRecord::Base @annotations[model] = generate_annotation(model) unless @annotations.keys.include?(model) end end |
#print ⇒ Object
Print out the annotation text.
72 73 74 75 76 77 |
# File 'lib/annotate_models/model_annotation_generator.rb', line 72 def print @annotations.values.sort.each do |annotation| puts annotation puts end end |