Class: Chef::ChefFS::FileSystem::Repository::BaseFile
- Inherits:
-
Object
- Object
- Chef::ChefFS::FileSystem::Repository::BaseFile
show all
- Defined in:
- lib/chef/chef_fs/file_system/repository/base_file.rb
Direct Known Subclasses
Acl, Client, ClientKey, Container, DataBagItem, Environment, Group, Node, Policy, PolicyGroup, Role, User
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_handler ⇒ Object
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_path ⇒ Object
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
|
#name ⇒ Object
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
|
#parent ⇒ Object
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
|
#path ⇒ Object
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_json ⇒ Object
96
97
98
|
# 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
|
Instance Method Details
#bare_name ⇒ Object
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
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
149
150
151
|
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 149
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
104
105
106
107
108
109
|
# 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? ⇒ Boolean
57
58
59
|
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 57
def dir?
false
end
|
#exists? ⇒ Boolean
111
112
113
|
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 111
def exists?
File.file?(file_path)
end
|
#fs_entry_valid? ⇒ 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
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
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
115
116
117
118
119
120
|
# 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? ⇒ 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_printing ⇒ Object
100
101
102
|
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 100
def path_for_printing
file_path
end
|
#read ⇒ Object
122
123
124
125
126
127
128
129
130
|
# 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 ⇒ Object
145
146
147
|
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 145
def root
parent.root
end
|
#write(content) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
|
# 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
|