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

Constructor Details

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

Returns a new instance of FileSystemEntry.



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

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.



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

def file_path
  @file_path
end

Instance Method Details

#childrenObject



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

def children
  begin
    @children ||= Dir.entries(file_path).select { |entry| entry != '.' && entry != '..' }.map { |entry| FileSystemEntry.new(entry, self) }
  rescue Errno::ENOENT
    raise Chef::ChefFS::FileSystem::NotFoundError.new($!), "#{file_path} not found"
  end
end

#create_child(child_name, file_contents = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 48

def create_child(child_name, file_contents=nil)
  result = FileSystemEntry.new(child_name, self)
  if file_contents
    result.write(file_contents)
  else
    Dir.mkdir(result.file_path)
  end
  result
end

#delete(recurse) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 62

def delete(recurse)
  if dir?
    if recurse
      FileUtils.rm_rf(file_path)
    else
      File.rmdir(file_path)
    end
  else
    File.delete(file_path)
  end
end

#dir?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 58

def dir?
  File.directory?(file_path)
end

#path_for_printingObject



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

def path_for_printing
  Chef::ChefFS::PathUtils::relative_to(file_path, File.expand_path(Dir.pwd))
end

#readObject



74
75
76
77
78
79
80
# File 'lib/chef/chef_fs/file_system/file_system_entry.rb', line 74

def read
  begin
    File.open(file_path, "rb") {|f| f.read}
  rescue Errno::ENOENT
    raise Chef::ChefFS::FileSystem::NotFoundError.new($!), "#{file_path} not found"
  end
end

#write(content) ⇒ Object



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

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