Class: Purse::Settings
- Inherits:
-
Object
- Object
- Purse::Settings
- Defined in:
- lib/purse/settings.rb
Class Method Summary collapse
- .default_path ⇒ Object
- .get(key) ⇒ Object
- .load ⇒ Object
- .loaded? ⇒ Boolean
- .path ⇒ Object
- .path=(new_path) ⇒ Object
- .save ⇒ Object
- .set(key, value, should_save = true) ⇒ Object
- .settings ⇒ Object
- .settings=(new_settings) ⇒ Object
Class Method Details
.default_path ⇒ Object
5 6 7 |
# File 'lib/purse/settings.rb', line 5 def default_path File.join(File.('~'), '.purse', 'settings.yml') end |
.get(key) ⇒ Object
34 35 36 37 |
# File 'lib/purse/settings.rb', line 34 def get(key) load unless loaded? settings[key.to_s] end |
.load ⇒ Object
19 20 21 22 23 |
# File 'lib/purse/settings.rb', line 19 def load save unless File.readable?(path) @settings = YAML.load_file(path) @loaded = true end |
.loaded? ⇒ Boolean
25 26 27 |
# File 'lib/purse/settings.rb', line 25 def loaded? @loaded ||= false end |
.path ⇒ Object
9 10 11 |
# File 'lib/purse/settings.rb', line 9 def path @path ||= default_path end |
.path=(new_path) ⇒ Object
13 14 15 16 17 |
# File 'lib/purse/settings.rb', line 13 def path=(new_path) raise MissingParameter unless new_path.is_a?(String) @path = new_path @loaded = false end |
.save ⇒ Object
29 30 31 32 |
# File 'lib/purse/settings.rb', line 29 def save FileUtils.mkdir_p(File.dirname(path)) File.open(path, 'w') {|f| f << YAML::dump(settings) } end |
.set(key, value, should_save = true) ⇒ Object
39 40 41 42 |
# File 'lib/purse/settings.rb', line 39 def set(key, value, should_save = true) settings[key.to_s] = value save if should_save end |
.settings ⇒ Object
44 45 46 |
# File 'lib/purse/settings.rb', line 44 def settings @settings ||= {} end |
.settings=(new_settings) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/purse/settings.rb', line 48 def settings=(new_settings) return unless new_settings.is_a?(Hash) new_settings.each do |k, v| set(k, v, false) end save end |