Class: CF::Dictionary

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/corefoundation/dictionary.rb

Overview

Wrapper class for CFDictionary. It implements Enumerable.

Values returned by the accessor methods or yielded by the block are retained and marked as releasable on garbage collection This means you can safely use the returned values even if the CFDictionary itself has been destroyed

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

check_cftype, #eql?, #equals?, #hash, #initialize, #inspect, #null?, #ptr=, #release, #release_on_gc, #retain, #to_cf, #to_ptr, typecast

Constructor Details

This class inherits a constructor from CF::Base

Class Method Details

.mutableCF::Dictionary

Return a new, empty mutable CF::Dictionary

Returns:



48
49
50
# File 'lib/corefoundation/dictionary.rb', line 48

def self.mutable
  new(CF.CFDictionaryCreateMutable nil, 0, CF.kCFTypeDictionaryKeyCallBacks.to_ptr, CF.kCFTypeDictionaryValueCallBacks.to_ptr).release_on_gc
end

Instance Method Details

#[](key) ⇒ CF::Base?

Returns the value associated with the key, or nil The key should be a CF::Base instance. As a convenience, instances of string will be converted to CF::String

Parameters:

Returns:



69
70
71
72
73
74
# File 'lib/corefoundation/dictionary.rb', line 69

def [](key)
  key = CF::String.from_string(key) if key.is_a?(::String)
  self.class.check_cftype(key)
  raw = CF.CFDictionaryGetValue(self, key)
  raw.null? ? nil : self.class.typecast(raw).retain.release_on_gc
end

#[]=(key, value) ⇒ CF::Base

Sets the value associated with the key, or nil The key should be a CF::Base instance. As a convenience, instances of string will be converted to CF::String

Parameters:

Returns:



81
82
83
84
85
86
87
# File 'lib/corefoundation/dictionary.rb', line 81

def []=(key, value)
  key = CF::String.from_string(key) if key.is_a?(::String)
  self.class.check_cftype(key)
  self.class.check_cftype(value)
  CF.CFDictionarySetValue(self, key, value)
  value
end

#eachObject

Iterates over the array yielding the value to the block The value is wrapped in the appropriate CF::Base subclass and retained (but marked for releasing upon garbage collection)

Returns:

  • self



56
57
58
59
60
61
62
# File 'lib/corefoundation/dictionary.rb', line 56

def each
  callback = lambda do |key, value, _|
    yield [Base.typecast(key).retain.release_on_gc, Base.typecast(value).retain.release_on_gc]
  end
  CF.CFDictionaryApplyFunction(self, callback, nil)
  self
end

#lengthInteger Also known as: size

Returns the number of key value pairs in the dictionary

Returns:



102
103
104
# File 'lib/corefoundation/dictionary.rb', line 102

def length
  CF.CFDictionaryGetCount(self)
end

#merge!(other) ⇒ Object

Adds the key value pairs from the argument to self

Parameters:

Returns:

  • self



93
94
95
96
97
98
# File 'lib/corefoundation/dictionary.rb', line 93

def merge!(other)
  other.each do |k,v|
    self[k] = v
  end
  self
end

#to_rubyHash

Returns a ruby hash constructed by calling ‘to_ruby` on each key and value

Returns:



109
110
111
# File 'lib/corefoundation/dictionary.rb', line 109

def to_ruby
  Hash[collect {|k,v| [k.to_ruby, v.to_ruby]}]
end