Class CloudKit::MemoryTable

  1. lib/cloudkit/store/memory_table.rb
Parent: Object

A MemoryTable implements the essential pieces of the Rufus Tokyo Table API required for CloudKit’s operation. It is basically a hash of hashes with querying capabilities. None of the data is persisted to disk nor is it designed with production use in mind. The primary purpose is to enable testing and development with CloudKit without depending on binary Tokyo Cabinet dependencies.

Implementing a new adapter for CloudKit means writing an adapter that passes the specs for this one.

Methods

public class

  1. new

public instance

  1. []
  2. []=
  3. clear
  4. generate_unique_id
  5. keys
  6. query

protected instance

  1. valid?

Public class methods

new ()

Create a new MemoryTable instance.

[show source]
    # File lib/cloudkit/store/memory_table.rb, line 15
15:     def initialize
16:       @serial_id = 0
17:       clear
18:     end

Public instance methods

[] (key)

Retrieve the hash record for a given key.

[show source]
    # File lib/cloudkit/store/memory_table.rb, line 32
32:     def [](key)
33:       @hash[key]
34:     end
[]= (key, record)

Create a hash record for the given key. Returns the record if valid or nil otherwise. Records are valid if they are hashses with both string keys and string values.

[show source]
    # File lib/cloudkit/store/memory_table.rb, line 23
23:     def []=(key, record)
24:       if valid?(record)
25:         @keys << key unless @hash[key]
26:         return @hash[key] = record
27:       end
28:       nil
29:     end
clear ()

Clear the contents of the store.

[show source]
    # File lib/cloudkit/store/memory_table.rb, line 37
37:     def clear
38:       @hash = {}
39:       @keys = []
40:     end
generate_unique_id ()

Generate a unique ID within the scope of this store.

[show source]
    # File lib/cloudkit/store/memory_table.rb, line 48
48:     def generate_unique_id
49:       @serial_id += 1
50:     end
keys ()

Return an ordered set of all keys in the store.

[show source]
    # File lib/cloudkit/store/memory_table.rb, line 43
43:     def keys
44:       @keys
45:     end
query (&block)

Run a query configured by the provided block. If no block is provided, all records are returned. Each record contains the original hash key/value pairs, plus the primary key (indexed by :pk => value).

[show source]
    # File lib/cloudkit/store/memory_table.rb, line 55
55:     def query(&block)
56:       return @keys.map { |key| @hash[key].merge(:pk => key) } unless block
57:       q = MemoryQuery.new
58:       block.call(q)
59:       q.run(self)
60:     end

Protected instance methods

valid? (record)
[show source]
    # File lib/cloudkit/store/memory_table.rb, line 64
64:     def valid?(record)
65:       return false unless record.is_a?(Hash)
66:       record.keys.all? { |k| k.is_a?(String) && record[k].is_a?(String) }
67:     end