lib/cocoa/sugarcube-nscoder/nscoder.rb in sugarcube-2.8.2 vs lib/cocoa/sugarcube-nscoder/nscoder.rb in sugarcube-2.9.1
- old
+ new
@@ -1,74 +1,101 @@
+# Hash-like access for NSCoder. Converts the `key` argument to a String,
+# because Symbols can mess things up.
class NSCoder
def [] key
- return self.decodeObjectForKey key
+ self.decodeObjectForKey(key.to_s)
end
def []= key, value
- return self.encodeObject(value, forKey:key)
+ self.encodeObject(value, forKey: key.to_s)
end
+ def key?(key)
+ self.containsValueForKey(key.to_s)
+ end
+
def bool(key)
- return self.decodeBoolForKey(key)
+ self.decodeBoolForKey(key.to_s)
end
def double(key)
- return self.decodeDoubleForKey(key)
+ self.decodeDoubleForKey(key.to_s)
end
def float(key)
- return self.decodeFloatForKey(key)
+ self.decodeFloatForKey(key.to_s)
end
def int(key)
- return self.decodeIntegerForKey(key)
+ self.decodeIntegerForKey(key.to_s)
end
def point(key)
- return CGPointFromString(self.decodeObjectForKey(key))
+ CGPointFromString(self.decodeObjectForKey(key.to_s))
end
def rect(key)
- return CGRectFromString(self.decodeObjectForKey(key))
+ CGRectFromString(self.decodeObjectForKey(key.to_s))
end
def size(key)
- return CGSizeFromString(self.decodeObjectForKey(key))
+ CGSizeFromString(self.decodeObjectForKey(key.to_s))
end
- def set(key, toBool:value)
- self.encodeBool(value, forKey:key)
- return self
+ def set(key, toBool: value)
+ self.encodeBool(value, forKey: key.to_s)
+ self
end
- def set(key, toDouble:value)
- self.encodeDouble(value, forKey:key)
- return self
+ def set(key, toDouble: value)
+ self.encodeDouble(value, forKey: key.to_s)
+ self
end
- def set(key, toFloat:value)
- self.encodeFloat(value, forKey:key)
- return self
+ def set(key, toFloat: value)
+ self.encodeFloat(value, forKey: key.to_s)
+ self
end
- def set(key, toInt:value)
- self.encodeInteger(value, forKey:key)
- return self
+ def set(key, toInt: value)
+ self.encodeInteger(value, forKey: key.to_s)
+ self
end
- def set(key, toPoint:value)
- self.encodeObject(NSStringFromCGPoint(value), forKey:key)
- return self
+ def set(key, toPoint: value)
+ self.encodeObject(NSStringFromCGPoint(value), forKey: key.to_s)
+ self
end
- def set(key, toRect:value)
- self.encodeObject(NSStringFromCGRect(value), forKey:key)
- return self
+ def set(key, toRect: value)
+ self.encodeObject(NSStringFromCGRect(value), forKey: key.to_s)
+ self
end
- def set(key, toSize:value)
- self.encodeObject(NSStringFromCGSize(value), forKey:key)
- return self
+ def set(key, toSize: value)
+ self.encodeObject(NSStringFromCGSize(value), forKey: key.to_s)
+ self
+ end
+
+end
+
+
+class NSCoder
+
+ def self.archive(root)
+ NSKeyedArchiver.archivedDataWithRootObject(root)
+ end
+
+ def self.archive(root, to_file: file)
+ NSKeyedArchiver.archivedRootObject(root, toFile: file)
+ end
+
+ def self.unarchive(data_or_file)
+ if data_or_file.is_a?(NSData)
+ NSKeyedUnarchiver.unarchiveObjectWithData(data_or_file)
+ else
+ NSKeyedUnarchiver.unarchiveObjectWithFile(data_or_file)
+ end
end
end