Class: ChefFS::FileSystem::ChefRepositoryFileSystemEntry

Inherits:
FileSystemEntry show all
Defined in:
lib/chef_fs/file_system/chef_repository_file_system_entry.rb

Overview

ChefRepositoryFileSystemEntry works just like FileSystemEntry, except can inflate Chef objects

Instance Attribute Summary

Attributes inherited from FileSystemEntry

#file_path

Attributes inherited from BaseFSObject

#name, #parent, #path

Instance Method Summary collapse

Methods inherited from FileSystemEntry

#create_child, #delete, #dir?, #path_for_printing, #read, #write

Methods inherited from BaseFSDir

#child, #dir?

Methods inherited from BaseFSObject

#child, #compare_to, #create_child, #delete, #dir?, #exists?, #path_for_printing, #read, #root, #write

Constructor Details

#initialize(name, parent, file_path = nil, data_handler = nil) ⇒ ChefRepositoryFileSystemEntry

Returns a new instance of ChefRepositoryFileSystemEntry.



27
28
29
30
# File 'lib/chef_fs/file_system/chef_repository_file_system_entry.rb', line 27

def initialize(name, parent, file_path = nil, data_handler = nil)
  super(name, parent, file_path)
  @data_handler = data_handler
end

Instance Method Details

#can_have_child?(name, is_dir) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/chef_fs/file_system/chef_repository_file_system_entry.rb', line 45

def can_have_child?(name, is_dir)
  !is_dir && name[-5..-1] == '.json'
end

#chef_objectObject



36
37
38
39
40
41
42
43
# File 'lib/chef_fs/file_system/chef_repository_file_system_entry.rb', line 36

def chef_object
  begin
    return data_handler.chef_object(JSON.parse(read, :create_additions => false))
  rescue
    Chef::Log.error("Could not read #{path_for_printing} into a Chef object: #{$!}")
  end
  nil
end

#childrenObject



49
50
51
52
53
54
# File 'lib/chef_fs/file_system/chef_repository_file_system_entry.rb', line 49

def children
  # Except cookbooks and data bag dirs, all things must be json files
  Dir.entries(file_path).sort.
      select { |child_name| can_have_child?(child_name, File.directory?(File.join(file_path, child_name))) }.
      map { |child_name| ChefRepositoryFileSystemEntry.new(child_name, self) }
end

#data_handlerObject



32
33
34
# File 'lib/chef_fs/file_system/chef_repository_file_system_entry.rb', line 32

def data_handler
  @data_handler || parent.data_handler
end