Module: App::Persistence

Defined in:
lib/project/PackingPeanut.rb

Constant Summary collapse

MODE_PRIVATE =
0
MODE_WORLD_READABLE =
1
MODE_WORLD_WRITEABLE =
2
MODE_MULTI_PROCESS =
4
JSONObject =
Org::Json::JSONObject
PREFERENCE_MODES =
{
  private: MODE_PRIVATE,
  readable: MODE_WORLD_READABLE,
  world_readable: MODE_WORLD_READABLE,
  writable: MODE_WORLD_WRITEABLE,
  world_writable: MODE_WORLD_WRITEABLE,
  multi: MODE_MULTI_PROCESS,
  multi_process: MODE_MULTI_PROCESS
}

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



33
34
35
36
37
# File 'lib/project/PackingPeanut.rb', line 33

def [](key)
  json_string = get_value(key)
  return json_string if json_string == ""
  deserialize(key, json_string)
end

.[]=(key, value) ⇒ Object

Serialize key/value as json then store that string with the settings key == json key



25
26
27
28
29
30
31
# File 'lib/project/PackingPeanut.rb', line 25

def []=(key, value)
  settings = get_settings
  editor = settings.edit
  json = serialize(key,value)
  editor.putString(key, json.toString)
  editor.commit 
end

.allObject



70
71
72
73
# File 'lib/project/PackingPeanut.rb', line 70

def all
  settings = get_settings
  settings.getAll.map { |key, value| {key.to_sym => JSONObject.new(value).get(key)} }
end

.context=(supplied_context) ⇒ Object

attr_accessor is not supported for modules in RMAndroid… yet.



86
87
88
# File 'lib/project/PackingPeanut.rb', line 86

def context= supplied_context 
  @context = supplied_context
end

.current_contextObject

Allows us to use this from anywhere by setting the context Useful when you want to access this module from the REPL



81
82
83
# File 'lib/project/PackingPeanut.rb', line 81

def current_context
  @context || getApplicationContext
end

.deserialize(key, json_string) ⇒ Object



44
45
46
47
# File 'lib/project/PackingPeanut.rb', line 44

def deserialize(key, json_string)
  json = JSONObject.new(json_string)
  json.get(key)
end

.get_settingsObject



75
76
77
# File 'lib/project/PackingPeanut.rb', line 75

def get_settings
  current_context.getSharedPreferences(storage_file, preference_mode)
end

.get_value(key) ⇒ Object



49
50
51
52
# File 'lib/project/PackingPeanut.rb', line 49

def get_value key
  settings = get_settings
  settings.getString(key,nil)
end

.preference_modeObject



66
67
68
# File 'lib/project/PackingPeanut.rb', line 66

def preference_mode
  @current_preference_mode ||= MODE_PRIVATE
end

.preference_mode=(value) ⇒ Object



62
63
64
# File 'lib/project/PackingPeanut.rb', line 62

def preference_mode=(value)
  @current_preference_mode = PREFERENCE_MODES[value] || value
end

.serialize(key, value) ⇒ Object



39
40
41
42
# File 'lib/project/PackingPeanut.rb', line 39

def serialize(key, value)
  json = JSONObject.new
  json.put(key, value)
end

.storage_fileObject



58
59
60
# File 'lib/project/PackingPeanut.rb', line 58

def storage_file
  @persistence_storage_file ||= "default_persistence_file"
end

.storage_file=(value) ⇒ Object



54
55
56
# File 'lib/project/PackingPeanut.rb', line 54

def storage_file=(value)
  @persistence_storage_file = value
end