Class: ContentBlockTools::ContentBlockReference

Inherits:
Data
  • Object
show all
Defined in:
lib/content_block_tools/content_block_reference.rb,
lib/content_block_tools/content_block_reference.rb

Overview

Defines a reference pointer for a Content Block

Constant Summary collapse

SUPPORTED_DOCUMENT_TYPES =

An array of the supported document types

%w[contact content_block_email_address content_block_postal_address content_block_pension].freeze
UUID_REGEX =

The regex used to find UUIDs

/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/
FIELD_REGEX =

The regex to find optional field names after the UUID, begins with '/'

/(\/[a-z0-9_\-\/]*)?/
EMBED_REGEX =

The regex used when scanning a document using find_all_in_document

/({{embed:(#{SUPPORTED_DOCUMENT_TYPES.join('|')}):#{UUID_REGEX}#{FIELD_REGEX}}})/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#content_id ⇒ String (readonly)

The content UUID for a block

Examples:

content_block_reference.id #=> "2b92cade-549c-4449-9796-e7a3957f3a86"

Returns:

  • (String)


27
28
29
# File 'lib/content_block_tools/content_block_reference.rb', line 27

def content_id
  @content_id
end

#document_type ⇒ String (readonly)

The document type of the content block - this will be used to work out which Presenter will be used to render the content block. All supported document_types are documented in SUPPORTED_DOCUMENT_TYPES

Examples:

content_block_reference.document_type #=> "content_block_email_address"

Returns:

  • (String) —

    the document type



27
28
29
# File 'lib/content_block_tools/content_block_reference.rb', line 27

def document_type
  @document_type
end

#embed_code ⇒ String (readonly)

The embed_code used for a block

Examples:

content_block_reference.embed_code #=> "{{embed:content_block_email_address:2b92cade-549c-4449-9796-e7a3957f3a86}}"

Returns:

  • (String)


27
28
29
# File 'lib/content_block_tools/content_block_reference.rb', line 27

def embed_code
  @embed_code
end

Class Method Details

.find_all_in_document(document) ⇒ Array<ContentBlockReference>

Finds all content block references within a document, using ContentBlockReference::EMBED_REGEX to scan through the document

Returns:



42
43
44
45
46
# File 'lib/content_block_tools/content_block_reference.rb', line 42

def find_all_in_document(document)
  document.scan(ContentBlockReference::EMBED_REGEX).map do |match|
    ContentBlockReference.new(document_type: match[1], content_id: match[2], embed_code: match[0])
  end
end