Class: Chef::ChefFS::FileSystem::ChefRepositoryFileSystemEntry

Inherits:
FileSystemEntry show all
Defined in:
lib/chef/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

Methods inherited from BaseFSDir

#child, #dir?

Methods inherited from BaseFSObject

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

Constructor Details

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

Returns a new instance of ChefRepositoryFileSystemEntry.



29
30
31
32
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb', line 29

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)


51
52
53
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb', line 51

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

#chef_objectObject



42
43
44
45
46
47
48
49
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb', line 42

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



69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb', line 69

def children
  # Except cookbooks and data bag dirs, all things must be json files
  begin
    Dir.entries(file_path).sort.
        select { |child_name| can_have_child?(child_name, File.directory?(File.join(file_path, child_name))) }.
        map { |child_name| make_child(child_name) }
  rescue Errno::ENOENT
    raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
  end
end

#data_handlerObject



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

def data_handler
  @data_handler || parent.data_handler
end

#minimize(file_contents, entry) ⇒ Object



62
63
64
65
66
67
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb', line 62

def minimize(file_contents, entry)
  object = JSONCompat.from_json(file_contents, :create_additions => false)
  object = data_handler.normalize(object, entry)
  object = data_handler.minimize(object, entry)
  JSONCompat.to_json_pretty(object)
end

#write(file_contents) ⇒ Object



55
56
57
58
59
60
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb', line 55

def write(file_contents)
  if file_contents && write_pretty_json && name[-5..-1] == '.json'
    file_contents = minimize(file_contents, self)
  end
  super(file_contents)
end

#write_pretty_jsonObject



34
35
36
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb', line 34

def write_pretty_json
  root.write_pretty_json
end