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 record 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)

    the record class corresponding to the given type

Raises:



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

def class_for(type)
  klass = record_classes.find { |c| c.type == type.to_sym }
  raise Exceptions::RecordTypeUnknownError, type unless klass
  klass
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

Raises:



51
52
53
54
55
56
57
# File 'lib/kbsecret/record.rb', line 51

def load_record!(session, path)
  hsh   = JSON.parse(File.read(path), symbolize_names: true)
  klass = class_for hsh[:type]
  klass.load!(session, hsh)
rescue Exceptions::RecordTypeUnknownError, JSON::JSONError
  raise Exceptions::RecordLoadError, path
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



18
19
20
21
22
# File 'lib/kbsecret/record.rb', line 18

def 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



25
26
27
# File 'lib/kbsecret/record.rb', line 25

def 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



40
41
42
43
# File 'lib/kbsecret/record.rb', line 40

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