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

Inherits:
BaseFSDir show all
Defined in:
lib/chef/chef_fs/file_system/repository/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?

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.



32
33
34
35
# File 'lib/chef/chef_fs/file_system/repository/file_system_entry.rb', line 32

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.



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

def file_path
  @file_path
end

Instance Method Details

#childrenObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/chef/chef_fs/file_system/repository/file_system_entry.rb', line 43

def children
  # Except cookbooks and data bag dirs, all things must be json files
  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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chef/chef_fs/file_system/repository/file_system_entry.rb', line 54

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef/chef_fs/file_system/repository/file_system_entry.rb', line 75

def delete(recurse)
  begin
    if dir?
      if !recurse
        raise MustDeleteRecursivelyError.new(self, $!)
      end
      FileUtils.rm_r(file_path)
    else
      File.delete(file_path)
    end
  rescue Errno::ENOENT
    raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
  end
end

#dir?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/chef/chef_fs/file_system/repository/file_system_entry.rb', line 71

def dir?
  File.directory?(file_path)
end

#exists?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/chef/chef_fs/file_system/repository/file_system_entry.rb', line 90

def exists?
  File.exists?(file_path) && (parent.nil? || parent.can_have_child?(name, dir?))
end

#path_for_printingObject



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

def path_for_printing
  file_path
end

#readObject



94
95
96
97
98
99
100
# File 'lib/chef/chef_fs/file_system/repository/file_system_entry.rb', line 94

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



102
103
104
105
106
# File 'lib/chef/chef_fs/file_system/repository/file_system_entry.rb', line 102

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