Module: ProjectStore::Editing

Included in:
Base
Defined in:
lib/project_store/editing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#editorObject



10
11
12
# File 'lib/project_store/editing.rb', line 10

def editor
  @editor ||= ENV['EDITOR']
end

Instance Method Details

#edit(file_or_entity) ⇒ Object



14
15
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
# File 'lib/project_store/editing.rb', line 14

def edit(file_or_entity)
  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).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_type|
          store[entity_type]
        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