Class: Sunstore::Store

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

Constant Summary collapse

@@instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



17
18
19
# File 'lib/sunstore/store.rb', line 17

def initialize
  config  
end

Instance Attribute Details

#basenameObject (readonly)

Returns the value of attribute basename.



7
8
9
# File 'lib/sunstore/store.rb', line 7

def basename
  @basename
end

#formatObject (readonly)

Returns the value of attribute format.



7
8
9
# File 'lib/sunstore/store.rb', line 7

def format
  @format
end

#handlerObject (readonly)

Returns the value of attribute handler.



7
8
9
# File 'lib/sunstore/store.rb', line 7

def handler
  @handler
end

#store_fileObject (readonly)

Returns the value of attribute store_file.



7
8
9
# File 'lib/sunstore/store.rb', line 7

def store_file
  @store_file
end

Class Method Details

.instanceObject



9
10
11
# File 'lib/sunstore/store.rb', line 9

def self.instance
  @@instance ||= new
end

.resetObject



13
14
15
# File 'lib/sunstore/store.rb', line 13

def self.reset
  @@instance = nil
end

Instance Method Details

#clearObject



48
49
50
51
# File 'lib/sunstore/store.rb', line 48

def clear
  data.clear
  save_data  
end

#config(params = {}) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/sunstore/store.rb', line 21

def config(params={})
  @format = [:yaml,:json,nil].include?(params[:format]) ? params[:format] || :yaml : raise("Only yaml and json currently supported")
  @basename = params[:basename] || "sunstore"
  @basedir = params[:basedir] ||  (defined?(Rails) ? Rails.root.to_s : Dir.getwd)
  create_dir_structure(@basedir)
  @handler = Sunstore::Handler.for(format)
  @store_file = File.join(@basedir,"#{@basename}.#{@handler.type}")
end

#delete(key) ⇒ Object



39
40
41
42
# File 'lib/sunstore/store.rb', line 39

def delete(key)
  data.delete(key)
  save_data
end

#get(key) ⇒ Object



30
31
32
# File 'lib/sunstore/store.rb', line 30

def get(key)
  data[key]
end

#keysObject



44
45
46
# File 'lib/sunstore/store.rb', line 44

def keys
  data.keys  
end

#put(key, value) ⇒ Object



34
35
36
37
# File 'lib/sunstore/store.rb', line 34

def put(key,value)
  data[key] = value
  save_data
end