Class: Nin::YamlStore
- Inherits:
-
Object
- Object
- Nin::YamlStore
- Defined in:
- lib/nin/yaml_store.rb
Constant Summary collapse
- DEFAULT_FILE =
"#{ENV['HOME']}/.todos.yaml"
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
-
#initialize(file = DEFAULT_FILE) ⇒ YamlStore
constructor
A new instance of YamlStore.
- #read ⇒ Object
- #write(hash) ⇒ Object
Constructor Details
#initialize(file = DEFAULT_FILE) ⇒ YamlStore
Returns a new instance of YamlStore.
7 8 9 10 11 |
# File 'lib/nin/yaml_store.rb', line 7 def initialize(file = DEFAULT_FILE) @file = file init_store end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
5 6 7 |
# File 'lib/nin/yaml_store.rb', line 5 def file @file end |
Instance Method Details
#read ⇒ Object
13 14 15 |
# File 'lib/nin/yaml_store.rb', line 13 def read Psych.load_file(@file) || {} # Psych returns false if the file is empty end |
#write(hash) ⇒ Object
17 18 19 20 21 |
# File 'lib/nin/yaml_store.rb', line 17 def write(hash) File.open(@file, 'w') do |file| file.write(Psych.dump(hash)) end end |