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)

63
64
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
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 63

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


152
153
154
155
156
157
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 152

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
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 54

def children
  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

#compare_to(other) ⇒ Object


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

def compare_to(other)
  nil
end

#create_child(child_name, file_contents = nil) ⇒ Object


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

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


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

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)

116
117
118
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 116

def dir?
  File.directory?(file_path)
end

#exists?Boolean

Returns:

  • (Boolean)

136
137
138
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 136

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

#path_for_printingObject


95
96
97
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 95

def path_for_printing
  file_path
end

#readObject


140
141
142
143
144
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 140

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

#rootObject


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

def root
  parent.root
end

#write(content) ⇒ Object


146
147
148
149
150
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 146

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

#write_pretty_jsonObject


91
92
93
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb', line 91

def write_pretty_json
  false
end