class MemStore
Constants
- VERSION
Attributes
Provides access to internal items collection (which is simply a Hash).
Public Class Methods
Initializes an MemStore
.
key - optional String/Symbol to be sent or Proc/lambda/method to be called access - optional String/Symbol to be sent or Proc/lambda/method to be called items - optional Array of items to be added
Returns initialized MemStore
.
# File lib/memstore/core.rb, line 10 def initialize(key: nil, access: nil, items: nil) @items = {} define_singleton_method :access_key, if key.nil? -> item { item.hash } elsif key.respond_to?(:call) -> item { key.call(item) } else -> item { access_attribute(item, key) } end define_singleton_method :access_attribute, if access.nil? -> item, attribute { item.send(attribute) } elsif [Symbol, String].include?(access.class) -> item, attribute { item.send(access, attribute) } elsif access.respond_to?(:call) -> item, attribute { access.call(item, attribute) } else raise "No usable access method." end add(*items) unless items.nil? end
Public Instance Methods
Adds one item to the data store.
item - item to add
Returns the data store itself.
Raises NoMethodError when an item does’t respond to the key attribute method.
# File lib/memstore/core.rb, line 58 def <<(item) @items[access_key(item)] = item self end
Retrieves one item by key.
key - key of item to be retrieved
Returns item if it exists, otherwise nil.
# File lib/memstore/core.rb, line 80 def [](key) @items[key] end
Adds one or more items to the data store.
items - items to be added
Returns the data store itself.
Raises NoMethodError when an item does’t respond to the key attribute method.
# File lib/memstore/core.rb, line 70 def add(*items) items.each { |item| self << item } self end
Returns all items as an Array.
# File lib/memstore/core.rb, line 42 def all @items.values end
Collects values of given attribute from all items.
attribute - name of attribute to be collected (symbol or string)
Returns an array of attribute values of each item.
# File lib/memstore/core.rb, line 134 def collect(attribute) all.collect { |item| access_attribute(item, attribute) } end
Counts the number of items that fulfill all provided conditions.
conditions - Hash mapping attributes to conditions (anything that responds to ===). block - Optional block taking an item and returning a bool. Evaluated after conditions.
Returns the number of items that fulfill all conditions.
# File lib/memstore/queries.rb, line 79 def count_all(conditions={}, &block) all.count { |item| match_all(item, conditions, &block) } end
Counts the number of items that fulfill at least one of the provided conditions.
# File lib/memstore/queries.rb, line 85 def count_any(conditions={}, &block) all.count { |item| match_any(item, conditions, &block) } end
Counts the number of items that violate all provided conditions.
# File lib/memstore/queries.rb, line 100 def count_none(conditions={}, &block) all.count { |item| match_none(item, conditions, &block) } end
Counts the number of items that violate at least one of the provided conditions.
# File lib/memstore/queries.rb, line 95 def count_not_all(conditions={}, &block) all.count { |item| !match_all(item, conditions, &block) } end
Counts the number of items that fulfill exactly one of the provided conditions.
# File lib/memstore/queries.rb, line 90 def count_one(conditions={}, &block) all.count { |item| match_one(item, conditions, &block) } end
Deletes items that fulfill all provided conditions.
conditions - Hash mapping attributes to conditions (anything that responds to ===). block - Optional block taking an item and returning a bool. Evaluated after conditions.
Returns an Array of items that fulfill all conditions and were deleted.
# File lib/memstore/queries.rb, line 112 def delete_all(conditions={}, &block) @items.inject([]) do |items, (key, item)| items << @items.delete(key) if match_all(item, conditions, &block) items end end
Deletes items that fulfill at least one of the provided conditions.
# File lib/memstore/queries.rb, line 121 def delete_any(conditions={}, &block) @items.inject([]) do |items, (key, item)| items << @items.delete(key) if match_any(item, conditions, &block) items end end
Deletes one item by reference.
item - item to be deleted
Returns the item that was deleted or nil if it doesn’t exist.
# File lib/memstore/core.rb, line 98 def delete_item(item) @items.delete(access_key(item)) end
Deletes one or more items by reference.
items - items to be deleted
Returns an array of items that were deleted with nil where an item doesn’t exist.
# File lib/memstore/core.rb, line 107 def delete_items(*items) items.collect { |item| @items.delete(access_key(item)) } end
Deletes one item by key.
key - key of item to be deleted
Returns the item that was deleted or nil if it doesn’t exist.
# File lib/memstore/core.rb, line 116 def delete_key(key) @items.delete(key) end
Deletes one or more items by key.
keys - keys of items to be deleted
Returns an array of items that were deleted with nil where no item with that key exists.
# File lib/memstore/core.rb, line 125 def delete_keys(*keys) keys.collect { |key| @items.delete(key) } end
Deletes items that violate all provided conditions.
# File lib/memstore/queries.rb, line 145 def delete_none(conditions={}, &block) @items.inject([]) do |items, (key, item)| items << @items.delete(key) if match_none(item, conditions, &block) items end end
Deletes items that violate at least one of the provided conditions.
# File lib/memstore/queries.rb, line 137 def delete_not_all(conditions={}, &block) @items.inject([]) do |items, (key, item)| items << @items.delete(key) if !match_all(item, conditions, &block) items end end
Deletes items that fulfill exactly one of the provided conditions.
# File lib/memstore/queries.rb, line 129 def delete_one(conditions={}, &block) @items.inject([]) do |items, (key, item)| items << @items.delete(key) if match_one(item, conditions, &block) items end end
Finds items that fulfill all provided conditions.
conditions - Hash mapping attributes to conditions (anything that responds to ===). block - Optional block taking an item and returning a bool. Evaluated after conditions.
Returns an Array of items that fulfill all conditions.
# File lib/memstore/queries.rb, line 13 def find_all(conditions={}, &block) all.select { |item| match_all(item, conditions, &block) } end
Finds items that fulfill at least one of the provided conditions.
# File lib/memstore/queries.rb, line 19 def find_any(conditions={}, &block) all.select { |item| match_any(item, conditions, &block) } end
Finds items that violate all provided conditions.
# File lib/memstore/queries.rb, line 34 def find_none(conditions={}, &block) all.select { |item| match_none(item, conditions, &block) } end
Finds items that violate at least one of the provided conditions.
# File lib/memstore/queries.rb, line 29 def find_not_all(conditions={}, &block) all.reject { |item| match_all(item, conditions, &block) } end
Finds items that fulfill exactly one of the provided conditions.
# File lib/memstore/queries.rb, line 24 def find_one(conditions={}, &block) all.select { |item| match_one(item, conditions, &block) } end
Finds the first item that fulfills all provided conditions.
conditions - Hash mapping attributes to conditions (anything that responds to ===). block - Optional block taking an item and returning a bool. Evaluated after conditions.
Returns the first item that fulfills all conditions.
# File lib/memstore/queries.rb, line 46 def first_all(conditions={}, &block) all.detect { |item| match_all(item, conditions, &block) } end
Finds the first item that fulfills at least one of the provided conditions.
# File lib/memstore/queries.rb, line 52 def first_any(conditions={}, &block) all.detect { |item| match_any(item, conditions, &block) } end
Finds the first item that violates all provided conditions.
# File lib/memstore/queries.rb, line 67 def first_none(conditions={}, &block) all.detect { |item| match_none(item, conditions, &block) } end
Finds the first item that violates at least one of the provided conditions.
# File lib/memstore/queries.rb, line 62 def first_not_all(conditions={}, &block) all.detect { |item| !match_all(item, conditions, &block) } end
Finds the first item that fulfills exactly one of the provided conditions.
# File lib/memstore/queries.rb, line 57 def first_one(conditions={}, &block) all.detect { |item| match_one(item, conditions, &block) } end
Retrieves one or more items by key.
keys - keys of items to be retrieved
Returns an array of items with nil where no item with that key exists.
# File lib/memstore/core.rb, line 89 def get(*keys) keys.collect { |key| @items[key] } end
Maps given block to all items.
block - block to invoke with each item
Returns an array of results from each block invocation.
# File lib/memstore/core.rb, line 143 def map(&block) all.map(&block) end
Returns total number of items in the data store.
# File lib/memstore/core.rb, line 47 def size @items.length end
Private Instance Methods
Evaluates conditions using AND, i.e. condition && condition && … [&& block]
item - The item to be tested. conditions - Hash of conditions to be evaluated. block - Optional block that can test the item after the conditions are evaluated.
Returns a bool indicating whether the item passed the conditions and/or block.
# File lib/memstore/queries.rb, line 161 def match_all(item, conditions={}, &block) conditions.all? { |attribute, condition| condition === access_attribute(item, attribute) } && if block_given? then yield(item) else true end end
Evaluates conditions using OR, i.e. condition || condition || … [|| block]
# File lib/memstore/queries.rb, line 167 def match_any(item, conditions={}, &block) conditions.any? { |attribute, condition| condition === access_attribute(item, attribute) } || if block_given? then yield(item) else false end end
Evaluates condition using AND NOT, i.e. !condition && !condition && … [&& !block]
# File lib/memstore/queries.rb, line 179 def match_none(item, conditions={}, &block) conditions.none? { |attribute, condition| condition === access_attribute(item, attribute) } && if block_given? then !yield(item) else true end end
Evaluates conditions using XOR, i.e. condition ^ condition ^ condition … [^ block]
# File lib/memstore/queries.rb, line 173 def match_one(item, conditions={}, &block) conditions.one? { |attribute, condition| condition === access_attribute(item, attribute) } ^ if block_given? then yield(item) else false end end