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?, #empty?
#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_path ⇒ Object
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
#children ⇒ Object
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 42
def children
begin
Dir.entries(file_path).sort.
map { |child_name| make_child_entry(child_name) }.
select { |child| child && can_have_child?(child.name, child.dir?) }
rescue Errno::ENOENT
raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
end
end
|
#create_child(child_name, file_contents = nil) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 53
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) ⇒ Object
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 74
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
70
71
72
|
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 70
def dir?
File.directory?(file_path)
end
|
#exists? ⇒ Boolean
85
86
87
|
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 85
def exists?
File.exists?(file_path) && parent.can_have_child?(name, dir?)
end
|
#path_for_printing ⇒ Object
38
39
40
|
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 38
def path_for_printing
file_path
end
|
#read ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 89
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
97
98
99
100
101
|
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 97
def write(content)
File.open(file_path, 'wb') do |file|
file.write(content)
end
end
|