Module: Imba::DataStore

Defined in:
lib/imba/data_store.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/imba/data_store.rb', line 6

def data
  @data
end

.pathObject



16
17
18
# File 'lib/imba/data_store.rb', line 16

def path
  @path ||= "#{Imba::DIRECTORY}/data.pstore"
end

Class Method Details

.[](key) ⇒ Object



24
25
26
# File 'lib/imba/data_store.rb', line 24

def [](key)
  data_store.transaction(true) { @data[key] }
end

.[]=(key, value) ⇒ Object



28
29
30
# File 'lib/imba/data_store.rb', line 28

def []=(key, value)
  data_store.transaction { @data[key] = value }
end

.allObject



32
33
34
# File 'lib/imba/data_store.rb', line 32

def all
  data_store.transaction(true) { @data.roots }
end

.clearObject



62
63
64
# File 'lib/imba/data_store.rb', line 62

def clear
  delete(all)
end

.data_storeObject



20
21
22
# File 'lib/imba/data_store.rb', line 20

def data_store
  data || init
end

.delete(*keys) ⇒ Object



55
56
57
58
59
60
# File 'lib/imba/data_store.rb', line 55

def delete(*keys)
  data_store.transaction do
    keys.flatten.each { |key| @data.delete(key) }
    @data.commit
  end
end

.initObject



9
10
11
12
13
14
# File 'lib/imba/data_store.rb', line 9

def init
  @data = PStore.new(path, true)
  @data.ultra_safe = true
  @data.transaction { @data.commit }
  @data
end

.inspectObject



66
67
68
69
# File 'lib/imba/data_store.rb', line 66

def inspect
  data_store
  "#<#{name}:#{ruby_like_object_id} #{instance_vars}>"
end

.key?(key) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/imba/data_store.rb', line 51

def key?(key)
  data_store.transaction(true) { @data.root?(key) }
end

.to_aObject



40
41
42
# File 'lib/imba/data_store.rb', line 40

def to_a
  Imba::DataStore.all.each_with_object([]) { |id, ary| ary << "#{id}: #{Imba::DataStore[id]}" }
end

.to_hashObject



36
37
38
# File 'lib/imba/data_store.rb', line 36

def to_hash
  Imba::DataStore.all.each_with_object({}) { |id, hsh| hsh[id] = Imba::DataStore[id] }
end

.transactionObject



44
45
46
47
48
49
# File 'lib/imba/data_store.rb', line 44

def transaction
  data_store.transaction do
    yield @data
    @data.commit
  end
end