module KManager::Documents::DocumentTaggable

Tag a document with attributes that can be used to uniquely identify a document within a project or namespace

Examples:

User DSL could be tagged user_entity
Blue print DSL for building a data layer User could be tagged user_blueprint
Users.csv file could be tagged data_files_users_csv
Account DSL in the CRM project could be tagged crm_account_entity
AccountController DSL in the CRM project could be tagged crm_controllers_account_controller

Attributes

error[R]

Errors in documents can be stored against the document

This helps debugging invalid DSL's and data documents

key[R]

Name of the document (required)

Examples: user, account, country

namespace[R]

Namespace of the document (optional, '' if not set)

When using a data file, this should be based on the relative file path When using a DSL data file, this will be manually configured

project[R]

Project that this document belongs to (optional)

project_key[R]

Project key is inferred from the attached project ('' if project not set)

resource[RW]

Resource that this document belongs to (optional)

type[R]

Type of document (optional, but will set to :default_document_type if not provided)

Examples by data type

:csv, :yaml, :json, :xml

Examples by shape of the data in a DSL

:entity, :microapp, blueprint

Public Instance Methods

debug() click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/k_manager/documents/document_taggable.rb, line 78
def debug
  log.section_heading(self.class.name)

  log.kv 'unique_key'   , unique_key  , 15
  log.kv 'key'          , key         , 15
  log.kv 'type'         , type        , 15
  log.kv 'namespace'    , namespace   , 15
  # log.kv 'resource'     , resource    , 15
  # log.kv 'project'      , project     , 15
  log.kv 'project_key'  , project_key , 15
  log.kv 'data'         , data.nil? ? '' : data.to_s[0..100].gsub("\n", '\n'), 15
  log.kv 'error'        , error       , 15
end
initialize_document_tags(**opts) click to toggle source
# File lib/k_manager/documents/document_taggable.rb, line 49
def initialize_document_tags(**opts)
  @key = opts[:key] || SecureRandom.alphanumeric(4)
  @type = opts[:type] || default_document_type
  @namespace = opts[:namespace] || ''
  @resource = opts[:resource]
  @project = @resource&.project
  @project_key = project&.infer_key
end
unique_key() click to toggle source

The unique key on resources provides a way to prevent conflicts between resource names, resource types, local namespaces and projects.

# File lib/k_manager/documents/document_taggable.rb, line 64
def unique_key
  return @unique_key if defined? @unique_key

  @unique_key = begin
    raise KDoc::Error, 'key is required when generating unique key' if key.nil? || key.empty?

    [project_key, namespace, key, type]
      .reject { |k| k.nil? || k == '' }
      .map { |k| k.to_s.gsub('_', '-') }
      .join('-')
  end
end