Class: Pageflow::EntryStructuredDataTypes

Inherits:
Object
  • Object
show all
Defined in:
lib/pageflow/entry_structured_data_types.rb

Overview

Registry for structured data types that can be used in entries.

Instance Method Summary collapse

Constructor Details

#initializeEntryStructuredDataTypes

Returns a new instance of EntryStructuredDataTypes.



4
5
6
7
# File 'lib/pageflow/entry_structured_data_types.rb', line 4

def initialize
  @types = {}
  @default_name = nil
end

Instance Method Details

#for(entry) ⇒ Object

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.

API:

  • private



36
37
38
39
40
41
# File 'lib/pageflow/entry_structured_data_types.rb', line 36

def for(entry)
  structured_data_type =
    @types[entry.structured_data_type_name] || @types[@default_name]

  structured_data_type ? structured_data_type.call(entry) : {}
end

#namesObject

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.

API:

  • private



31
32
33
# File 'lib/pageflow/entry_structured_data_types.rb', line 31

def names
  @types.keys
end

#register(name, callable, options = {}) ⇒ Object

Register a structured data type for entries.

Examples:

config.entry_structured_data_types.register(:video_object, lambda do |entry|
  {
    '@type' => 'VideoObject',
    videoSection: 'documentary'
  }
end, default: true)

Parameters:

  • The name of the structured data type

  • A lambda that receives a PublishedEntry and returns a hash of structured data properties including @type

  • (defaults to: {})

    Optional configuration

Options Hash (options):

  • :default (Boolean) — default: false

    Set this type as the default when no structured_data_type_name is specified on the entry



25
26
27
28
# File 'lib/pageflow/entry_structured_data_types.rb', line 25

def register(name, callable, options = {})
  @types[name.to_s] = callable
  @default_name = name.to_s if options[:default]
end