Class: Zap::Store

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Store

Returns a new instance of Store.



6
7
8
9
10
11
# File 'lib/zap/store.rb', line 6

def initialize(file)
  self.file = file
  
  init_file unless File.exists?(file)
  self.store = Yajl::Parser.parse(File.new(file, 'r'))
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



4
5
6
# File 'lib/zap/store.rb', line 4

def file
  @file
end

#storeObject

Returns the value of attribute store.



4
5
6
# File 'lib/zap/store.rb', line 4

def store
  @store
end

Instance Method Details

#[](key) ⇒ Object



27
28
29
# File 'lib/zap/store.rb', line 27

def [](key)
  store[key]
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  self.store[key] = value
  self.store.delete(key) if value.nil?
  value
end

#each_pairObject



31
32
33
34
35
# File 'lib/zap/store.rb', line 31

def each_pair
  store.each do |(key, value)|
    yield key, value
  end
end

#to_jsonObject



13
14
15
# File 'lib/zap/store.rb', line 13

def to_json
  Yajl::Encoder.encode(store)
end

#writeObject



17
18
19
# File 'lib/zap/store.rb', line 17

def write
  File.open(file, 'w') { |f| f.write(to_json) }
end