Class: Swarm::Catalog

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm/catalog.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear(key) ⇒ Object



43
44
45
# File 'lib/swarm/catalog.rb', line 43

def clear(key)
  instance.synchronize { |categories| categories[key].clear }
end

.count(key) ⇒ Object



6
7
8
# File 'lib/swarm/catalog.rb', line 6

def count(key)
  instance.synchronize { |categories| categories[key].count }
end

.delete(key, value) ⇒ Object



25
26
27
# File 'lib/swarm/catalog.rb', line 25

def delete(key, value)
  instance.synchronize { |categories| categories[key].delete value }
end

.fetch(key, value, default = nil) ⇒ Object



10
11
12
# File 'lib/swarm/catalog.rb', line 10

def fetch(key, value, default = nil)
  instance.synchronize { |categories| categories[key].fetch value, default }
end

.flush(key, fetch: :keys) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/swarm/catalog.rb', line 29

def flush(key, fetch: :keys)
  instance.synchronize do |categories|
    result = categories.delete(key)
    result ||= Hash.new
    result.send fetch
  end
end

.increment(key, value) ⇒ Object



14
15
16
17
18
19
# File 'lib/swarm/catalog.rb', line 14

def increment(key, value)
  instance.synchronize do |categories|
    categories[key][value] ||= 0
    categories[key][value] += 1
  end
end

.instanceObject



47
48
49
# File 'lib/swarm/catalog.rb', line 47

def instance
  @instance ||= new
end

.select(*keys, fetch: :keys) ⇒ Object



37
38
39
40
41
# File 'lib/swarm/catalog.rb', line 37

def select(*keys, fetch: :keys)
  instance.synchronize do |categories|
    keys.flat_map { |key| categories[key].send fetch }
  end
end

.store(key, value) ⇒ Object



21
22
23
# File 'lib/swarm/catalog.rb', line 21

def store(key, value)
  instance.synchronize { |categories| categories[key].store value, true }
end

Instance Method Details

#categoriesObject (private)



66
67
68
# File 'lib/swarm/catalog.rb', line 66

def categories
  @categories ||= Hash.new { |h, k| h[k] = Hash.new }
end

#synchronize {|categories| ... } ⇒ Object

Yields:



55
56
57
58
# File 'lib/swarm/catalog.rb', line 55

def synchronize
  yield categories
  # transaction.synchronize { yield categories }
end

#transactionObject (private)



62
63
64
# File 'lib/swarm/catalog.rb', line 62

def transaction
  @transaction ||= Mutex.new
end