Class: Chef::ChefFS::FileSystem::BaseFSObject

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent) ⇒ BaseFSObject

Returns a new instance of BaseFSObject.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 25

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

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



39
40
41
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 39

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



40
41
42
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 40

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.)

Returns:

  • (Boolean)


74
75
76
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 74

def can_have_child?(name, is_dir)
  false
end

#child(name) ⇒ Object



67
68
69
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 67

def child(name)
  NonexistentFSObject.new(name, self)
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


111
112
113
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 111

def compare_to(other)
  return nil
end

#dir?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 59

def dir?
  false
end

#exists?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 63

def exists?
  true
end

#path_for_printingObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 46

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

#rootObject



42
43
44
# File 'lib/chef/chef_fs/file_system/base_fs_object.rb', line 42

def root
  parent ? parent.root : self
end