module KBSecret::Record

The namespace for {KBSecret} record types.

Public Instance Methods

class_for(type) click to toggle source

@param type [String, Symbol] the record type @return [Class] the record class corresponding to the given type @raise [Exceptions::RecordTypeUnknownError] if the requested type is unknown

# 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) click to toggle source

Load a record by path into the given session. @param session [Session] the session to load into @param path [String] the fully-qualified record path @return [Record::AbstractRecord] the loaded record @raise [Exceptions::RecordLoadError] if an error occurs during record loading @api private

# 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_classes() click to toggle source

@return [Array<Class>] the class objects of all non-abstract record types

# 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_types() click to toggle source

@return [Array<Symbol>] the types of all records

# File lib/kbsecret/record.rb, line 25
def record_types
  record_classes.map(&:type)
end
type?(type) click to toggle source

@param type [String, Symbol] the record type @return [Boolean] whether a record class exists of the given type

# File lib/kbsecret/record.rb, line 40
def type?(type)
  return false unless type
  record_types.include?(type.to_sym)
end