Class: KBSecret::Session
- Inherits:
-
Object
- Object
- KBSecret::Session
- Defined in:
- lib/kbsecret/session.rb
Overview
Represents a session of N keybase users with collective read/write access to a collection of records.
Instance Attribute Summary collapse
-
#config ⇒ Hash
readonly
The session-specific configuration, from Config::CONFIG_FILE.
-
#label ⇒ Symbol
readonly
The session's label.
-
#path ⇒ String
readonly
The fully-qualified path of the session.
Instance Method Summary collapse
-
#[](label) ⇒ Record::Abstract?
The record with the requested label, if extant.
-
#add_record(type, label, *args) ⇒ void
Add a record to the session.
-
#delete_record(label) ⇒ void
Delete a record from the session, if it exists.
-
#initialize(label: :default) ⇒ Session
constructor
A new instance of Session.
-
#load_records! ⇒ Array<Record::Abstract>
private
Load all records associated with the session.
-
#record?(label) ⇒ Boolean
Whether or not the session contains a record with the given label.
-
#record_labels ⇒ Array<String>
The labels of all records known to the session.
-
#record_paths ⇒ Array<String>
The fully qualified paths of all records in the session.
-
#records(type = nil) ⇒ Array<Record::Abstract>
All records (of a given type) in the session.
-
#rel_path(mkdir: false) ⇒ String
private
The fully qualified path to the session.
-
#unlink! ⇒ void
Delete the entire session.
Constructor Details
#initialize(label: :default) ⇒ Session
This does not create a new session, but loads one already specified in Config::CONFIG_FILE. To create a new session, see Config#configure_session.
Returns a new instance of Session.
24 25 26 27 28 29 30 31 32 |
# File 'lib/kbsecret/session.rb', line 24 def initialize(label: :default) @label = label.to_sym @config = Config.session(@label) raise Exceptions::SessionLoadError, "no users in session" if @config[:users].empty? @path = rel_path mkdir: true @records = load_records! end |
Instance Attribute Details
#config ⇒ Hash (readonly)
Returns the session-specific configuration, from Config::CONFIG_FILE.
14 15 16 |
# File 'lib/kbsecret/session.rb', line 14 def config @config end |
#label ⇒ Symbol (readonly)
Returns the session's label.
10 11 12 |
# File 'lib/kbsecret/session.rb', line 10 def label @label end |
#path ⇒ String (readonly)
Returns the fully-qualified path of the session.
17 18 19 |
# File 'lib/kbsecret/session.rb', line 17 def path @path end |
Instance Method Details
#[](label) ⇒ Record::Abstract?
Returns the record with the requested label, if extant.
36 37 38 |
# File 'lib/kbsecret/session.rb', line 36 def [](label) @records.find { |r| r.label == label.to_s } end |
#add_record(type, label, *args) ⇒ void
This method returns an undefined value.
Add a record to the session.
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/kbsecret/session.rb', line 67 def add_record(type, label, *args) klass = Record.class_for(type.to_sym) arity = klass.external_fields.length raise Exceptions::RecordCreationArityError.new(arity, args.size) unless arity == args.size body = klass.external_fields.zip(args).to_h record = klass.new(self, label.to_s, **body) records << record record.sync! end |
#delete_record(label) ⇒ void
This method returns an undefined value.
Delete a record from the session, if it exists. Does nothing if no such record can be found.
84 85 86 87 88 89 90 |
# File 'lib/kbsecret/session.rb', line 84 def delete_record(label) record = records.find { |r| r.label == label.to_s } return unless record File.delete(record.path) records.delete(record) end |
#load_records! ⇒ Array<Record::Abstract>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Load all records associated with the session.
116 117 118 119 120 |
# File 'lib/kbsecret/session.rb', line 116 def load_records! record_paths.map do |path| Record.load_record! self, path end end |
#record?(label) ⇒ Boolean
Returns whether or not the session contains a record with the given label.
95 96 97 |
# File 'lib/kbsecret/session.rb', line 95 def record?(label) record_labels.include?(label.to_s) end |
#record_labels ⇒ Array<String>
Returns the labels of all records known to the session.
54 55 56 |
# File 'lib/kbsecret/session.rb', line 54 def record_labels records.map(&:label) end |
#record_paths ⇒ Array<String>
Returns the fully qualified paths of all records in the session.
109 110 111 |
# File 'lib/kbsecret/session.rb', line 109 def record_paths Dir[File.join(path, "*.json")] end |
#records(type = nil) ⇒ Array<Record::Abstract>
All records (of a given type) in the session.
43 44 45 46 47 48 49 |
# File 'lib/kbsecret/session.rb', line 43 def records(type = nil) if type @records.select { |r| r.type == type.to_sym } else @records end end |
#rel_path(mkdir: false) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the fully qualified path to the session.
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/kbsecret/session.rb', line 125 def rel_path(mkdir: false) # /keybase/private/[u1,u2,...,uN]/kbsecret/[session] path = File.join(Keybase::Configuration::KBFS_MOUNT, "private", Keybase::U[@config[:users]], "kbsecret", @config[:root]) FileUtils.mkdir_p path if mkdir path end |
#unlink! ⇒ void
Use this with caution, as all files under the session path will be deleted. Furthermore, the session path itself will be deleted, and this object will become garbage.
This method returns an undefined value.
Delete the entire session.
104 105 106 |
# File 'lib/kbsecret/session.rb', line 104 def unlink! FileUtils.rm_rf(path) end |