Class: KeyStore::Key

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/key_store/key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, roles: nil, notes: nil) ⇒ Key

Returns a new instance of Key.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/key_store/key.rb', line 12

def initialize(name, roles: nil, notes: nil)
  @name = name.to_s

  if exists?(name)
    set_roles_from_file if roles.nil?
    set_notes_from_file if notes.nil?
  end

  @roles ||= (roles || []).map(&:to_s)
  @notes ||= notes.to_s
end

Instance Attribute Details

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



8
9
10
# File 'lib/key_store/key.rb', line 8

def name
  @name
end

#notesObject

Returns the value of attribute notes.



9
10
11
# File 'lib/key_store/key.rb', line 9

def notes
  @notes
end

#rolesObject (readonly)

Returns the value of attribute roles.



8
9
10
# File 'lib/key_store/key.rb', line 8

def roles
  @roles
end

Instance Method Details

#http_headerObject



24
25
26
# File 'lib/key_store/key.rb', line 24

def http_header
  "Authorization: Token token=\"#{name}\""
end

#save!Object



28
29
30
31
32
33
34
35
# File 'lib/key_store/key.rb', line 28

def save!
  file.transaction do |f|
    f[name] ||= {}
    f[name][:roles] = roles.map(&:to_s)
    f[name][:notes] = notes.to_s
    f[name][:http_header] = http_header
  end
end