Class: Financo::N26::HistoryStore

Inherits:
Object
  • Object
show all
Defined in:
lib/financo/n26/history_store.rb

Overview

HistoryStore

Instance Method Summary collapse

Constructor Details

#initialize(base_dir: File.join(Dir.pwd, '.financo')) ⇒ HistoryStore

Returns a new instance of HistoryStore.



10
11
12
13
14
# File 'lib/financo/n26/history_store.rb', line 10

def initialize(base_dir: File.join(Dir.pwd, '.financo'))
  @base_dir = base_dir

  FileUtils.mkdir_p(@base_dir, mode: 0o700)
end

Instance Method Details

#load(id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/financo/n26/history_store.rb', line 16

def load(id)
  path = filename(id)

  FileUtils.touch(path)

  File.open(path, 'r+') do |f|
    data = YAML.safe_load(f) || {}
    data = data.each_with_object({}) { |(k, v), m| m[k.to_sym] = v; }

    History.new(**data)
  end
end

#save(id, history) ⇒ Object



29
30
31
# File 'lib/financo/n26/history_store.rb', line 29

def save(id, history)
  File.open(filename(id), 'w') { |f| f.write(history.dump) }
end