Class: AnnotateControllers::Annotator

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_controllers/annotator.rb

Constant Summary collapse

VERBS =
['GET', 'POST', 'PATCH', 'PUT', 'DELETE']

Class Method Summary collapse

Class Method Details

.annotate!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/annotate_controllers/annotator.rb', line 10

def annotate!
  rake_output = []
  mapped_routes = routes
  pattern = /.*\/controllers\/(.*)\_controller\./
  controller_paths = Dir[Rails.root.join('app/controllers/**/*_controller.rb')]

  controller_paths.each do |path|
    raise ControllerNotFound.new(path) unless File.exists?(path)

    lines = []
    prefixes = []
    controller_name = pattern.match(path)[-1] # ignore match for entire filename
    original_file_digest = Digest::SHA1.hexdigest(File.read(path))

    IO.readlines(path).each do |line|
      action_name = /def\W(.*)/.match(line).try(:[], -1)

      if action_name && matches = select_routes_from_method(mapped_routes, controller_name, action_name)
        matches.each do |match|
          # add matched line to global array in order to reuse prefixes
          prefixes << { action: action_name, prefix: match[:prefix] }
          # replace existing annotation (if exists)
          if overwrite_self?(match[:verb], lines[-1])
            lines[-1] = comment(prefixes, match)
          # otherwise, insert comment
          else
            lines << comment(prefixes, match)
          end
        end
      end

      lines << line
    end

    File.open(path, 'w') do |file|
      file.puts lines
    end

    new_file_digest = Digest::SHA1.hexdigest(File.read(path))
    rake_output << "Annotated #{controller_name}_controller" unless original_file_digest == new_file_digest
  end

  puts rake_output.any? ? rake_output : 'Nothing annotated'
end

.comment(prefixes, match) ⇒ Object



62
63
64
65
# File 'lib/annotate_controllers/annotator.rb', line 62

def comment(prefixes, match)
  "\t# #{match[:verb]} #{trim(match[:uri])}"\
  "#{prefix_or_shared(prefixes, match[:verb], match[:prefix], match[:action])}\n"
end

.overwrite_self?(verb, line) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/annotate_controllers/annotator.rb', line 96

def overwrite_self?(verb, line)
  "# #{verb}" == /(\#\s\w*)/.match(line).try(:[], 0)
end

.prefix_or_shared(prefixes, verb, prefix, action) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/annotate_controllers/annotator.rb', line 67

def prefix_or_shared(prefixes, verb, prefix, action)
  if prefix.present?
    ' (' + prefix + '_path)'
  else
    shared_prefix(prefixes, verb, action)
  end
end

.routesObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/annotate_controllers/annotator.rb', line 100

def routes
  mapped_routes = []
  AnnotateControllers::Inspector.map_all_routes.each do |route|
    r = route.split(' ')
    contraction = r[-1].split('#') # controller, action, controlleraction...
    mapped_routes << {
      prefix: r.size > 3 ? r.try(:[], 0) : nil,
      verb: r.size > 3 ? r[1] : r[0],
      uri: r.size > 3 ? r[2] : r[1],
      controller: contraction.first,
      action: contraction.last
    }
  end
  mapped_routes
end

.select_routes_from_method(routes, controller, action) ⇒ Object



55
56
57
58
59
60
# File 'lib/annotate_controllers/annotator.rb', line 55

def select_routes_from_method(routes, controller, action)
  results = routes.select{ |r|
    r[:controller] == controller && r[:action] == action
  }
  results.any? ? results : nil
end

.shared_action_prefix(action) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/annotate_controllers/annotator.rb', line 85

def shared_action_prefix(action)
  case action
  when 'create'
    'index'
  when 'update'
    'show'
  when 'destroy'
    'show'
  end
end

.shared_prefix(prefixes, verb, action) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/annotate_controllers/annotator.rb', line 75

def shared_prefix(prefixes, verb, action)
  if VERBS.include? verb
    ' (' +
    prefixes.detect{ |l| l[:action] == shared_action_prefix(action) }.try(:[], :prefix) +
    '_path)'
  else
    ''
  end
end

.trim(string) ⇒ Object

Removes (.:format) from URI pattern string



117
118
119
120
# File 'lib/annotate_controllers/annotator.rb', line 117

def trim(string)
  string.slice! '(.:format)'
  string
end