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, #exists?, #root

Constructor Details

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

Returns a new instance of FileSystemEntry.



30
31
32
33
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 30

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.



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

def file_path
  @file_path
end

Instance Method Details

#childrenObject



41
42
43
44
45
46
47
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 41

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



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 49

def create_child(child_name, file_contents=nil)
  child = make_child(child_name)
  if file_contents
    child.write(file_contents)
  else
    begin
      Dir.mkdir(child.file_path)
    rescue Errno::EEXIST
    end
  end
  @children = nil
  child
end

#delete(recurse) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 67

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)


63
64
65
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 63

def dir?
  File.directory?(file_path)
end

#path_for_printingObject



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

def path_for_printing
  file_path
end

#readObject



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

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



86
87
88
89
90
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 86

def write(content)
  File.open(file_path, 'wb') do |file|
    file.write(content)
  end
end