Class: MemStore

Inherits:
Object
  • Object
show all
Defined in:
lib/memstore/core.rb,
lib/memstore/queries.rb,
lib/memstore/version.rb

Constant Summary collapse

VERSION =
"2.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key: nil, access: nil, items: nil) ⇒ MemStore

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.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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

Instance Attribute Details

#itemsObject

Provides access to internal items collection (which is simply a Hash).



39
40
41
# File 'lib/memstore/core.rb', line 39

def items
  @items
end

Instance Method Details

#<<(item) ⇒ Object

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.



58
59
60
61
# File 'lib/memstore/core.rb', line 58

def <<(item)
  @items[access_key(item)] = item
  self
end

#[](key) ⇒ Object

Retrieves one item by key.

key - key of item to be retrieved

Returns item if it exists, otherwise nil.



80
81
82
# File 'lib/memstore/core.rb', line 80

def [](key)
  @items[key]
end

#add(*items) ⇒ Object

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.



70
71
72
73
# File 'lib/memstore/core.rb', line 70

def add(*items)
  items.each { |item| self << item }
  self
end

#allObject

Returns all items as an Array.



42
43
44
# File 'lib/memstore/core.rb', line 42

def all
  @items.values
end

#collect(attribute) ⇒ Object

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.



134
135
136
# File 'lib/memstore/core.rb', line 134

def collect(attribute)
  all.collect { |item| access_attribute(item, attribute) }
end

#count_all(conditions = {}, &block) ⇒ Object Also known as: count

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.



79
80
81
# File 'lib/memstore/queries.rb', line 79

def count_all(conditions={}, &block)
  all.count { |item| match_all(item, conditions, &block) }
end

#count_any(conditions = {}, &block) ⇒ Object

Counts the number of items that fulfill at least one of the provided conditions.



85
86
87
# File 'lib/memstore/queries.rb', line 85

def count_any(conditions={}, &block)
  all.count { |item| match_any(item, conditions, &block) }
end

#count_none(conditions = {}, &block) ⇒ Object

Counts the number of items that violate all provided conditions.



100
101
102
# File 'lib/memstore/queries.rb', line 100

def count_none(conditions={}, &block)
  all.count { |item| match_none(item, conditions, &block) }
end

#count_not_all(conditions = {}, &block) ⇒ Object

Counts the number of items that violate at least one of the provided conditions.



95
96
97
# File 'lib/memstore/queries.rb', line 95

def count_not_all(conditions={}, &block)
  all.count { |item| !match_all(item, conditions, &block) }
end

#count_one(conditions = {}, &block) ⇒ Object

Counts the number of items that fulfill exactly one of the provided conditions.



90
91
92
# File 'lib/memstore/queries.rb', line 90

def count_one(conditions={}, &block)
  all.count { |item| match_one(item, conditions, &block) }
end

#delete_all(conditions = {}, &block) ⇒ Object Also known as: delete

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.



112
113
114
115
116
117
# 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

#delete_any(conditions = {}, &block) ⇒ Object

Deletes items that fulfill at least one of the provided conditions.



121
122
123
124
125
126
# 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

#delete_item(item) ⇒ Object

Deletes one item by reference.

item - item to be deleted

Returns the item that was deleted or nil if it doesn’t exist.



98
99
100
# File 'lib/memstore/core.rb', line 98

def delete_item(item)
  @items.delete(access_key(item))
end

#delete_items(*items) ⇒ Object

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.



107
108
109
# File 'lib/memstore/core.rb', line 107

def delete_items(*items)
  items.collect { |item| @items.delete(access_key(item)) }
end

#delete_key(key) ⇒ Object

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.



116
117
118
# File 'lib/memstore/core.rb', line 116

def delete_key(key)
  @items.delete(key)
end

#delete_keys(*keys) ⇒ Object

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.



125
126
127
# File 'lib/memstore/core.rb', line 125

def delete_keys(*keys)
  keys.collect { |key| @items.delete(key) }
end

#delete_none(conditions = {}, &block) ⇒ Object

Deletes items that violate all provided conditions.



145
146
147
148
149
150
# 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

#delete_not_all(conditions = {}, &block) ⇒ Object

Deletes items that violate at least one of the provided conditions.



137
138
139
140
141
142
# 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

#delete_one(conditions = {}, &block) ⇒ Object

Deletes items that fulfill exactly one of the provided conditions.



129
130
131
132
133
134
# 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

#find_all(conditions = {}, &block) ⇒ Object Also known as: find

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.



13
14
15
# File 'lib/memstore/queries.rb', line 13

def find_all(conditions={}, &block)
  all.select { |item| match_all(item, conditions, &block) }
end

#find_any(conditions = {}, &block) ⇒ Object

Finds items that fulfill at least one of the provided conditions.



19
20
21
# File 'lib/memstore/queries.rb', line 19

def find_any(conditions={}, &block)
  all.select { |item| match_any(item, conditions, &block) }
end

#find_none(conditions = {}, &block) ⇒ Object

Finds items that violate all provided conditions.



34
35
36
# File 'lib/memstore/queries.rb', line 34

def find_none(conditions={}, &block)
  all.select { |item| match_none(item, conditions, &block) }
end

#find_not_all(conditions = {}, &block) ⇒ Object

Finds items that violate at least one of the provided conditions.



29
30
31
# File 'lib/memstore/queries.rb', line 29

def find_not_all(conditions={}, &block)
  all.reject { |item| match_all(item, conditions, &block) }
end

#find_one(conditions = {}, &block) ⇒ Object

Finds items that fulfill exactly one of the provided conditions.



24
25
26
# File 'lib/memstore/queries.rb', line 24

def find_one(conditions={}, &block)
  all.select { |item| match_one(item, conditions, &block) }
end

#first_all(conditions = {}, &block) ⇒ Object Also known as: first

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.



46
47
48
# File 'lib/memstore/queries.rb', line 46

def first_all(conditions={}, &block)
  all.detect { |item| match_all(item, conditions, &block) }
end

#first_any(conditions = {}, &block) ⇒ Object

Finds the first item that fulfills at least one of the provided conditions.



52
53
54
# File 'lib/memstore/queries.rb', line 52

def first_any(conditions={}, &block)
  all.detect { |item| match_any(item, conditions, &block) }
end

#first_none(conditions = {}, &block) ⇒ Object

Finds the first item that violates all provided conditions.



67
68
69
# File 'lib/memstore/queries.rb', line 67

def first_none(conditions={}, &block)
  all.detect { |item| match_none(item, conditions, &block) }
end

#first_not_all(conditions = {}, &block) ⇒ Object

Finds the first item that violates at least one of the provided conditions.



62
63
64
# File 'lib/memstore/queries.rb', line 62

def first_not_all(conditions={}, &block)
  all.detect { |item| !match_all(item, conditions, &block) }
end

#first_one(conditions = {}, &block) ⇒ Object

Finds the first item that fulfills exactly one of the provided conditions.



57
58
59
# File 'lib/memstore/queries.rb', line 57

def first_one(conditions={}, &block)
  all.detect { |item| match_one(item, conditions, &block) }
end

#get(*keys) ⇒ Object

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.



89
90
91
# File 'lib/memstore/core.rb', line 89

def get(*keys)
  keys.collect { |key| @items[key] }
end

#map(&block) ⇒ Object

Maps given block to all items.

block - block to invoke with each item

Returns an array of results from each block invocation.



143
144
145
# File 'lib/memstore/core.rb', line 143

def map(&block)
  all.map(&block)
end

#sizeObject

Returns total number of items in the data store.



47
48
49
# File 'lib/memstore/core.rb', line 47

def size
  @items.length
end