Class: Keychain::Scope
- Inherits:
-
Object
- Object
- Keychain::Scope
- Defined in:
- lib/keychain/scope.rb
Overview
A scope that represents the search for a keychain item
Instance Method Summary collapse
-
#all ⇒ Array
Returns an array containing all of the matching items.
-
#create(attributes) ⇒ Keychain::Item
Creates a new keychain item.
-
#first ⇒ Keychain::Item?
Returns the first matching item in the scope.
-
#in(*keychains) ⇒ Keychain::Scope
Set the list of keychains to search.
-
#initialize(kind, keychain = nil) ⇒ Scope
constructor
A new instance of Scope.
-
#limit(value) ⇒ Keychain::Scope
Sets the number of items returned by the scope.
-
#where(conditions) ⇒ Keychain::Scope
Adds conditions to the scope.
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] @conditions = {} end |
Instance Method Details
#all ⇒ Array
Returns an array containing all of the matching items
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
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 |
#first ⇒ Keychain::Item?
Returns the first matching item in the scope
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
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
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
35 36 37 38 |
# File 'lib/keychain/scope.rb', line 35 def where(conditions) @conditions.merge! conditions self end |