Class: Sec::Base Abstract

Inherits:
CF::Base
  • Object
show all
Defined in:
lib/keychain/sec.rb

Overview

This class is abstract.

The base class of all CF types from the security framework

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr) ⇒ Base

Returns a new instance of Base.



133
134
135
136
# File 'lib/keychain/sec.rb', line 133

def initialize(ptr)
  super
  @attributes = {}
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



113
114
115
# File 'lib/keychain/sec.rb', line 113

def attributes
  @attributes
end

Class Method Details

.define_attributes(attr_map) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/keychain/sec.rb', line 120

def self.define_attributes(attr_map)
  attr_map.values.each do |ruby_name|
    unless method_defined?(ruby_name)
      define_method ruby_name do
        self.attributes[ruby_name]
      end
      define_method ruby_name.to_s+'=' do |value|
        self.attributes[ruby_name] = value
      end
    end
  end
end

.register_type(type_name) ⇒ Object



115
116
117
118
# File 'lib/keychain/sec.rb', line 115

def self.register_type(type_name)
  Sec.attach_function "#{type_name}GetTypeID", [], CF.find_type(:cftypeid)
  @@type_map[Sec.send("#{type_name}GetTypeID")] = self
end

Instance Method Details

#keychainKeychain::Keychain

Returns the keychain the item is in

Returns:



150
151
152
153
154
155
# File 'lib/keychain/sec.rb', line 150

def keychain
  out = FFI::MemoryPointer.new :pointer
  status = Sec.SecKeychainItemCopyKeychain(self,out)
  Sec.check_osstatus(status)
  CF::Base.new(out.read_pointer).release_on_gc
end

#load_attributesObject



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/keychain/sec.rb', line 157

def load_attributes
  result = FFI::MemoryPointer.new :pointer
  status = Sec.SecItemCopyMatching({Sec::Query::SEARCH_LIST => [self.keychain],
                                    Sec::Query::ITEM_LIST => [self],
                                    Sec::Query::CLASS => self.klass,
                                    Sec::Query::RETURN_ATTRIBUTES => true,
                                    Sec::Query::RETURN_REF => false}.to_cf, result)
  Sec.check_osstatus(status)

  cf_dict = CF::Base.typecast(result.read_pointer).release_on_gc
  update_self_from_dictionary(cf_dict)
end

#update_self_from_dictionary(cf_dict) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/keychain/sec.rb', line 138

def update_self_from_dictionary(cf_dict)
  @attributes = cf_dict.inject({}) do |memo, (k,v)|
    if ruby_name = self.class::ATTR_MAP[k]
      memo[ruby_name] = v.to_ruby
    end
    memo
  end
end