class Hikki::Adapters::MemoryCollection
Attributes
store[R]
uuid_generator[R]
Public Class Methods
new(collection, store, uuid_generator)
click to toggle source
Calls superclass method
Hikki::Collection::new
# File lib/hikki/adapters/memory_collection.rb, line 6 def initialize(collection, store, uuid_generator) super(collection) @store = store @uuid_generator = uuid_generator end
Public Instance Methods
all(options={})
click to toggle source
# File lib/hikki/adapters/memory_collection.rb, line 30 def all(options={}) options = normalize_options(options) store.fetch(collection, {}).values[page_range(options)] end
find(id)
click to toggle source
# File lib/hikki/adapters/memory_collection.rb, line 26 def find(id) store.fetch(collection, {}).fetch(id.to_s, {}) end
find_by(field, value, options={})
click to toggle source
# File lib/hikki/adapters/memory_collection.rb, line 35 def find_by(field, value, options={}) options = normalize_options(options) return find_by_index(field, value, options) if has_index?(field) all.select { |o| o.fetch(field.to_s) == value }[page_range(options)] end
has_index?(field)
click to toggle source
# File lib/hikki/adapters/memory_collection.rb, line 53 def has_index?(field) store.has_key?(index_key) && store[index_key].has_key?(field.to_s) end
id_for(data)
click to toggle source
# File lib/hikki/adapters/memory_collection.rb, line 58 def id_for(data) data.fetch('id', uuid_generator.uuid).to_s end
index(field)
click to toggle source
# File lib/hikki/adapters/memory_collection.rb, line 12 def index(field) store[index_key] ||= {} store[index_key][field.to_s] ||= {} true end
remove(id)
click to toggle source
# File lib/hikki/adapters/memory_collection.rb, line 41 def remove(id) remove_from_index(find(id)) store.fetch(collection, {}).delete(id.to_s) true end
remove_all()
click to toggle source
# File lib/hikki/adapters/memory_collection.rb, line 47 def remove_all store.delete(collection) store.delete(index_key) true end
save(data)
click to toggle source
# File lib/hikki/adapters/memory_collection.rb, line 18 def save(data) data = normalize_data(data) store[collection] ||= {} store[collection][data['id']] = data add_to_index(data) data end
Private Instance Methods
add_to_index(data)
click to toggle source
# File lib/hikki/adapters/memory_collection.rb, line 71 def add_to_index(data) store.fetch(index_key, {}).keys.each do |field| store[index_key][field][data[field]] ||= [] store[index_key][field][data[field]] << data['id'] end end
find_by_index(field, value, options)
click to toggle source
# File lib/hikki/adapters/memory_collection.rb, line 67 def find_by_index(field, value, options) store[index_key][field.to_s].fetch(value, []).map { |id| find(id) }.reject { |v| v == {} }[page_range(options)] end
index_key()
click to toggle source
# File lib/hikki/adapters/memory_collection.rb, line 63 def index_key collection + '_index' end
remove_from_index(data)
click to toggle source
# File lib/hikki/adapters/memory_collection.rb, line 78 def remove_from_index(data) return if data == {} store.fetch(index_key, {}).keys.each do |field| store[index_key][field].fetch(data[field], []).delete(data['id']) end end