Class: Chef::ChefFS::FileSystem::FileSystemEntry

Inherits:
BaseFSDir show all
Defined in:
lib/chef/chef_fs/file_system/file_system_entry.rb

Instance Attribute Summary collapse

Attributes inherited from BaseFSObject

#name, #parent, #path

Instance Method Summary collapse

Methods inherited from BaseFSDir

#can_have_child?, #child

Methods inherited from BaseFSObject

#can_have_child?, #chef_object, #child, #compare_to, #root

Constructor Details

#initialize(name, parent, file_path = nil) ⇒ FileSystemEntry

Returns a new instance of FileSystemEntry.



31
32
33
34
# 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

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



36
37
38
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 36

def file_path
  @file_path
end

Instance Method Details

#childrenObject



42
43
44
45
46
47
48
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# 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?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 67

def dir?
  File.directory?(file_path)
end

#exists?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 82

def exists?
  File.exists?(file_path)
end

#path_for_printingObject



38
39
40
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 38

def path_for_printing
  file_path
end

#readObject



86
87
88
89
90
91
92
# 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) ⇒ Object



94
95
96
97
98
# 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