Class: PiggyOptionsPersistence
- Inherits:
-
Object
- Object
- PiggyOptionsPersistence
- Defined in:
- lib/piggy-core/options_persistence.rb
Overview
Store PiggyOptions into (or load from) configuration files. File format will be Yaml. File location will be system specific:
"#{WinShell.instance.path_for_special_folder("LOCALAPPDATA")}/piggy"
Instance Method Summary collapse
-
#initialize ⇒ PiggyOptionsPersistence
constructor
A new instance of PiggyOptionsPersistence.
- #load_yaml ⇒ Object
- #save_yaml(options) ⇒ Object
Constructor Details
#initialize ⇒ PiggyOptionsPersistence
Returns a new instance of PiggyOptionsPersistence.
14 15 16 17 18 19 20 21 |
# File 'lib/piggy-core/options_persistence.rb', line 14 def initialize super shell = WinShell.instance local_app_data_path = shell.path_for_special_folder("LOCALAPPDATA") piggy_app_data_path = local_app_data_path + "#{File::SEPARATOR}" + 'piggy' Dir.mkdir(piggy_app_data_path) unless File.directory?(piggy_app_data_path) @optionsFile = piggy_app_data_path + "#{File::SEPARATOR}" + 'options.yaml' end |
Instance Method Details
#load_yaml ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/piggy-core/options_persistence.rb', line 23 def load_yaml return PiggyOptions.new unless File.exists?(@optionsFile) begin config = File.open(@optionsFile) { |io| YAML.load(io) } if config.class == Array = config.detect{ |entry| entry.class == PiggyOptions } else = config end .initialize_nils_with_default rescue StandardError => ex puts('Configuration error, options.yaml ignored: ' + ex.) = PiggyOptions.new end return end |
#save_yaml(options) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/piggy-core/options_persistence.rb', line 40 def save_yaml() begin if File.exists?(@optionsFile) File.rename(@optionsFile, "#{@optionsFile}.bak") end File.open(@optionsFile, 'w') { |out| YAML.dump(, out) } puts "Options saved to #{@optionsFile}" rescue StandardError => ex puts "Options could not be saved to #{@optionsFile}: " + ex. end end |