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
public instance
protected instance
Public class methods
Create a new MemoryTable instance.
# File lib/cloudkit/store/memory_table.rb, line 15 15: def initialize 16: @serial_id = 0 17: clear 18: end
Public instance methods
Retrieve the hash record for a given key.
# File lib/cloudkit/store/memory_table.rb, line 32 32: def [](key) 33: @hash[key] 34: end
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.
# 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 the contents of the store.
# File lib/cloudkit/store/memory_table.rb, line 37 37: def clear 38: @hash = {} 39: @keys = [] 40: end
Generate a unique ID within the scope of this store.
# File lib/cloudkit/store/memory_table.rb, line 48 48: def generate_unique_id 49: @serial_id += 1 50: end
Return an ordered set of all keys in the store.
# File lib/cloudkit/store/memory_table.rb, line 43 43: def keys 44: @keys 45: end
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).
# 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
# 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