Skip to content

Simple, Clean Persistence in Rubymotion with motion-persistable

Gavin Morrice

Written by Gavin Morrice on June 11th, 2014 13:16

While working on a current iOS app I was using the very useful BubbleWrap gem for persisting values within a few classes. While BubbleWrap is great, but I found that having to call `App::Persistence[]` throughout my app wasn't ideal.

For one, using strings as the persistence keys is not DRY - you change the key in one place, you have to change it all over. `motion-persistable` is a simple gem that provides a wrapper around BubbleWrap's persistence (which uses NSUserDefaults under the hood) to add attribute accessor macros for persistable attributes. 

Using it is as simple as:

class User include Motion::Persistable 

  attr_accessor :name 

  # This is the motion-persistable part 

  attr_persisted :email 


@user ="bodacious") = "[email protected]" 

# ... later 

@user ="bodacious") # => "[email protected]"

For class methods: 

class User 

  class << self 

  include Motion::Persistable 

  attr_persisted :use_count, 0

  attr_persisted :email, "[default]" do |value| 

   # This block is executed when a new value is set    

   mixpanel.people.set(email: value) 



end = "bodacious" # => "bodacious" 

The gem is available on Github

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more