Class: Chef::ChefFS::FileSystem::Repository::Directory
- Inherits:
-
Object
- Object
- Chef::ChefFS::FileSystem::Repository::Directory
- Defined in:
- lib/chef/chef_fs/file_system/repository/directory.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#can_have_child?(name, is_dir) ⇒ Boolean
Public API called by multiplexed_dir.
-
#child(name) ⇒ Object
Public API callied by chef_fs/file_system.
- #children ⇒ Object
-
#create(file_contents = nil) ⇒ Object
File system wrappers.
- #create_child(child_name, file_contents = nil) ⇒ Object
- #delete(recurse) ⇒ Object
-
#dir? ⇒ Boolean
Public API callied by chef_fs/file_system.
- #dir_ls ⇒ Object
-
#empty? ⇒ Boolean
An empty children array is an empty dir.
- #exists? ⇒ Boolean
-
#fs_entry_valid? ⇒ Boolean
Whether or not the file system entry this object represents is valid.
-
#initialize(name, parent, file_path = nil) ⇒ Directory
constructor
A new instance of Directory.
- #name_valid? ⇒ Boolean
- #path_for_printing ⇒ Object
- #root ⇒ Object
Constructor Details
#initialize(name, parent, file_path = nil) ⇒ Directory
Returns a new instance of Directory.
31 32 33 34 35 36 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 31 def initialize(name, parent, file_path = nil) @parent = parent @name = name @path = Chef::ChefFS::PathUtils.join(parent.path, name) @file_path = file_path || "#{parent.file_path}/#{name}" end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
29 30 31 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 29 def file_path @file_path end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 26 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
27 28 29 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 27 def parent @parent end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
28 29 30 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 28 def path @path end |
Instance Method Details
#can_have_child?(name, is_dir) ⇒ Boolean
Public API called by multiplexed_dir
52 53 54 55 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 52 def can_have_child?(name, is_dir) possible_child = make_child_entry(name) possible_child.dir? == is_dir && possible_child.name_valid? end |
#child(name) ⇒ Object
Public API callied by chef_fs/file_system
84 85 86 87 88 89 90 91 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 84 def child(name) possible_child = make_child_entry(name) if possible_child.name_valid? possible_child else NonexistentFSObject.new(name, self) end end |
#children ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 66 def children dir_ls.sort. map { |child_name| make_child_entry(child_name) }. select { |maybe_child| maybe_child.fs_entry_valid? } rescue Errno::ENOENT => e raise Chef::ChefFS::FileSystem::NotFoundError.new(self, e) end |
#create(file_contents = nil) ⇒ Object
File system wrappers
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 99 def create(file_contents = nil) if exists? raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, self) end begin Dir.mkdir(file_path) rescue Errno::EEXIST raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, self) end end |
#create_child(child_name, file_contents = nil) ⇒ Object
74 75 76 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 74 def create_child(child_name, file_contents = nil) make_child_entry(child_name).tap { |c| c.create(file_contents) } end |
#delete(recurse) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 114 def delete(recurse) if exists? if !recurse raise MustDeleteRecursivelyError.new(self, $!) end FileUtils.rm_r(file_path) else raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) end end |
#dir? ⇒ Boolean
Public API callied by chef_fs/file_system
58 59 60 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 58 def dir? true end |
#dir_ls ⇒ Object
110 111 112 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 110 def dir_ls Dir.entries(file_path).select { |p| !p.start_with?(".") } end |
#empty? ⇒ Boolean
An empty children array is an empty dir
79 80 81 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 79 def empty? children.empty? end |
#exists? ⇒ Boolean
125 126 127 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 125 def exists? File.exists?(file_path) end |
#fs_entry_valid? ⇒ Boolean
Whether or not the file system entry this object represents is valid. Mainly used to trim dotfiles/dotdirs and non directories from the list of children when enumerating items on the filesystem
45 46 47 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 45 def fs_entry_valid? name_valid? && File.directory?(file_path) end |
#name_valid? ⇒ Boolean
38 39 40 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 38 def name_valid? !name.start_with?(".") end |
#path_for_printing ⇒ Object
62 63 64 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 62 def path_for_printing file_path end |
#root ⇒ Object
93 94 95 |
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 93 def root parent.root end |