Class: Ruber::ProjectBackend
Overview
Backend for SettingsContainer used by the project class. It is similar to YamlSettingsBackend, except for the fact that it produces three files instead of one: one only contains options of type :global
(and is referred to as the project file), another one contains only options of type :user
and the last contains only options of type :session
Constant Summary collapse
- EXTENSIONS =
Associates each option type (
:global
,:user
and:session
) with the extension of the file where those options will be stored {:global => '.ruprj', :user => '.ruusr', :session => '.ruses'}
Instance Method Summary collapse
-
#[](opt) ⇒ Object
Returns the value of the option described by the option object opt (see SettingsContainer#add_option).
-
#file ⇒ Object
Returns the path of the file were the global options are stored.
-
#initialize(file) ⇒ ProjectBackend
constructor
Creates a new ProjectBackend.
-
#write(opts) ⇒ Object
Writes the options opts back to disk.
Constructor Details
#initialize(file) ⇒ ProjectBackend
Creates a new ProjectBackend. file is the name of the project file. The name of the file for the user options and the session options is obtained by appending the appropriate extension to the project file:
-
.ruusr
for the user options file -
.ruses
for the session options file
If file has the .ruprj
extension, that extension will be removed before adding the extensions above. If it has any other extension (or no extension at all), they’ll be kept.
For example, if file is /home/stefano/xyz.ruprj
, the other two files will be /home/stefano/xyz.ruusr
and /home/stefano/xyz.ruses
. Instead, if file is /home/stefano/xyz.abc
, the other two files will be /home/stefano/xyz.abc.ruusr
and /home/stefano/xyz.abc.ruses
If either the file for the global or the user options already exists but doesn’t have the correct format, YamlSettingsBackend::InvalidSettingsFile
will be raised. If the same happens for the session options file, instead, a warning will be emitted, default values will be used for the session options and they will never be saved to file.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ruber/project_backend.rb', line 61 def initialize file @backends = {} base_file = file.sub(/#{EXTENSIONS[:global]}$/, '') @backends[:global] = YamlSettingsBackend.new file @backends[:user] = YamlSettingsBackend.new base_file + EXTENSIONS[:user] @backends[:session] = begin YamlSettingsBackend.new base_file + EXTENSIONS[:session] rescue YamlSettingsBackend::InvalidSettingsFile warn "The file #{base_file + '.ruses'} already exists but it's not a valid session file. Session options won't be saved" YamlSettingsBackend.new '' end end |
Instance Method Details
#[](opt) ⇒ Object
Returns the value of the option described by the option object opt (see SettingsContainer#add_option).
The value will be retrieved from the backend corresponding to the type of the option
86 87 88 |
# File 'lib/ruber/project_backend.rb', line 86 def [] opt @backends[opt.type][opt] end |
#file ⇒ Object
Returns the path of the file were the global options are stored
76 77 78 |
# File 'lib/ruber/project_backend.rb', line 76 def file @backends[:global].file end |
#write(opts) ⇒ Object
Writes the options opts back to disk. The options contained in opts will be grouped according to their types and the appropriate backend will be used to save each group.
95 96 97 98 99 100 101 |
# File 'lib/ruber/project_backend.rb', line 95 def write opts = {:global => {}, :user => {}, :session => {}} opts.each{|k, v| [k.type][k] = v} @backends[:global].write [:global] @backends[:user].write [:user] @backends[:session].write [:session] unless @backends[:session].file.empty? end |