Class: Ruber::DocumentProject::Backend

Inherits:
YamlSettingsBackend show all
Defined in:
lib/ruber/document_project.rb

Overview

Backend for SettingsContainer used in particular for ProjectDocuments. It mostly works as YamlSettingsBackend, with the following differences:

  • it doesn’t create the file if the only option to be written (that is, the only one different from its default value) is the project_name. In that case, if the file already exists, it is deleted

  • it automatically determines the name of the associated file from the name of the document

Instance Method Summary collapse

Methods inherited from YamlSettingsBackend

#[], #file

Constructor Details

#initialize(file) ⇒ Backend

$CALLED = 0



58
59
60
61
62
63
# File 'lib/ruber/document_project.rb', line 58

def initialize file
  @old_files = []
  begin super file_for(file)
  rescue InvalidSettingsFile
  end
end

Instance Method Details

#document_path=(value) ⇒ Object

Changes the project name and the file name so that they match a document path of value. This means:

  • setting the project name to value

  • changing the file associated with the backend to an encoded version of value

  • adding the old associated file to a list of obsolete files, which will be deleted at the next write



94
95
96
97
98
99
# File 'lib/ruber/document_project.rb', line 94

def document_path= value
  @data[:general] ||= {}
  @data[:general][:project_name] = value
  @old_files << @filename unless @filename.empty?
  @filename = file_for(value)
end

#write(opts) ⇒ Object

Works mostly as YamlSettingsBackend#write. If the only option to be written is the project name, the file isn’t created, if it doesn’t exist, and is deleted if it does exist. Also, if there are any obsolete files (see document_path=), they are deleted, too.

If no file name is associated with the backend (that is, if file returns an empty string), a SystemCall error (most likely, Errno::ENOENT) will be raised



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruber/document_project.rb', line 74

def write opts
  new_data = compute_data opts
  if new_data.has_only_keys?(:general) and new_data[:general].has_only_keys?(:project_name)
    FileUtils.rm_f @filename
    return
  end
  File.open(@filename, 'w'){|f| f.write YAML.dump(new_data)}
  @old_files.each{|f| FileUtils.rm_f f}
  @old_files.clear
  @data = new_data
end