Module: KBSecret::Record

Defined in:
lib/kbsecret/record.rb,
lib/kbsecret/record/todo.rb,
lib/kbsecret/record/login.rb,
lib/kbsecret/record/snippet.rb,
lib/kbsecret/record/abstract.rb,
lib/kbsecret/record/environment.rb,
lib/kbsecret/record/unstructured.rb

Overview

The namespace for kbsecret records types.

Defined Under Namespace

Classes: Abstract, Environment, Login, Snippet, Todo, Unstructured

Class Method Summary collapse

Class Method Details

.class_for(type) ⇒ Class?

Returns the record class corresponding to the given type.

Parameters:

  • type (String, Symbol)

    the record type

Returns:

  • (Class, nil)

    the record class corresponding to the given type



27
28
29
# File 'lib/kbsecret/record.rb', line 27

def self.class_for(type)
  record_classes.find { |c| c.type == type.to_sym }
end

.load_record!(session, path) ⇒ Record::AbstractRecord

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 a record by path into the given session.

Parameters:

  • session (Session)

    the session to load into

  • path (String)

    the fully-qualified record path

Returns:

  • (Record::AbstractRecord)

    the loaded record



43
44
45
46
47
# File 'lib/kbsecret/record.rb', line 43

def self.load_record!(session, path)
  hsh   = JSON.parse(File.read(path), symbolize_names: true)
  klass = record_classes.find { |c| c.type == hsh[:type].to_sym }
  klass&.load!(session, hsh)
end

.record_classesArray<Class>

Returns the class objects of all non-abstract record types.

Returns:

  • (Array<Class>)

    the class objects of all non-abstract record types



14
15
16
17
18
# File 'lib/kbsecret/record.rb', line 14

def self.record_classes
  klasses = constants.map(&Record.method(:const_get)).grep(Class)
  klasses.delete(Record::Abstract)
  klasses
end

.record_typesArray<Symbol>

Returns the types of all records.

Returns:

  • (Array<Symbol>)

    the types of all records



21
22
23
# File 'lib/kbsecret/record.rb', line 21

def self.record_types
  record_classes.map(&:type)
end

.type?(type) ⇒ Boolean

Returns whether a record class exists of the given type.

Parameters:

  • type (String, Symbol)

    the record type

Returns:

  • (Boolean)

    whether a record class exists of the given type



33
34
35
36
# File 'lib/kbsecret/record.rb', line 33

def self.type?(type)
  return false unless type
  record_types.include?(type.to_sym)
end