Class: Geode::Store

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

Overview

Subclass this to implement your own stores.

Direct Known Subclasses

RedisStore, SequelStore

Instance Method Summary collapse

Constructor Details

#initialize(name, connection = nil) ⇒ Store

Connect to a store.

Parameters:

  • name (Symbol, String) —

    The name of the store

  • connection (Hash, String) (defaults to: nil) —

    Connection parameters passed to the DB client



7
8
9
# File 'lib/geode.rb', line 7

def initialize(name, connection = nil)
  @name = name.to_s
end

Instance Method Details

#[](key) ⇒ Object

Retrieve the object at key from the store. This is implemented using #peek and therefore changes to the object returned by this method will NOT be persisted in the store. Use this if you simply need to fetch a value from the store.

Examples:

"store[:key] #=> 5"

Parameters:

  • key (Object) —

    The key to look up

Returns:

  • (Object) —

    The object at key



44
45
46
# File 'lib/geode.rb', line 44

def [](key)
  peek[key]
end

#destroy ⇒ Object

"Destroy" the store, deleting all data. The store can be opened again, recreating it in a blank state.



50
51
52
# File 'lib/geode.rb', line 50

def destroy
  raise 'subclasses must implement #destroy'
end

#open {|table| ... } ⇒ Object

"Open" the store for reading and/or writing. be persisted in the store When in doubt, use this method.

Examples:

"store.open { |table| table[:key] = 5 }"
"store.open { |table| table[:key] } #=> 5"

Yields:

  • a block which receives table as its sole parameter

Yield Parameters:

  • table (Hash) —

    The store's table. Changes to this Hash will

Returns:

  • (Object) —

    The return value of the block



21
22
23
# File 'lib/geode.rb', line 21

def open
  raise 'subclasses must implement #open'
end

#peek ⇒ Hash

"Peek" inside the store, returning a copy of its table. Changes to this copy will NOT be persisted in the store. Use this if you simply want to view the store's table.

Examples:

"store.peek.key?(:test) #=> false"

Returns:

  • (Hash) —

    A copy of the store's table



31
32
33
# File 'lib/geode.rb', line 31

def peek
  open(&:itself)
end