Class: Chef::ChefFS::FileSystem::Repository::BaseFile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent) ⇒ BaseFile

Returns a new instance of BaseFile.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

Instance Attribute Details

#data_handlerObject (readonly)

Returns the value of attribute data_handler.



32
33
34
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 32

def data_handler
  @data_handler
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



31
32
33
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 31

def file_path
  @file_path
end

#nameObject (readonly) Also known as: display_name

Returns the value of attribute name.



28
29
30
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 28

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



29
30
31
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 29

def parent
  @parent
end

#pathObject (readonly) Also known as: display_path

Returns the value of attribute path.



30
31
32
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 30

def path
  @path
end

#write_pretty_jsonObject



95
96
97
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 95

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

Instance Method Details

#bare_nameObject

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



62
63
64
# 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) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# 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) ⇒ Object



148
149
150
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 148

def compare_to(other)
  nil
end

#create(file_contents) ⇒ Object



82
83
84
85
86
87
88
# 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(_) ⇒ Object



103
104
105
106
107
108
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 103

def delete(_)
  FileSystemCache.instance.delete!(file_path)
  File.delete(file_path)
rescue Errno::ENOENT
  raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
end

#dir?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 57

def dir?
  false
end

#exists?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 110

def exists?
  File.file?(file_path)
end

#fs_entry_valid?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 78

def fs_entry_valid?
  name_valid? && exists?
end

#is_json_file?Boolean

Returns:

  • (Boolean)


66
67
68
# 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?Boolean

Returns:

  • (Boolean)


70
71
72
# 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) ⇒ Object



114
115
116
117
118
119
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 114

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?Boolean

Returns:

  • (Boolean)


74
75
76
# 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_printingObject



99
100
101
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 99

def path_for_printing
  file_path
end

#readObject



121
122
123
124
125
126
127
128
129
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 121

def read
  if is_ruby_file?
    data_handler.from_ruby(file_path).to_json
  else
    File.open(file_path, "rb", &:read)
  end
rescue Errno::ENOENT
  raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
end

#rootObject



144
145
146
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 144

def root
  parent.root
end

#write(content) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 131

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