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
90
# 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..] != ".rb"
  end

  # Check chefignore
  ignorer = self

  loop do
    if ignorer.is_a?(ChefRepositoryFileSystemCookbookDir)
      # Grab the path from entry to child
      path_to_child = name
      child = self
      while child != 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



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

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



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

def compare_to(other)
  nil
end

#create_child(child_name, file_contents = nil) ⇒ Object



100
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 100

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
137
# 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?
      unless 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)


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

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

#path_for_printingObject



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

def path_for_printing
  file_path
end

#readObject



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

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

#rootObject



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

def root
  parent.root
end

#write(content) ⇒ Object



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

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

#write_pretty_jsonObject



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

def write_pretty_json
  false
end