Module: Sharkey::Setting
- Defined in:
- lib/sharkey/setting.rb
Overview
Global in-app settings.
It saves and reads from a YAML file
Constant Summary collapse
- SETTING_FILE =
Trying to put it on the root of the app
File.('../../../settings.yml', __FILE__)
Class Method Summary collapse
-
.[](label) ⇒ Object
Accesses individual settings, just like a Hash.
-
.[]=(label, val) ⇒ Object
Changes individual settings, just like a Hash.
-
.initialize ⇒ Object
Initialize settings with default values THEN loads values from the file.
-
.reset ⇒ Object
Initialize settings with default values.
-
.save ⇒ Object
Writes settings into the file.
Class Method Details
.[](label) ⇒ Object
Accesses individual settings, just like a Hash.
64 65 66 |
# File 'lib/sharkey/setting.rb', line 64 def [] label @values[label] end |
.[]=(label, val) ⇒ Object
Changes individual settings, just like a Hash.
69 70 71 |
# File 'lib/sharkey/setting.rb', line 69 def []=(label, val) @values[label] = val end |
.initialize ⇒ Object
Note:
Must be called at the beginning of the program!
Initialize settings with default values THEN loads values from the file.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 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 |
.reset ⇒ Object
Note:
No need to call this, it’s used to guarantee that the setting file exist.
Initialize settings with default values
19 20 21 22 23 24 25 26 |
# File 'lib/sharkey/setting.rb', line 19 def reset @values = { 'loading_bar' => 'true', 'date_format' => 'relative', 'theme' => 'bootstrap', 'auto_fill' => 'true' } end |
.save ⇒ Object
Writes settings into the file
57 58 59 60 61 |
# File 'lib/sharkey/setting.rb', line 57 def save File.open(SETTING_FILE, 'w') do |file| file.write @values.to_yaml end end |