Created
November 24, 2012 20:18
-
-
Save defvol/4141258 to your computer and use it in GitHub Desktop.
Makes any NSObject conform to the NSCoding protocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Makes any NSObject conform to the NSCoding protocol | |
# Authors: | |
# @rod_wilhelmy | |
# @cicloid | |
module Serializable | |
def attr_accessor_setters | |
methods.grep(/\w=:$/) | |
end | |
def initWithCoder(coder) | |
self.init | |
attr_accessor_setters.each do |method| | |
key = method.to_s.sub('=:', '') | |
self.send(method, coder.decodeObjectForKey(key)) | |
end | |
self | |
end | |
def encodeWithCoder(coder) | |
attr_accessor_setters.each do |method| | |
key = method.to_s.sub('=:', '') | |
coder.encodeObject(self.send(key), forKey:key) | |
end | |
end | |
end |
Cool!
A module system for the C family: http://llvm.org/devmtg/2012-11/Gregor-Modules.pdf?=submit
That is brilliant, although took me a while to understand exactly how it works! Didn't realise setters were returned as example_setter:= by the methods method
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: