class ChefFS::FileSystem::BaseFSObject
Attributes
Public Class Methods
# File lib/chef_fs/file_system/base_fs_object.rb, line 25 def initialize(name, parent) @parent = parent @name = name if parent @path = ChefFS::PathUtils::join(parent.path, name) else if name != '' raise ArgumentError, "Name of root object must be empty string: was '#{name}' instead" end @path = '/' end end
Public Instance Methods
Override can_have_child? to report whether a given file could be added to this directory. (Some directories can't have subdirs, some can only have .json files, etc.)
# File lib/chef_fs/file_system/base_fs_object.rb, line 82 def can_have_child?(name, is_dir) false end
Expand this entry into a chef object (Chef::Role, ::Node, etc.)
# File lib/chef_fs/file_system/base_fs_object.rb, line 107 def chef_object raise NotFoundError.new(self) if !exists? nil end
Get a child of this entry with the given name. This MUST always return a child, even if it is NonexistentFSObject
. Overriders should take caution not to do expensive network requests to get the list of children to fulfill this request, unless absolutely necessary here; it is intended as a quick way to traverse a hierarchy.
For example, knife show /data_bags/x/y.json will call root.child('data_bags').child('x').child('y.json'), which can then directly perform a network request to retrieve the y.json data bag. No network request was necessary to retrieve
# File lib/chef_fs/file_system/base_fs_object.rb, line 96 def child(name) NonexistentFSObject.new(name, self) end
Override children to report your actual list of children as an array.
# File lib/chef_fs/file_system/base_fs_object.rb, line 101 def children raise NotFoundError.new(self) if !exists? [] end
Override this if you have a special comparison algorithm that can tell you whether this entry is the same as another–either a quicker or a more reliable one. Callers will use this to decide whether to upload, download or diff an object.
You should not override this if you're going to do the standard +self.read == other.read+. If you return nil
, the caller will call +other.compare_to(you)+ instead. Give them a chance :)
Parameters¶ ↑
-
other
- the entry to compare to
Returns¶ ↑
-
+[ are_same, value, other_value ]+
are_same
may betrue
,false
ornil
(which means “don't know”).value
andother_value
must either be the text ofself
orother
,:none
(if the entry does not exist or has no value) ornil
if the value was not retrieved. -
nil
if a definitive answer cannot be had and nothing was retrieved.
Example¶ ↑
are_same, value, other_value = entry.compare_to(other) if are_same.nil? are_same, other_value, value = other.compare_to(entry) end if are_same.nil? value = entry.read if value.nil? other_value = entry.read if other_value.nil? are_same = (value == other_value) end
# File lib/chef_fs/file_system/base_fs_object.rb, line 75 def compare_to(other) nil end
Create a child of this entry with the given name and contents. If contents is nil, create a directory.
NOTE: create_child_from is an optional method that can also be added to your entry class, and will be called without actually reading the file_contents. This is used for knife upload /cookbooks/cookbookname.
# File lib/chef_fs/file_system/base_fs_object.rb, line 118 def create_child(name, file_contents) raise NotFoundError.new(self) if !exists? raise OperationNotAllowedError.new(:create_child, self) end
Delete this item, possibly recursively. Entries MUST NOT delete a directory unless recurse is true.
# File lib/chef_fs/file_system/base_fs_object.rb, line 125 def delete(recurse) raise NotFoundError.new(self) if !exists? raise OperationNotAllowedError.new(:delete, self) end
Ask whether this entry is a directory. If not, it is a file.
# File lib/chef_fs/file_system/base_fs_object.rb, line 131 def dir? false end
Ask whether this entry exists.
# File lib/chef_fs/file_system/base_fs_object.rb, line 136 def exists? true end
Printable path, generally used to distinguish paths in one root from paths in another.
# File lib/chef_fs/file_system/base_fs_object.rb, line 142 def path_for_printing if parent parent_path = parent.path_for_printing if parent_path == '.' name else ChefFS::PathUtils::join(parent.path_for_printing, name) end else name end end
Read the contents of this file entry.
# File lib/chef_fs/file_system/base_fs_object.rb, line 160 def read raise NotFoundError.new(self) if !exists? raise OperationNotAllowedError.new(:read, self) end
# File lib/chef_fs/file_system/base_fs_object.rb, line 155 def root parent ? parent.root : self end
Write the contents of this file entry.
# File lib/chef_fs/file_system/base_fs_object.rb, line 166 def write(file_contents) raise NotFoundError.new(self) if !exists? raise OperationNotAllowedError.new(:write, self) end