Class: LWAC::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/lwac/client/storage.rb

Instance Method Summary collapse

Constructor Details

#initialize(filepath = nil) ⇒ Store

Create a new store with a given file.

If a filepath is given, PStore is used for on-disk, persistent storage. if thread_safe is true then:

- Hashes will be made thread-safe
- PStores will be switched to thread-safe mode


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lwac/client/storage.rb', line 15

def initialize(filepath=nil)
  # Create a mutex if using a hash
  @mutex = Mutex.new

  if filepath == nil or filepath.to_s == ""
    @store = Hash.new
    @type = :hash
  else
    @store = FileCache.new(filepath)
    @type = :file
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object


Method_missing handles most things…



31
32
33
34
35
# File 'lib/lwac/client/storage.rb', line 31

def method_missing(m, *args, &block)
  @store.send(m, *args, &block)
rescue NoMethodError => e
  super
end

Instance Method Details

#closeObject

Closes the file system, missing from Hash



41
42
43
44
# File 'lib/lwac/client/storage.rb', line 41

def close
  return if @type == :hash
  @store.close
end

#delete_allObject

Removes all items



56
57
58
59
60
61
62
63
64
65
# File 'lib/lwac/client/storage.rb', line 56

def delete_all
  # GC's probably quicker than looping and removing stuff
  if @type == :hash   
    @mutex.synchronize{
      @store = Hash.new 
    }
  else
    @store.delete_all
  end
end

#delete_from_index(key) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/lwac/client/storage.rb', line 46

def delete_from_index(key)
  if @type == :hash
    @mutex.synchronize{
      return @store.delete(key)
    }
  end
  @store.delete_from_index(key)
end