class Chef::ChefFS::FileSystem::Repository::ChefRepositoryFileSystemCookbookEntry

NB: unlike most other things in chef_fs/file_system/repository, this class represents both files and directories, so it needs to have the methods/if branches for each.

Attributes

bare_name[R]
display_name[R]
display_path[R]
file_path[R]
name[R]
parent[R]
path[R]
recursive[R]
ruby_only[R]

Public Class Methods

new(name, parent, file_path = nil, ruby_only = false, recursive = false) click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 44
def initialize(name, parent, file_path = nil, ruby_only = false, recursive = false)
  @parent = parent
  @name = name
  @path = Chef::ChefFS::PathUtils.join(parent.path, name)
  @ruby_only = ruby_only
  @recursive = recursive
  @data_handler = nil
  @file_path = file_path || "#{parent.file_path}/#{name}"
end

Public Instance Methods

can_have_child?(name, is_dir) click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 63
def can_have_child?(name, is_dir)
  if is_dir
    return recursive && name != "." && name != ".."
  elsif ruby_only
    return false if name[-3..] != ".rb"
  end

  # Check chefignore
  ignorer = self

  loop do
    if ignorer.is_a?(ChefRepositoryFileSystemCookbookDir)
      # Grab the path from entry to child
      path_to_child = name
      child = self
      while child != ignorer
        path_to_child = PathUtils.join(child.name, path_to_child)
        child = child.parent
      end
      # Check whether that relative path is ignored
      return !ignorer.chefignore || !ignorer.chefignore.ignored?(path_to_child)
    end
    ignorer = ignorer.parent
    break unless ignorer
  end

  true
end
child(name) click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 155
def child(name)
  if can_have_child?(name, true) || can_have_child?(name, false)
    result = make_child_entry(name)
  end
  result || NonexistentFSObject.new(name, self)
end
children() click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 54
def children
  entries = Dir.entries(file_path).sort
    .map { |child_name| make_child_entry(child_name) }
    .select { |child| child && can_have_child?(child.name, child.dir?) }
  entries.select { |entry| !(entry.dir? && entry.children.size == 0 ) }
rescue Errno::ENOENT
  raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
end
compare_to(other) click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 166
def compare_to(other)
  nil
end
create_child(child_name, file_contents = nil) click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 100
def create_child(child_name, file_contents = nil)
  child = make_child_entry(child_name)
  if child.exists?
    raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, child)
  end

  if file_contents
    child.write(file_contents)
  else
    begin
      Dir.mkdir(child.file_path)
    rescue Errno::EEXIST
      raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, child)
    end
  end
  child
end
delete(recurse) click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 122
def delete(recurse)
  FileSystemCache.instance.delete!(file_path)
  begin
    if dir?
      unless recurse
        raise MustDeleteRecursivelyError.new(self, $!)
      end

      FileUtils.rm_r(file_path)
    else
      File.delete(file_path)
    end
  rescue Errno::ENOENT
    raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
  end
end
dir?() click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 118
def dir?
  File.directory?(file_path)
end
exists?() click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 139
def exists?
  File.exist?(file_path) && (parent.nil? || parent.can_have_child?(name, dir?))
end
path_for_printing() click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 96
def path_for_printing
  file_path
end
read() click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 143
def read
  File.open(file_path, "rb", &:read)
rescue Errno::ENOENT
  raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
end
root() click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 162
def root
  parent.root
end
write(content) click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 149
def write(content)
  File.open(file_path, "wb") do |file|
    file.write(content)
  end
end
write_pretty_json() click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 92
def write_pretty_json
  false
end

Protected Instance Methods

make_child_entry(child_name) click to toggle source
# File lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb, line 172
def make_child_entry(child_name)
  ChefRepositoryFileSystemCookbookEntry.new(child_name, self, nil, ruby_only, recursive)
end