module PackingPeanut
Persistence module built on top of NSUserDefaults
Constants
- JSONObject
- MODE_MULTI_PROCESS
- MODE_PRIVATE
- MODE_WORLD_READABLE
- MODE_WORLD_WRITEABLE
- PREFERENCE_MODES
Public Instance Methods
[](key)
click to toggle source
# File lib/PackingPeanut-android/PackingPeanut.rb, line 34 def [](key) # Let's play nice with strings and non-strings string_key = key.to_s json_string = get_value(string_key) return nil if json_string == "" deserialize(string_key, json_string) end
[]=(key, value)
click to toggle source
Serialize key/value as json then store that string with the settings key == json key
# File lib/PackingPeanut-android/PackingPeanut.rb, line 24 def []=(key, value) # Let's play nice with strings and non-strings string_key = key.to_s settings = get_settings editor = settings.edit json = serialize(string_key,value) editor.putString(string_key, json.toString) editor.commit end
all()
click to toggle source
# File lib/PackingPeanut-android/PackingPeanut.rb, line 71 def all settings = get_settings all_hashes = settings.getAll.map { |key, value| Moran.parse(value) } # Currently an array of hashes, needs to be one big hash merged_hashes = {} all_hashes.each do |h| merged_hashes = merged_hashes.merge(h) end merged_hashes end
app_key()
click to toggle source
# File lib/PackingPeanut-ios/PackingPeanut.rb, line 5 def app_key @app_key ||= NSBundle.mainBundle.bundleIdentifier end
context=(supplied_context)
click to toggle source
attr_accessor is not supported for modules in RMAndroid… yet.
# File lib/PackingPeanut-android/PackingPeanut.rb, line 113 def context= supplied_context @context = supplied_context end
current_context()
click to toggle source
Allows us to use this from anywhere by setting the context Useful when you want to access this module from the REPL
# File lib/PackingPeanut-android/PackingPeanut.rb, line 106 def current_context # THIS SHOULD HAVE SAFE FAILOVERS # BUT defined? is causing erros in RMA native methods as of this commit @context end
delete(key)
click to toggle source
# File lib/PackingPeanut-android/PackingPeanut.rb, line 84 def delete(key) settings = get_settings string_key = key.to_s editor = settings.edit editor.remove(string_key) editor.commit end
delete_all!()
click to toggle source
# File lib/PackingPeanut-android/PackingPeanut.rb, line 92 def delete_all! settings = get_settings editor = settings.edit editor.clear() editor.commit end
deserialize(key, json_string)
click to toggle source
# File lib/PackingPeanut-android/PackingPeanut.rb, line 46 def deserialize(key, json_string) Moran.parse(json_string)[key] end
get_settings()
click to toggle source
# File lib/PackingPeanut-android/PackingPeanut.rb, line 99 def get_settings raise "[PackingPeanut] Fatal Error - You must set the context before accessing persistent data." unless @context current_context.getSharedPreferences(storage_file, preference_mode) end
get_value(key)
click to toggle source
# File lib/PackingPeanut-android/PackingPeanut.rb, line 50 def get_value key settings = get_settings settings.getString(key,nil) end
merge(values)
click to toggle source
# File lib/PackingPeanut-ios/PackingPeanut.rb, line 32 def merge(values) values.each do |key, value| storage.setObject(value, forKey: storage_key(key)) end storage.synchronize end
preference_mode()
click to toggle source
# File lib/PackingPeanut-android/PackingPeanut.rb, line 67 def preference_mode @current_preference_mode ||= MODE_PRIVATE end
preference_mode=(value)
click to toggle source
# File lib/PackingPeanut-android/PackingPeanut.rb, line 63 def preference_mode=(value) @current_preference_mode = PREFERENCE_MODES[value] || value end
serialize(key, value)
click to toggle source
# File lib/PackingPeanut-android/PackingPeanut.rb, line 42 def serialize(key, value) Moran.generate({:"#{key}" => value}) end
storage()
click to toggle source
# File lib/PackingPeanut-ios/PackingPeanut.rb, line 53 def storage NSUserDefaults.standardUserDefaults end
storage_file()
click to toggle source
# File lib/PackingPeanut-android/PackingPeanut.rb, line 59 def storage_file @persistence_storage_file ||= "default_persistence_file" end
storage_file=(value)
click to toggle source
# File lib/PackingPeanut-android/PackingPeanut.rb, line 55 def storage_file=(value) @persistence_storage_file = value end
storage_key(key)
click to toggle source
# File lib/PackingPeanut-ios/PackingPeanut.rb, line 57 def storage_key(key) "#{app_key}_#{storage_file}_#{key}" end