class Chef::ChefFS::FileSystem::FileSystemEntry

Attributes

file_path[R]

Public Class Methods

new(name, parent, file_path = nil) click to toggle source
Calls superclass method Chef::ChefFS::FileSystem::BaseFSDir.new
# File lib/chef/chef_fs/file_system/file_system_entry.rb, line 31
def initialize(name, parent, file_path = nil)
  super(name, parent)
  @file_path = file_path || "#{parent.file_path}/#{name}"
end

Public Instance Methods

children() click to toggle source
# File lib/chef/chef_fs/file_system/file_system_entry.rb, line 42
def children
  begin
    Dir.entries(file_path).sort.select { |entry| entry != '.' && entry != '..' }.map { |entry| make_child(entry) }
  rescue Errno::ENOENT
    raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
  end
end
create_child(child_name, file_contents=nil) click to toggle source
# File lib/chef/chef_fs/file_system/file_system_entry.rb, line 50
def create_child(child_name, file_contents=nil)
  child = make_child(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/file_system_entry.rb, line 71
def delete(recurse)
  if dir?
    if !recurse
      raise MustDeleteRecursivelyError.new(self, $!)
    end
    FileUtils.rm_rf(file_path)
  else
    File.delete(file_path)
  end
end
dir?() click to toggle source
# File lib/chef/chef_fs/file_system/file_system_entry.rb, line 67
def dir?
  File.directory?(file_path)
end
exists?() click to toggle source
# File lib/chef/chef_fs/file_system/file_system_entry.rb, line 82
def exists?
  File.exists?(file_path)
end
path_for_printing() click to toggle source
# File lib/chef/chef_fs/file_system/file_system_entry.rb, line 38
def path_for_printing
  file_path
end
read() click to toggle source
# File lib/chef/chef_fs/file_system/file_system_entry.rb, line 86
def read
  begin
    File.open(file_path, "rb") {|f| f.read}
  rescue Errno::ENOENT
    raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
  end
end
write(content) click to toggle source
# File lib/chef/chef_fs/file_system/file_system_entry.rb, line 94
def write(content)
  File.open(file_path, 'wb') do |file|
    file.write(content)
  end
end

Protected Instance Methods

make_child(child_name) click to toggle source
# File lib/chef/chef_fs/file_system/file_system_entry.rb, line 102
def make_child(child_name)
  FileSystemEntry.new(child_name, self)
end