class FoodCritic::Linter

def cookbook_dir(file)

Returns:
  • (String) - the path to the cookbook

Parameters:
  • file (String) -- - a file path in the cookbook
def cookbook_dir(file)
  @dir_cache ||= {}
  abs_file = File.absolute_path(file)
  # lookup the file in the cache has and return that if we find something
  cook_val = @dir_cache[abs_file]
  return cook_val unless cook_val.nil?
  if file =~ /\.erb$/
    # split each directory into an item in the array
    dir_array = File.dirname(file).split(File::SEPARATOR)
    # walk through the array of directories backwards until we hit the templates directory
    position = -1
    position -= 1 until dir_array[position] == "templates"
    # go back 1 more position to get to the cookbook dir
    position -= 1
    # slice from the start to the cookbook dir and then join it all back to a string
    cook_val = dir_array.slice(0..position).join(File::SEPARATOR)
  else
    # determine the difference to the root of the CB from our file's directory
    relative_difference = case File.basename(file)
                          when "recipe.rb", "attributes.rb", "metadata.rb" then ""
                          else # everything else is 1 directory up ie. cookbook/recipes/default.rb
                            ".."
                          end
    cook_val = Pathname.new(File.join(File.dirname(file), relative_difference)).cleanpath
  end
  @dir_cache[abs_file] = cook_val
  cook_val
end