Class: MAndM::Preferences

Inherits:
Object
  • Object
show all
Defined in:
lib/m_and_m/preferences.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Preferences

Returns a new instance of Preferences.



4
5
6
7
8
9
10
11
# File 'lib/m_and_m/preferences.rb', line 4

def initialize name
  @preferences_path = File.join File.expand_path('~'), name
  if File.exists? preferences_path
    @hash = YAML.load_file preferences_path
  else
    @hash = {}
  end
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



2
3
4
# File 'lib/m_and_m/preferences.rb', line 2

def hash
  @hash
end

#preferences_pathObject (readonly)

Returns the value of attribute preferences_path.



2
3
4
# File 'lib/m_and_m/preferences.rb', line 2

def preferences_path
  @preferences_path
end

Instance Method Details

#[](key) ⇒ Object



21
22
23
# File 'lib/m_and_m/preferences.rb', line 21

def [] key
  hash[key]
end

#[]=(key, value) ⇒ Object



25
26
27
28
# File 'lib/m_and_m/preferences.rb', line 25

def []= key, value
  hash[key] = value
  persist
end

#ask(key, prompt) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/m_and_m/preferences.rb', line 13

def ask key, prompt
  unless hash[key]
    print "#{prompt} ? "
    self[key] = $stdin.gets.chomp
  end
  self[key]
end

#persistObject



30
31
32
# File 'lib/m_and_m/preferences.rb', line 30

def persist
  File.open(preferences_path, 'w') {|f| f.puts hash.to_yaml}
end