Class: Barker::Store::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/barker/store/memory.rb

Instance Method Summary collapse

Constructor Details

#initialize(key, options = {}) ⇒ Memory

Returns a new instance of Memory.



6
7
8
9
10
11
# File 'lib/barker/store/memory.rb', line 6

def initialize(key, options = {})
  @key        = key
  @index      = ThreadSafe::Cache.new
  @build_key  = options[:build_key] || false
  init_index options[:init]
end

Instance Method Details

#add(item) ⇒ Object

Raises:

  • (DuplicateError)


13
14
15
16
17
18
19
20
# File 'lib/barker/store/memory.rb', line 13

def add(item)
  key = lookup_key(item)

  raise DuplicateError if exist?(key)

  __add(key, item)
  item
end

#allObject



60
61
62
# File 'lib/barker/store/memory.rb', line 60

def all
  deep_copy(__all)
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/barker/store/memory.rb', line 38

def exist?(key)
  @index.key?(key)
end

#filter(&block) ⇒ Object



34
35
36
# File 'lib/barker/store/memory.rb', line 34

def filter(&block)
  @index.values.select(&block)
end

#get(key) ⇒ Object



42
43
44
45
46
# File 'lib/barker/store/memory.rb', line 42

def get(key)
  if item = __get(key)
    deep_copy(item)
  end
end

#reload(item) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/barker/store/memory.rb', line 68

def reload(item)
  if item
    get(key(item))
  else
    nil
  end
end

#remove(item) ⇒ Object

Raises:

  • (NotFoundError)


28
29
30
31
32
# File 'lib/barker/store/memory.rb', line 28

def remove(item)
  key = key(item)
  raise NotFoundError unless exist?(key)
  @index.delete(key)
end

#select(group_by, *values) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/barker/store/memory.rb', line 48

def select(group_by, *values)
  conditions = Hash[Array(group_by).each_with_index.map do |group, i|
    [group, values[i]]
  end]

  __all.select do |item|
    conditions.each.all? do |k, v|
      item.respond_to?(k) && item.public_send(k) == v
    end
  end
end

#sizeObject



64
65
66
# File 'lib/barker/store/memory.rb', line 64

def size
  @index.size
end

#update(item) ⇒ Object

Raises:

  • (NotFoundError)


22
23
24
25
26
# File 'lib/barker/store/memory.rb', line 22

def update(item)
  key = key(item)
  raise NotFoundError unless exist?(key)
  @index[key] = deep_copy(item)
end

#update_attributes(key, attributes) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/barker/store/memory.rb', line 76

def update_attributes(key, attributes)
  if item = __get(key)
    attributes.each do |attribute, value|
      item.public_send("#{attribute}=", value)
    end
  end

  get(key)
end