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
end
@user = User.new("bodacious")
@user.email = "[email protected]"
# ... later
@user = User.new("bodacious")
@user.email # => "[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
end
end
User.email = "bodacious"
User.email # => "bodacious"
The gem is available on Github
RubyMotion is a toolchain that lets us build great iOS apps - faster! Here's the scoop...
Written by Gavin Morrice—Are you sick of creating in-line logic and methods to handle gender differences in your code? Maybe this will help...
Written by Gavin Morrice—There are dozens of web application development frameworks to choose from out there, and choosing one to build your business on can be daunting...
Written by Gavin Morrice—Don't let your site content changes go unnoticed by Google and other search engines
Written by Gavin Morrice—Learn to avoid the pitfalls that catch even the seasoned entrepreneurs
Written by Gavin Morrice—