Class: Peanut::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/peanut/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/peanut/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/peanut/store.rb', line 4

def file
  @file
end

#storeObject

Returns the value of attribute store.



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

def store
  @store
end

Instance Method Details

#[](key) ⇒ Object



23
24
25
# File 'lib/peanut/store.rb', line 23

def [](key)
  return store[key]
end

#[]=(key, val) ⇒ Object



17
18
19
20
21
# File 'lib/peanut/store.rb', line 17

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

#each_pairObject



31
32
33
34
35
36
37
38
39
# File 'lib/peanut/store.rb', line 31

def each_pair
  store.each do |key, val|
    yield key, val
  end
  
  if store.empty?
    yield "Bucket", "empty"
  end
end

#save!Object



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

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

#to_jsonObject



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

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