Class: Purse::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/purse/settings.rb

Class Method Summary collapse

Class Method Details

.default_pathObject



5
6
7
# File 'lib/purse/settings.rb', line 5

def default_path
  File.join(File.expand_path('~'), '.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

.loadObject



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

Returns:

  • (Boolean)


25
26
27
# File 'lib/purse/settings.rb', line 25

def loaded?
  @loaded ||= false
end

.pathObject



9
10
11
# File 'lib/purse/settings.rb', line 9

def path
  @path ||= default_path
end

.path=(new_path) ⇒ Object

Raises:



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

.saveObject



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

.settingsObject



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