Sha256: 001b86ed2781e0fcc9c5efc23dc18ad9a12c22f9be2d8272d781bec420e1b4a4
Contents?: true
Size: 1.74 KB
Versions: 6
Compression:
Stored size: 1.74 KB
Contents
require 'tempfile' require 'fileutils' module ProjectStore module Editing EDITOR_ENVIRONMENT_VARIABLE = 'DM_EDITOR' attr_writer :editor def editor @editor ||= ENV[EDITOR_ENVIRONMENT_VARIABLE] end 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 # begin 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 # rescue => e # logger.debug "#{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}" # raise PSE, 'Invalid modifications. Aborted !' # end ensure File.unlink tmp_file end end private def edit_file(file) raise PSE, 'No editor specified' if editor.nil? logger.debug "Editing file '#{file}', using editor '#{editor}'" if block_given? yield editor, file else system "#{editor} '#{file}'" end end end end
Version data entries
6 entries across 6 versions & 1 rubygems