class Chef::ChefFS::FileSystem::Repository::BaseFile

Attributes

data_handler[R]
display_name[R]
display_path[R]
file_path[R]
name[R]
parent[R]
path[R]
write_pretty_json[W]

Public Class Methods

new(name, parent) click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 37
def initialize(name, parent)
  @parent = parent

  if %w{ .rb .json }.include? File.extname(name)
    name = File.basename(name, ".*")
  end

  file_path = "#{parent.file_path}/#{name}"

  Chef::Log.trace "BaseFile: Detecting file extension for #{name}"
  ext = File.exist?(file_path + ".rb") ? ".rb" : ".json"
  name += ext
  file_path += ext

  Chef::Log.trace "BaseFile: got a file path of #{file_path} for #{name}"
  @name = name
  @path = Chef::ChefFS::PathUtils.join(parent.path, name)
  @file_path = file_path
end

Public Instance Methods

bare_name() click to toggle source

Used to compare names on disk to the API, for diffing.

# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 62
def bare_name
  File.basename(name, ".*")
end
can_have_child?(name, is_dir) click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 90
def can_have_child?(name, is_dir)
  false
end
compare_to(other) click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 149
def compare_to(other)
  nil
end
create(file_contents) click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 82
def create(file_contents)
  if exists?
    raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, self)
  else
    write(file_contents)
  end
end
delete(_) click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 104
def delete(_)
  FileSystemCache.instance.delete!(file_path)
  File.delete(file_path)
rescue Errno::ENOENT
  raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
end
dir?() click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 57
def dir?
  false
end
exists?() click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 111
def exists?
  File.file?(file_path)
end
fs_entry_valid?() click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 78
def fs_entry_valid?
  name_valid? && exists?
end
is_json_file?() click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 66
def is_json_file?
  File.extname(file_path) == ".json"
end
is_ruby_file?() click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 70
def is_ruby_file?
  File.extname(file_path) == ".rb"
end
minimize(content, entry) click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 115
def minimize(content, entry)
  object = Chef::JSONCompat.parse(content)
  object = data_handler.normalize(object, entry)
  object = data_handler.minimize(object, entry)
  Chef::JSONCompat.to_json_pretty(object)
end
name_valid?() click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 74
def name_valid?
  !name.start_with?(".") && (is_json_file? || is_ruby_file?)
end
path_for_printing() click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 100
def path_for_printing
  file_path
end
read() click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 122
def read
  if is_ruby_file?
    data_handler.from_ruby(file_path).to_json
  else
    File.binread(file_path)
  end
rescue Errno::ENOENT
  raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
end
root() click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 145
def root
  parent.root
end
write(content) click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 132
def write(content)
  if is_ruby_file?
    raise Chef::ChefFS::FileSystem::RubyFileError.new(:write, self)
  end

  if content && write_pretty_json && is_json_file?
    content = minimize(content, self)
  end
  File.open(file_path, "wb") do |file|
    file.write(content)
  end
end
write_pretty_json() click to toggle source
# File lib/chef/chef_fs/file_system/repository/base_file.rb, line 96
def write_pretty_json
  @write_pretty_json.nil? ? root.write_pretty_json : @write_pretty_json
end