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)
-
- (Object) document_path(value)
Changes the project name and the file name so that they match a document path of value.
-
- (Object) file_for(path)
private
Returns the file where the data for the document path path should be stored (an empty string if path is empty).
-
- (Backend) initialize(file)
constructor
Creates a new DocumentProject::Backend.
-
- (Object) write(opts)
Works mostly as YamlSettingsBackend#write.
Methods inherited from YamlSettingsBackend
Constructor Details
- (Backend) initialize(file)
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.
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
- (Object) document_path=(value)
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 |
- (Object) file_for(path) (private)
Returns the file where the data for the document path path should be stored (an empty string if path is empty).
107 108 109 110 111 112 113 |
# File 'lib/ruber/document_project.rb', line 107 def file_for path return '' if path.empty? dir = KDE::Global.dirs.locate_local('appdata', 'documents/') md5 = Digest::MD5.new md5 << path File.join dir, md5.hexdigest end |
- (Object) write(opts)
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 |