Module: ProjectStore::Editing

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

Constant Summary collapse

EDITOR_ENVIRONMENT_VARIABLE =
'DM_EDITOR'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#editorObject



12
13
14
# File 'lib/project_store/editing.rb', line 12

def editor
  @editor ||= ENV[EDITOR_ENVIRONMENT_VARIABLE]
end

Instance Method Details

#edit(file_or_entity, &block) ⇒ Object



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
    # 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