Class: MAndM::Preferences
- Inherits:
-
Object
- Object
- MAndM::Preferences
- Defined in:
- lib/m_and_m/preferences.rb
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#preferences_path ⇒ Object
readonly
Returns the value of attribute preferences_path.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #ask(key, prompt) ⇒ Object
-
#initialize(name) ⇒ Preferences
constructor
A new instance of Preferences.
- #persist ⇒ Object
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.('~'), name if File.exists? preferences_path @hash = YAML.load_file preferences_path else @hash = {} end end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
2 3 4 |
# File 'lib/m_and_m/preferences.rb', line 2 def hash @hash end |
#preferences_path ⇒ Object (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 |
#persist ⇒ Object
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 |