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?, #exists?, #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)


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

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

#chef_objectObject



46
47
48
49
50
51
52
53
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb', line 46

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

#childrenObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb', line 73

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



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

def data_handler
  @data_handler || parent.data_handler
end

#minimize(file_contents, entry) ⇒ Object



66
67
68
69
70
71
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb', line 66

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

#write(file_contents) ⇒ Object



59
60
61
62
63
64
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb', line 59

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



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

def write_pretty_json
  @write_pretty_json.nil? ? root.write_pretty_json : @write_pretty_json
end

#write_pretty_json=(value) ⇒ Object



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

def write_pretty_json=(value)
  @write_pretty_json = value
end