Class: Chef::ChefFS::FileSystem::Repository::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/chef_fs/file_system/repository/directory.rb

Direct Known Subclasses

CookbooksDir, DataBag, DataBagsDir

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pathObject (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

#nameObject (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

#parentObject (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

#pathObject (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

Returns:

  • (Boolean)


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

#childrenObject



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

Returns:

  • (Boolean)


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

def dir?
  true
end

#dir_lsObject



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

Returns:

  • (Boolean)


79
80
81
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 79

def empty?
  children.empty?
end

#exists?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


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

Returns:

  • (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_printingObject



62
63
64
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 62

def path_for_printing
  file_path
end

#rootObject



93
94
95
# File 'lib/chef/chef_fs/file_system/repository/directory.rb', line 93

def root
  parent.root
end