Class: Tanakai::Base::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/tanakai/base/storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStorage

Returns a new instance of Storage.



6
7
8
9
# File 'lib/tanakai/base/storage.rb', line 6

def initialize
  @mutex = Mutex.new
  @database = {}
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



4
5
6
# File 'lib/tanakai/base/storage.rb', line 4

def database
  @database
end

Instance Method Details

#add(scope, value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tanakai/base/storage.rb', line 24

def add(scope, value)
  @mutex.synchronize do
    database[scope] ||= []
    if value.kind_of?(Array)
      database[scope] += value
      database[scope].uniq!
    else
      database[scope].push(value) unless database[scope].include?(value)
    end
  end
end

#all(scope = nil) ⇒ Object



11
12
13
14
15
# File 'lib/tanakai/base/storage.rb', line 11

def all(scope = nil)
  @mutex.synchronize do
    scope ? database.fetch(scope, []) : database
  end
end

#clear!Object



47
48
49
50
51
# File 'lib/tanakai/base/storage.rb', line 47

def clear!
  @mutex.synchronize do
    @database = {}
  end
end

#include?(scope, value) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
# File 'lib/tanakai/base/storage.rb', line 17

def include?(scope, value)
  @mutex.synchronize do
    database[scope] ||= []
    database[scope].include?(value)
  end
end

#unique?(scope, value) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/tanakai/base/storage.rb', line 38

def unique?(scope, value)
  @mutex.synchronize do
    database[scope] ||= []
    database[scope].include?(value) ? false : database[scope].push(value) and true
  end
end