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.



30
31
32
33
# File 'lib/chef/chef_fs/file_system/repository/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/repository/file_system_entry.rb', line 35

def file_path
  @file_path
end

Instance Method Details

#childrenObject



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

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



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

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



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

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)


69
70
71
# File 'lib/chef/chef_fs/file_system/repository/file_system_entry.rb', line 69

def dir?
  File.directory?(file_path)
end

#exists?Boolean

Returns:

  • (Boolean)


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

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

#path_for_printingObject



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

def path_for_printing
  file_path
end

#readObject



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

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



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

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