class AnnotateRoutes::Annotator

Constants

COMMENT

Public Class Methods

new(root, route) click to toggle source
# File lib/annotate_routes/annotator.rb, line 7
def initialize(root, route)
  @root = root
  @reqs = route.fetch(:reqs)
  @info = route.fetch(:info)
end

Public Instance Methods

action() click to toggle source
# File lib/annotate_routes/annotator.rb, line 40
def action
  @action ||= @reqs.split('#').last
end
annotate!() click to toggle source
# File lib/annotate_routes/annotator.rb, line 13
def annotate!
  raise ControllerNotFound.new(controller_file_path) unless File.exists?(controller_file_path)

  File.open(controller_file_path, 'r+') do |f|
    body = f.read

    raise ActionNotFound.new(@reqs) unless body =~ /def #{action}\W/

    new_body = body.gsub(/(?:^ *# #{COMMENT}\n(?: *#.*\n)+)?( *def #{action}\W)/, comment + "\\1")

    f.rewind
    f.write(new_body)
  end
end
comment() click to toggle source
# File lib/annotate_routes/annotator.rb, line 28
def comment
  ["#{COMMENT}", *@info.map {|i| i.join(' ')} ].map {|c| "  # " + c }.join("\n") + "\n"
end
controller() click to toggle source
# File lib/annotate_routes/annotator.rb, line 36
def controller
  @controller ||= @reqs.split('#').first
end
controller_file_path() click to toggle source
# File lib/annotate_routes/annotator.rb, line 32
def controller_file_path
  @root.join("app/controllers/#{controller}_controller.rb")
end