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

Instance Attribute Summary

Attributes inherited from Base

#ptr

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

check_cftype, #eql?, #equals?, finalize, #hash, #initialize, #null?, #to_cf, typecast

Methods included from Memory

#inspect, #release, #retain, #to_ptr

Methods included from Register

included, #register_type

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)
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)
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
# 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)
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), Base.typecast(value)]
  end
  CF.CFDictionaryApplyFunction(self, callback, nil)
  self
end

#lengthInteger Also known as: size

Returns the number of key value pairs in the dictionary

Returns:

  • (Integer)


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

def length
  CF.CFDictionaryGetCount(self)
end

#merge!(other) ⇒ Object

Adds the key value pairs from the argument to self

Parameters:

Returns:

  • self



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

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:

  • (Hash)


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

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