Class: Peanut::Store
- Inherits:
-
Object
- Object
- Peanut::Store
- Defined in:
- lib/peanut/store.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#store ⇒ Object
Returns the value of attribute store.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #each_pair ⇒ Object
-
#initialize(file) ⇒ Store
constructor
A new instance of Store.
- #save! ⇒ Object
- #to_json ⇒ Object
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
#file ⇒ Object
Returns the value of attribute file.
4 5 6 |
# File 'lib/peanut/store.rb', line 4 def file @file end |
#store ⇒ Object
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_pair ⇒ Object
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_json ⇒ Object
13 14 15 |
# File 'lib/peanut/store.rb', line 13 def to_json Yajl::Encoder.encode(store) end |