Class: Chef::ChefFS::FileSystem::BaseFSObject
- Inherits:
-
Object
- Object
- Chef::ChefFS::FileSystem::BaseFSObject
- Defined in:
- lib/chef/chef_fs/file_system/base_fs_object.rb
Direct Known Subclasses
BaseFSDir, ChefServer::CookbookFile, ChefServer::RestListEntry, Memory::MemoryFile, NonexistentFSObject
Instance Attribute Summary collapse
-
#name ⇒ Object
(also: #display_name, #bare_name)
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#path ⇒ Object
(also: #display_path)
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#can_have_child?(name, is_dir) ⇒ Boolean
Override can_have_child? to report whether a given file could be added to this directory.
-
#chef_object ⇒ Object
Expand this entry into a chef object (Chef::Role, ::Node, etc.).
-
#child(name) ⇒ Object
Get a child of this entry with the given name.
-
#children ⇒ Object
Override children to report your actual list of children as an array.
-
#compare_to(other) ⇒ Object
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.
-
#create_child(name, file_contents) ⇒ Object
Create a child of this entry with the given name and contents.
-
#delete(recurse) ⇒ Object
Delete this item, possibly recursively.
-
#dir? ⇒ Boolean
Ask whether this entry is a directory.
-
#exists? ⇒ Boolean
Ask whether this entry exists.
-
#initialize(name, parent) ⇒ BaseFSObject
constructor
A new instance of BaseFSObject.
-
#path_for_printing ⇒ Object
Printable path, generally used to distinguish paths in one root from paths in another.
-
#read ⇒ Object
Read the contents of this file entry.
- #root ⇒ Object
-
#write(file_contents) ⇒ Object
Write the contents of this file entry.
Constructor Details
#initialize(name, parent) ⇒ BaseFSObject
Returns a new instance of BaseFSObject.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 26 def initialize(name, parent) @parent = parent @name = name if parent @path = Chef::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 |
Instance Attribute Details
#name ⇒ Object (readonly) Also known as: display_name, bare_name
Returns the value of attribute name.
40 41 42 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 40 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
41 42 43 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 41 def parent @parent end |
#path ⇒ Object (readonly) Also known as: display_path
Returns the value of attribute path.
42 43 44 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 42 def path @path end |
Instance Method Details
#can_have_child?(name, is_dir) ⇒ Boolean
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.)
88 89 90 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 88 def can_have_child?(name, is_dir) false end |
#chef_object ⇒ Object
Expand this entry into a chef object (Chef::Role, ::Node, etc.)
117 118 119 120 121 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 117 def chef_object raise NotFoundError.new(self) unless exists? nil end |
#child(name) ⇒ Object
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
102 103 104 105 106 107 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 102 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 |
#children ⇒ Object
Override children to report your actual list of children as an array.
110 111 112 113 114 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 110 def children raise NotFoundError.new(self) unless exists? [] end |
#compare_to(other) ⇒ Object
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 be +true+, +false+ or +nil+ (which means "don't know"). +value+ and +other_value+ must either be the text of +self+ or +other+, +:none+ (if the entry does not exist or has no value) or +nil+ 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
81 82 83 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 81 def compare_to(other) nil end |
#create_child(name, file_contents) ⇒ Object
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.
129 130 131 132 133 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 129 def create_child(name, file_contents) raise NotFoundError.new(self) unless exists? raise OperationNotAllowedError.new(:create_child, self) end |
#delete(recurse) ⇒ Object
Delete this item, possibly recursively. Entries MUST NOT delete a directory unless recurse is true.
137 138 139 140 141 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 137 def delete(recurse) raise NotFoundError.new(self) unless exists? raise OperationNotAllowedError.new(:delete, self) end |
#dir? ⇒ Boolean
Ask whether this entry is a directory. If not, it is a file.
144 145 146 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 144 def dir? false end |
#exists? ⇒ Boolean
Ask whether this entry exists.
149 150 151 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 149 def exists? true end |
#path_for_printing ⇒ Object
Printable path, generally used to distinguish paths in one root from paths in another.
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 155 def path_for_printing if parent parent_path = parent.path_for_printing if parent_path == "." name else Chef::ChefFS::PathUtils.join(parent.path_for_printing, name) end else name end end |
#read ⇒ Object
Read the contents of this file entry.
173 174 175 176 177 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 173 def read raise NotFoundError.new(self) unless exists? raise OperationNotAllowedError.new(:read, self) end |
#root ⇒ Object
168 169 170 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 168 def root parent ? parent.root : self end |
#write(file_contents) ⇒ Object
Write the contents of this file entry.
180 181 182 183 184 |
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 180 def write(file_contents) raise NotFoundError.new(self) unless exists? raise OperationNotAllowedError.new(:write, self) end |