Class: Ruber::DocumentProject::Backend
- Inherits:
-
YamlSettingsBackend
- Object
- YamlSettingsBackend
- Ruber::DocumentProject::Backend
- 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
-
#document_path=(value) ⇒ Object
Changes the project name and the file name so that they match a document path of value.
-
#initialize(file) ⇒ Backend
constructor
Creates a new DocumentProject::Backend.
-
#write(opts) ⇒ Object
Works mostly as
YamlSettingsBackend#write
.
Methods inherited from YamlSettingsBackend
Constructor Details
#initialize(file) ⇒ Backend
Creates a new DocumentProject::Backend. file is the path of the file associated with the backend.
If file is an invalid project file, the behaviour will be the same as the file didn’t exist. This means that it will be overwritten when the project is saved. The reason for this behaviour is that there should be no user file in the directory where document projects are saved.
57 58 59 60 61 62 |
# File 'lib/ruber/document_project.rb', line 57 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
93 94 95 96 97 98 |
# File 'lib/ruber/document_project.rb', line 93 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
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ruber/document_project.rb', line 73 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 |