Module: App::Persistence

Defined in:
lib/PackingPeanut-ios/PackingPeanut.rb,
lib/PackingPeanut-android/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



23
24
25
26
27
28
29
30
31
# File 'lib/PackingPeanut-ios/PackingPeanut.rb', line 23

def [](key)
  value = storage.objectForKey storage_key(key)

  # RubyMotion currently has a bug where the strings returned from
  # standardUserDefaults are missing some methods (e.g. to_data).
  # And because the returned object is slightly different than a normal
  # String, we can't just use `value.is_a?(String)`
  value.class.to_s == 'String' ? value.dup : value
end

.[]=(key, value) ⇒ Object

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



25
26
27
28
# File 'lib/PackingPeanut-android/PackingPeanut.rb', line 25

def []=(key, value)
  storage.setObject(value, forKey: storage_key(key))
  storage.synchronize
end

.allObject



55
56
57
58
59
60
61
62
# File 'lib/PackingPeanut-ios/PackingPeanut.rb', line 55

def all
  hash = storage.dictionaryRepresentation.select{|k,v| k.start_with?("#{app_key}_#{storage_file}") }
  new_hash = {}
  hash.each do |k,v|
    new_hash[k.sub("#{app_key}_#{storage_file}_", '')] = v
  end
  new_hash
end

.app_keyObject



6
7
8
# File 'lib/PackingPeanut-ios/PackingPeanut.rb', line 6

def app_key
  @app_key ||= NSBundle.mainBundle.bundleIdentifier
end

.context=(supplied_context) ⇒ Object

attr_accessor is not supported for modules in RMAndroid… yet.



97
98
99
# File 'lib/PackingPeanut-android/PackingPeanut.rb', line 97

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



91
92
93
94
# File 'lib/PackingPeanut-android/PackingPeanut.rb', line 91

def current_context
  #p defined? getApplicationContext
  @context || getApplicationContext
end

.delete(key) ⇒ Object



40
41
42
43
44
45
# File 'lib/PackingPeanut-ios/PackingPeanut.rb', line 40

def delete(key)
  value = storage.objectForKey storage_key(key)
  storage.removeObjectForKey(storage_key(key))
  storage.synchronize
  value
end

.deserialize(key, json_string) ⇒ Object



47
48
49
# File 'lib/PackingPeanut-android/PackingPeanut.rb', line 47

def deserialize(key, json_string)
  Moran.parse(json_string)[key]
end

.get_settingsObject



85
86
87
# File 'lib/PackingPeanut-android/PackingPeanut.rb', line 85

def get_settings
  current_context.getSharedPreferences(storage_file, preference_mode)
end

.get_value(key) ⇒ Object



51
52
53
54
# File 'lib/PackingPeanut-android/PackingPeanut.rb', line 51

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

.merge(values) ⇒ Object



33
34
35
36
37
38
# File 'lib/PackingPeanut-ios/PackingPeanut.rb', line 33

def merge(values)
  values.each do |key, value|
    storage.setObject(value, forKey: storage_key(key))
  end
  storage.synchronize
end

.preference_modeObject



68
69
70
# File 'lib/PackingPeanut-android/PackingPeanut.rb', line 68

def preference_mode
  @current_preference_mode ||= MODE_PRIVATE
end

.preference_mode=(value) ⇒ Object



64
65
66
# File 'lib/PackingPeanut-android/PackingPeanut.rb', line 64

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

.serialize(key, value) ⇒ Object



43
44
45
# File 'lib/PackingPeanut-android/PackingPeanut.rb', line 43

def serialize(key, value)
  Moran.generate({:"#{key}" => value})
end

.storageObject



47
48
49
# File 'lib/PackingPeanut-ios/PackingPeanut.rb', line 47

def storage
  NSUserDefaults.standardUserDefaults
end

.storage_fileObject



10
11
12
# File 'lib/PackingPeanut-ios/PackingPeanut.rb', line 10

def storage_file
  @persistence_storage_file ||= "default_persistence_file"
end

.storage_file=(value) ⇒ Object



14
15
16
# File 'lib/PackingPeanut-ios/PackingPeanut.rb', line 14

def storage_file=(value)
  @persistence_storage_file = value
end

.storage_key(key) ⇒ Object



51
52
53
# File 'lib/PackingPeanut-ios/PackingPeanut.rb', line 51

def storage_key(key)
  "#{app_key}_#{storage_file}_#{key}"
end