Class: Keytaker::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/keytaker/item.rb

Constant Summary collapse

SERVICE_NAME =
'ruby-keytaker'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ Item

Returns a new instance of Item.



29
30
31
# File 'lib/keytaker/item.rb', line 29

def initialize(item)
  @item = item
end

Class Method Details

.create(key:, value:) ⇒ Object



24
25
26
# File 'lib/keytaker/item.rb', line 24

def create(key:, value:)
  Keychain.generic_passwords.create(service: SERVICE_NAME, account: key, password: value)
end

.find(key) ⇒ Object



9
10
11
12
# File 'lib/keytaker/item.rb', line 9

def find(key)
  item = Keychain.generic_passwords.where(service: SERVICE_NAME, account: key).first
  item ? new(item) : nil
end

.selectObject



14
15
16
17
18
19
20
21
22
# File 'lib/keytaker/item.rb', line 14

def select
  accounts = Keychain.generic_passwords.where(service: SERVICE_NAME).all.map(&:account)
  if accounts.empty?
    nil
  else
    key, _ = *PecoSelector.select_from(accounts)
    Keytaker::Item.find(key)
  end
end

Instance Method Details

#copyObject



33
34
35
36
37
# File 'lib/keytaker/item.rb', line 33

def copy
  IO.popen('pbcopy', 'w') do |f|
    f << @item.password
  end
end

#deleteObject



44
45
46
# File 'lib/keytaker/item.rb', line 44

def delete
  @item.delete
end

#update(value:) ⇒ Object



39
40
41
42
# File 'lib/keytaker/item.rb', line 39

def update(value:)
  @item.password = value
  @item.save!
end