Module: ProjectStore::Editing

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

Constant Summary collapse

EDITOR_ENVIRONMENT_VARIABLE =
'DM_EDITOR'
EDIT_RETRY_ENVIRONMENT_VARIABLE =
'DM_EDIT_RETRY'
DEFAULT_RETRY_MAX_COUNT =
1
ERROR_FILE_KEPT =
'/tmp/last-failed-edit.yaml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#editorObject



15
16
17
# File 'lib/project_store/editing.rb', line 15

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

#nb_max_edit_retriesObject



19
20
21
22
23
# File 'lib/project_store/editing.rb', line 19

def nb_max_edit_retries
  default = ENV[EDIT_RETRY_ENVIRONMENT_VARIABLE].to_i unless ENV[EDIT_RETRY_ENVIRONMENT_VARIABLE].nil?
  default ||= DEFAULT_RETRY_MAX_COUNT
  @nb_max_edit_retries ||= default
end

Instance Method Details

#edit(file_or_entity, &block) ⇒ Object



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/project_store/editing.rb', line 25

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
    retry_count ||= 1
    FileUtils.copy file, tmp_file if retry_count == 1
    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
  rescue StandardError => e
    retry_count += 1
    retry unless retry_count > nb_max_edit_retries
    raise e
  ensure
    if retry_count > nb_max_edit_retries
      logger.error "Keeping file '#{ERROR_FILE_KEPT}' which was invalid !"
      FileUtils.copy tmp_file, ERROR_FILE_KEPT
    end
    File.unlink tmp_file
  end

end