Class: NSCoder

Inherits:
Object show all
Defined in:
lib/cocoa/sugarcube-nscoder/nscoder.rb,
lib/ios/sugarcube-nscoder/nscoder.rb,
lib/osx/sugarcube-nscoder/nscoder.rb,
lib/cocoa/sugarcube-nscoder/nscoder.rb

Overview

Hash-like access for NSCoder. Converts the ‘key` argument to a String, because Symbols can mess things up.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.archive(root, to_file: file) ⇒ Object



58
59
60
# File 'lib/cocoa/sugarcube-nscoder/nscoder.rb', line 58

def self.archive(root)
  NSKeyedArchiver.archivedDataWithRootObject(root)
end

.unarchive(data_or_file) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/cocoa/sugarcube-nscoder/nscoder.rb', line 66

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

Instance Method Details

#[](key) ⇒ Object



5
6
7
# File 'lib/cocoa/sugarcube-nscoder/nscoder.rb', line 5

def [] key
  self.decodeObjectForKey(key.to_s)
end

#[]=(key, value) ⇒ Object



9
10
11
# File 'lib/cocoa/sugarcube-nscoder/nscoder.rb', line 9

def []= key, value
  self.encodeObject(value, forKey: key.to_s)
end

#bool(key) ⇒ Object



17
18
19
# File 'lib/cocoa/sugarcube-nscoder/nscoder.rb', line 17

def bool(key)
  self.decodeBoolForKey(key.to_s)
end

#double(key) ⇒ Object



21
22
23
# File 'lib/cocoa/sugarcube-nscoder/nscoder.rb', line 21

def double(key)
  self.decodeDoubleForKey(key.to_s)
end

#float(key) ⇒ Object



25
26
27
# File 'lib/cocoa/sugarcube-nscoder/nscoder.rb', line 25

def float(key)
  self.decodeFloatForKey(key.to_s)
end

#int(key) ⇒ Object



29
30
31
# File 'lib/cocoa/sugarcube-nscoder/nscoder.rb', line 29

def int(key)
  self.decodeIntegerForKey(key.to_s)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/cocoa/sugarcube-nscoder/nscoder.rb', line 13

def key?(key)
  self.containsValueForKey(key.to_s)
end

#point(key) ⇒ Object



3
4
5
# File 'lib/ios/sugarcube-nscoder/nscoder.rb', line 3

def point(key)
  CGPointFromString(self.decodeObjectForKey(key.to_s))
end

#rect(key) ⇒ Object



7
8
9
# File 'lib/ios/sugarcube-nscoder/nscoder.rb', line 7

def rect(key)
  CGRectFromString(self.decodeObjectForKey(key.to_s))
end

#set(key, toInt: value) ⇒ Object



15
16
17
18
# File 'lib/ios/sugarcube-nscoder/nscoder.rb', line 15

def set(key, toPoint: value)
  self.encodeObject(NSStringFromCGPoint(value), forKey: key.to_s)
  self
end

#size(key) ⇒ Object



11
12
13
# File 'lib/ios/sugarcube-nscoder/nscoder.rb', line 11

def size(key)
  CGSizeFromString(self.decodeObjectForKey(key.to_s))
end