module Sharkey::Setting
Global in-app settings.
It saves and reads from a YAML file
Constants
- SETTING_FILE
Trying to put it on the root of the app
Public Class Methods
new()
click to toggle source
Initialize settings with default values THEN loads values from the file.
@note Must be called at the beginning of the
program!
# File lib/sharkey/setting.rb, line 33 def initialize self.reset if File.exist? SETTING_FILE # Sometimes on the _development_ environment # the settings file gets corrupted... # Well, that's a shame! begin @values = YAML::load_file SETTING_FILE rescue self.reset end # Strange error that sometimes appear # (@values becomes `false`) if not @values.class == Hash self.reset end end self.save end
Public Instance Methods
[](label)
click to toggle source
Accesses individual settings, just like a Hash.
# File lib/sharkey/setting.rb, line 64 def [] label @values[label] end
[]=(label, val)
click to toggle source
Changes individual settings, just like a Hash.
# File lib/sharkey/setting.rb, line 69 def []=(label, val) @values[label] = val end
reset()
click to toggle source
Initialize settings with default values
@note No need to call this, it’s used to guarantee
that the setting file exist.
# File lib/sharkey/setting.rb, line 19 def reset @values = { 'loading_bar' => 'true', 'date_format' => 'relative', 'theme' => 'bootstrap', 'auto_fill' => 'true' } end
save()
click to toggle source
Writes settings into the file
# File lib/sharkey/setting.rb, line 57 def save File.open(SETTING_FILE, 'w') do |file| file.write @values.to_yaml end end