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.
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#label ⇒ Symbol
readonly
The session's label.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
-
#add_record(type, label, *args) ⇒ void
Add a record to the session.
-
#delete_record(rec_label) ⇒ void
Delete a record from the session, if it exists.
-
#initialize(label: :default) ⇒ Session
constructor
A new instance of Session.
-
#record?(label) ⇒ Boolean
Whether or not the session contains a record with the given label.
-
#record_labels ⇒ Array<Symbol>
The labels of all records known to the 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.
18 19 20 21 22 23 24 |
# File 'lib/kbsecret/session.rb', line 18 def initialize(label: :default) @label = label @config = Config.session(label.to_sym) @directory = rel_path config[:root], mkdir: true @records = load_records! end |
Instance Attribute Details
#config ⇒ Hash (readonly)
Returns the session-specific configuration, from Config::CONFIG_FILE.
10 11 12 |
# File 'lib/kbsecret/session.rb', line 10 def config @config end |
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
11 12 13 |
# File 'lib/kbsecret/session.rb', line 11 def directory @directory end |
#label ⇒ Symbol (readonly)
Returns the session's label.
6 7 8 |
# File 'lib/kbsecret/session.rb', line 6 def label @label end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
12 13 14 |
# File 'lib/kbsecret/session.rb', line 12 def records @records end |
Instance Method Details
#add_record(type, label, *args) ⇒ void
This method returns an undefined value.
Add a record to the session.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/kbsecret/session.rb', line 40 def add_record(type, label, *args) klass = Record.record_classes.find { |k| k.type == type } arity = klass.instance_method(:initialize).arity - 2 unless arity == args.size raise RecordCreationArityError.new(arity, args.size) end record = klass.new(self, label, *args) records << record record.sync! end |
#delete_record(rec_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.
57 58 59 60 61 62 63 |
# File 'lib/kbsecret/session.rb', line 57 def delete_record(rec_label) record = records.find { |r| r.label == rec_label } return unless record File.delete(record.path) records.delete(record) end |
#record?(label) ⇒ Boolean
Returns whether or not the session contains a record with the given label.
67 68 69 |
# File 'lib/kbsecret/session.rb', line 67 def record?(label) record_labels.include?(label) end |
#record_labels ⇒ Array<Symbol>
Returns the labels of all records known to the session.
29 30 31 |
# File 'lib/kbsecret/session.rb', line 29 def record_labels records.map(&:label) end |