Module: ChaWork::Persistence
- Defined in:
- lib/cha_work/persistence.rb
Class Method Summary collapse
- .[](key) ⇒ Object
- .[]=(key, value) ⇒ Object
- .all ⇒ Object
- .app_key ⇒ Object
- .delete(key) ⇒ Object
- .merge(values) ⇒ Object
- .storage ⇒ Object
- .storage_key(key) ⇒ Object
Class Method Details
.[](key) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/cha_work/persistence.rb', line 15 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
10 11 12 13 |
# File 'lib/cha_work/persistence.rb', line 10 def []=(key, value) storage.setObject(value, forKey: storage_key(key)) storage.synchronize end |
.all ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/cha_work/persistence.rb', line 47 def all hash = storage.dictionaryRepresentation.select{|k,v| k.start_with?(app_key) } new_hash = {} hash.each do |k,v| new_hash[k.sub("#{app_key}_", '')] = v end new_hash end |
.app_key ⇒ Object
6 7 8 |
# File 'lib/cha_work/persistence.rb', line 6 def app_key @app_key ||= NSBundle.mainBundle.bundleIdentifier end |
.delete(key) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/cha_work/persistence.rb', line 32 def delete(key) value = storage.objectForKey storage_key(key) storage.removeObjectForKey(storage_key(key)) storage.synchronize value end |
.merge(values) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/cha_work/persistence.rb', line 25 def merge(values) values.each do |key, value| storage.setObject(value, forKey: storage_key(key)) end storage.synchronize end |
.storage ⇒ Object
39 40 41 |
# File 'lib/cha_work/persistence.rb', line 39 def storage NSUserDefaults.standardUserDefaults end |
.storage_key(key) ⇒ Object
43 44 45 |
# File 'lib/cha_work/persistence.rb', line 43 def storage_key(key) "#{app_key}_#{key}" end |