class RouteCounter::FileRecorder

Public Class Methods

action_visited(controller_name, action_name) click to toggle source
# File lib/route_counter/file_recorder.rb, line 37
def action_visited(controller_name, action_name)
  # write down it was visited
  file_path = File.join(File.join(current_directory, controller_name, action_name))
  logdev = LogDevice.new(file_path)
  logdev.write('X')
  logdev.close
end
actions_visited() click to toggle source
# File lib/route_counter/file_recorder.rb, line 45
def actions_visited
  # returns what was visited and counts
  read_directory(current_directory)
end
clear!() click to toggle source
# File lib/route_counter/file_recorder.rb, line 58
def clear!
  FileUtils.rm_rf(parent_directory)
end
current_directory() click to toggle source
# File lib/route_counter/file_recorder.rb, line 18
def current_directory
  File.join(parent_directory, 'current')
end
parent_directory() click to toggle source
# File lib/route_counter/file_recorder.rb, line 14
def parent_directory
  RouteCounter.config.directory
end
read_directory(root) click to toggle source
# File lib/route_counter/file_recorder.rb, line 22
def read_directory(root)
  out = {}
  Dir[ File.join(root, '**', '*') ].each do |file|
    next if File.directory?(file)
    hits = File.size(file) # each should be 1 byte
    action_name = File.basename(file)
    controller_name = File.dirname(file)
    controller_name.gsub!("#{root}/", '')
    key = "#{controller_name}##{action_name}"
    out[key] ||= 0
    out[key] += hits
  end
  out
end
rotate!() click to toggle source
# File lib/route_counter/file_recorder.rb, line 50
def rotate!
  # lock the current one and return the visited
  alt_directory = File.join(parent_directory, "#{Time.now.to_i}-#{rand(99999999)}")
  FileUtils.mkdir_p(current_directory) # make sure it's there
  FileUtils.mv(current_directory, alt_directory)
  read_directory(alt_directory)
end