16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/project_store/editing.rb', line 16
def edit(file_or_entity, &block)
file = case file_or_entity
when String
if File.exists? file_or_entity and File.readable? file_or_entity
file_or_entity
else
raise PSE, "Invalid file to edit '#{file_or_entity}'"
end
when ProjectStore::Entity::Base
file_or_entity.backing_store.path
end
tmp_file = Tempfile.new([self.class.name, '.yaml']).path
begin
FileUtils.copy file, tmp_file
edit_file tmp_file
store = YAML::Store.new(tmp_file)
store.transaction do
store.roots.each do |entity_name|
entity = store[entity_name]
setup_entity! entity_name, entity, &block
entity.valid_to_save? raise_exception: true
end
end
FileUtils.copy tmp_file, file
logger.info "File '#{file}' updated successfully."
file
ensure
File.unlink tmp_file
end
end
|