Sha256: a46ae3158cd344b9168f16398ff0cb0010dbd0872bd82e8c1d5e62584a5f269b

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

# Persistence module built on top of NSUserDefaults
module BubbleWrap
  module Persistence
    module_function

    def app_key
      @app_key ||= BubbleWrap::App.identifier
    end

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

    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

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

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

    def storage
      NSUserDefaults.standardUserDefaults
    end

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

end
::Persistence = BubbleWrap::Persistence unless defined?(::Persistence)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bubble-wrap-1.6.0 motion/core/persistence.rb
bubble-wrap-1.6.0.rc1 motion/core/persistence.rb
bubble-wrap-1.5.0 motion/core/persistence.rb
bubble-wrap-1.5.0.rc1 motion/core/persistence.rb
bubble-wrap-1.4.0 motion/core/persistence.rb