Class: AnnotateModels::ModelAnnotationGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_models/model_annotation_generator.rb

Overview

Class contains the Ruby code to generate the annotations

Instance Method Summary collapse

Constructor Details

#initializeModelAnnotationGenerator

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

Parameters:

  • path (String)

    Relative path (from root of application) of directory to apply annotations to.

  • suffix (String) (defaults to: nil)

    Optionally specify suffix of files to apply annotation to (e.g. “<model.name>_<suffix>.rb”).

  • extension (String) (defaults to: "rb")

    Optionally specify extension of files to apply annotaations to (e.g. “<model.name>_<suffix>.<extension>”).



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_factoriesObject



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_fixturesObject



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_testsObject



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_modelsObject



48
49
50
# File 'lib/annotate_models/model_annotation_generator.rb', line 48

def apply_to_models
  self.apply_annotation("app/models")
end

#generateObject

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 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