Class: Anschel::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/anschel/store.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, log) ⇒ Store

Returns a new instance of Store.



6
7
8
9
10
11
12
13
14
15
# File 'lib/anschel/store.rb', line 6

def initialize path, log
  @path = path || '/tmp/anschel.db'
  @hash = nil
  if File.exist? @path
    @hash = JrJackson::Json.load \
      File.read(@path), symbolize_keys: true
  end
  @hash ||= {}
  log.info event: 'stats-loaded'
end

Instance Method Details

#[](k) ⇒ Object



17
# File 'lib/anschel/store.rb', line 17

def [] k ; @hash[k] end

#[]=(k, v) ⇒ Object



19
# File 'lib/anschel/store.rb', line 19

def []= k,v ; @hash[k] = v ; save end

#saveObject



21
22
23
24
25
# File 'lib/anschel/store.rb', line 21

def save
  File.open(@path, 'w') do |f|
    f.puts JrJackson::Json.dump(@hash)
  end
end