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.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 35

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.debug "BaseFile: Detecting file extension for #{name}"
  ext = File.exist?(file_path + ".rb") ? ".rb" : ".json"
  name += ext
  file_path += ext

  Chef::Log.debug "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.



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

def data_handler
  @data_handler
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



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

def file_path
  @file_path
end

#nameObject (readonly) Also known as: display_name

Returns the value of attribute name.



26
27
28
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 26

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#pathObject (readonly) Also known as: display_path

Returns the value of attribute path.



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

def path
  @path
end

#write_pretty_jsonObject



93
94
95
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 93

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.



60
61
62
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 60

def bare_name
  File.basename(name, ".*")
end

#can_have_child?(name, is_dir) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 88

def can_have_child?(name, is_dir)
  false
end

#compare_to(other) ⇒ Object



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

def compare_to(other)
  nil
end

#create(file_contents) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 80

def create(file_contents)
  if exists?
    raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, self)
  else
    write(file_contents)
  end
end

#delete(_) ⇒ Object



101
102
103
104
105
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 101

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

#dir?Boolean

Returns:

  • (Boolean)


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

def dir?
  false
end

#exists?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 107

def exists?
  File.file?(file_path)
end

#fs_entry_valid?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 76

def fs_entry_valid?
  name_valid? && exists?
end

#is_json_file?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 64

def is_json_file?
  File.extname(file_path) == ".json"
end

#is_ruby_file?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 68

def is_ruby_file?
  File.extname(file_path) == ".rb"
end

#minimize(content, entry) ⇒ Object



111
112
113
114
115
116
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 111

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)


72
73
74
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 72

def name_valid?
  !name.start_with?(".") && (is_json_file? || is_ruby_file?)
end

#path_for_printingObject



97
98
99
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 97

def path_for_printing
  file_path
end

#readObject



118
119
120
121
122
123
124
125
126
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 118

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

#rootObject



140
141
142
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 140

def root
  parent.root
end

#write(content) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/chef/chef_fs/file_system/repository/base_file.rb', line 128

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