Class: Keychain::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/keychain/scope.rb

Overview

A scope that represents the search for a keychain item

Instance Method Summary collapse

Constructor Details

#initialize(kind, keychain = nil) ⇒ Scope

Returns a new instance of Scope.



5
6
7
8
9
10
# File 'lib/keychain/scope.rb', line 5

def initialize(kind, keychain=nil)
  @kind = kind
  @limit = nil
  @keychains = [keychain].compact
  @conditions = {}
end

Instance Method Details

#allArray

Returns an array containing all of the matching items

Returns:

  • (Array)

    The matching items. May be empty



69
70
71
72
73
# File 'lib/keychain/scope.rb', line 69

def all
  query = to_query
  query[Sec::Search::LIMIT] = @limit ? @limit.to_cf : Sec::Search::ALL
  execute query
end

#create(attributes) ⇒ Keychain::Item

Creates a new keychain item

Parameters:

  • attributes (Hash)

    options to create the item with

Options Hash (attributes):

  • :account (String)

    The account (user name)

  • :comment (String)

    A free text comment about the item

  • :creator (Integer)

    the item’s creator, as the unsigned integer representation of a 4 char code

  • :generic (String)

    generic passwords can have a generic data attribute

  • :invisible (String)

    whether the item should be invisible

  • :negative (String)

    A negative item records that the user decided to never save a password

  • :label (String)

    A label for the item (Shown in keychain access)

  • :path (String)

    The path the password is associated with (internet passwords only)

  • :port (String)

    The path the password is associated with (internet passwords only)

  • :port (String)

    The protocol the password is associated with (internet passwords only) Should be one of the constants at Keychain::Protocols

  • :domain (String)

    the domain the password is associated with (internet passwords only)

  • :server (String)

    the host name the password is associated with (internet passwords only)

  • :service (String)

    the service the password is associated with (generic passwords only)

  • :type (Integer)

    the item’s type, as the unsigned integer representation of a 4 char code

Returns:



95
96
97
98
99
# File 'lib/keychain/scope.rb', line 95

def create(attributes)
  raise "You must specify a password" unless attributes[:password]

  Keychain::Item.new(attributes.merge(:klass => @kind)).save!(:keychain => @keychains.first)
end

#firstKeychain::Item?

Returns the first matching item in the scope

Returns:



61
62
63
64
# File 'lib/keychain/scope.rb', line 61

def first
  query = to_query
  execute(query).first
end

#in(*keychains) ⇒ Keychain::Scope

Set the list of keychains to search

Parameters:

Returns:

  • (Keychain::Scope)

    Returns self as a convenience. The scope is modified in place



53
54
55
56
# File 'lib/keychain/scope.rb', line 53

def in *keychains
  @keychains = keychains.flatten
  self
end

#limit(value) ⇒ Keychain::Scope

Sets the number of items returned by the scope

Parameters:

  • value (Integer)

    The maximum number of items to return

Returns:

  • (Keychain::Scope)

    Returns self as a convenience. The scope is modified in place



44
45
46
47
# File 'lib/keychain/scope.rb', line 44

def limit value
  @limit = value
  self
end

#where(conditions) ⇒ Keychain::Scope

Adds conditions to the scope. Conditions are merged with any previously defined conditions.

The set of possible keys for conditions is given by Sec::ATTR_MAP.values. The legal values for the :protocol key are the constants in Keychain::Protocols

Parameters:

  • conditions (Hash)

    options to create the item with

Options Hash (conditions):

  • :account (String)

    The account (user name)

  • :comment (String)

    A free text comment about the item

  • :creator (Integer)

    the item’s creator, as the unsigned integer representation of a 4 char code

  • :generic (String)

    generic passwords can have a generic data attribute

  • :invisible (String)

    whether the item should be invisible

  • :negative (String)

    A negative item records that the user decided to never save a password

  • :label (String)

    A label for the item (Shown in keychain access)

  • :path (String)

    The path the password is associated with (internet passwords only)

  • :port (String)

    The path the password is associated with (internet passwords only)

  • :port (String)

    The protocol the password is associated with (internet passwords only) Should be one of the constants at Keychain::Protocols

  • :domain (String)

    the domain the password is associated with (internet passwords only)

  • :server (String)

    the host name the password is associated with (internet passwords only)

  • :service (String)

    the service the password is associated with (generic passwords only)

  • :type (Integer)

    the item’s type, as the unsigned integer representation of a 4 char code

Returns:

  • (Keychain::Scope)

    Returns self as a convenience. The scope is modified in place



35
36
37
38
# File 'lib/keychain/scope.rb', line 35

def where(conditions)
  @conditions.merge! conditions
  self
end