Skip to content

Instantly share code, notes, and snippets.

@oren
Created September 25, 2010 01:38
Show Gist options
  • Save oren/596353 to your computer and use it in GitHub Desktop.
Save oren/596353 to your computer and use it in GitHub Desktop.
module ProjectReader
extend self # keep extract_keys 'module private'
# finds keys in .erb files from a given directory
def find_keys(path)
repo_name = File.basename(path)
if !File.directory?(path)
raise "The directory #{repo_name} does not exist in #{File.dirname(path)}"
end
client_id = Client.find_by_repository_name( repo_name )
if !client_id
raise "The Client with the repository named #{repo_name} was not found in the DB"
end
found_keys = []
found_keys = Dir["#{path}/**/*.erb"].reduce([],&extract_keys)
if found_keys
mark_missing_keys_as_deleted(found_keys, client_id)
end
found_keys
end
private
# extracts keys from a given file and adds to a given array
def extract_keys
@extract_keys ||= lambda do |rval,path|
rval += File.open(path,"r") do |file|
file.read.scan(/@content\[\'(.+?)\'\]/).flatten
end
end
end
def mark_missing_keys_as_deleted(found_keys, client_id)
client_id = Client.find(client_id)
current_keys = TranslationKey.all(:conditions => {:client_id => client_id})
missing_keys = current_keys.reject{|key| found_keys.include?(key.name)}
missing_keys.each do |key|
key.deleted = true
key.save!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment