Class: Chef::ChefFS::FileSystem::Repository::ChefRepositoryFileSystemCookbookEntry

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

Overview

NB: unlike most other things in chef_fs/file_system/repository, this class represents both files and directories, so it needs to have the methods/if branches for each.

Direct Known Subclasses

ChefRepositoryFileSystemCookbookDir

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent, file_path = nil, ruby_only = false, recursive = false) ⇒ ChefRepositoryFileSystemCookbookEntry

Returns a new instance of ChefRepositoryFileSystemCookbookEntry.



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

def initialize(name, parent, file_path = nil, ruby_only = false, recursive = false)
  @parent = parent
  @name = name
  @path = Chef::ChefFS::PathUtils.join(parent.path, name)
  @ruby_only = ruby_only
  @recursive = recursive
  @data_handler = nil
  @file_path = file_path || "#{parent.file_path}/#{name}"
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



38
39
40
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 38

def file_path
  @file_path
end

#nameObject (readonly) Also known as: display_name, bare_name

Returns the value of attribute name.



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

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#pathObject (readonly) Also known as: display_path

Returns the value of attribute path.



35
36
37
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 35

def path
  @path
end

#recursiveObject (readonly)

Returns the value of attribute recursive.



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

def recursive
  @recursive
end

#ruby_onlyObject (readonly)

Returns the value of attribute ruby_only.



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

def ruby_only
  @ruby_only
end

Instance Method Details

#can_have_child?(name, is_dir) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 65

def can_have_child?(name, is_dir)
  if is_dir
    return recursive && name != "." && name != ".."
  elsif ruby_only
    return false if name[-3..-1] != ".rb"
  end

  # Check chefignore
  ignorer = parent
  loop do
    if ignorer.is_a?(CookbooksDir)
      # Grab the path from entry to child
      path_to_child = name
      child = self
      while child.parent != ignorer
        path_to_child = PathUtils.join(child.name, path_to_child)
        child = child.parent
      end
      # Check whether that relative path is ignored
      return !ignorer.chefignore || !ignorer.chefignore.ignored?(path_to_child)
    end
    ignorer = ignorer.parent
    break unless ignorer
  end

  true
end

#child(name) ⇒ Object



156
157
158
159
160
161
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 156

def child(name)
  if can_have_child?(name, true) || can_have_child?(name, false)
    result = make_child_entry(name)
  end
  result || NonexistentFSObject.new(name, self)
end

#childrenObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 54

def children
  begin
    entries = Dir.entries(file_path).sort.
              map { |child_name| make_child_entry(child_name) }.
              select { |child| child && can_have_child?(child.name, child.dir?) }
    entries.select { |entry| !(entry.dir? && entry.children.size == 0 ) }
  rescue Errno::ENOENT
    raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
  end
end

#compare_to(other) ⇒ Object



167
168
169
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 167

def compare_to(other)
  nil
end

#create_child(child_name, file_contents = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 101

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 122

def delete(recurse)
  FileSystemCache.instance.delete!(file_path)
  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)


118
119
120
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 118

def dir?
  File.directory?(file_path)
end

#exists?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 138

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

#path_for_printingObject



97
98
99
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 97

def path_for_printing
  file_path
end

#readObject



142
143
144
145
146
147
148
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 142

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

#rootObject



163
164
165
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 163

def root
  parent.root
end

#write(content) ⇒ Object



150
151
152
153
154
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 150

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

#write_pretty_jsonObject



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

def write_pretty_json
  false
end