Class: Nin::YamlStore

Inherits:
Object
  • Object
show all
Defined in:
lib/nin/yaml_store.rb

Constant Summary collapse

DEFAULT_FILE =
"#{ENV['HOME']}/.todos.yaml"

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fileObject (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

#readObject



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