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

If you enjoyed this, you might also like:

Page depositphotos 29329033 original

A Review of RubyMotion in 2015

Written by Gavin Morrice

Page depositphotos 60289999 original fix

Adding gender-specific messages to your rails app with genderize

Written by Gavin Morrice

Page 1 n d7rv2wlmhjkucnldwnfg

Why you should build your web startup using Ruby on Rails

Written by Gavin Morrice


We've Just Launched Pingr

Written by Gavin Morrice

Page boots

The Four Tenets of Bootstrapping a Tech Startup

Written by Gavin Morrice

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