Class: Plum::ContentSourceRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/plum/content_source_registry.rb

Constant Summary collapse

RESERVED_HANDLES =
%w[entry entries forms globals nav site].freeze
HANDLE_PATTERN =
/\A[a-z][a-z0-9_]*\z/

Instance Method Summary collapse

Constructor Details

#initializeContentSourceRegistry

Returns a new instance of ContentSourceRegistry.



10
11
12
# File 'lib/plum/content_source_registry.rb', line 10

def initialize
  @sources = {}
end

Instance Method Details

#clearObject



28
29
30
# File 'lib/plum/content_source_registry.rb', line 28

def clear
  @sources.clear
end

#handlesObject



24
25
26
# File 'lib/plum/content_source_registry.rb', line 24

def handles
  @sources.keys
end

#register(handle, source = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
# File 'lib/plum/content_source_registry.rb', line 14

def register(handle, source = nil, &block)
  raise ArgumentError, "Provide a source or a block, not both" if source && block
  raise ArgumentError, "Content source is required" unless source || block

  normalized_handle = normalize_handle(handle)
  validate_handle!(normalized_handle)

  @sources[normalized_handle] = source || block
end

#to_liquid_context(controller:, site:) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/plum/content_source_registry.rb', line 32

def to_liquid_context(controller:, site:)
  context = ContentSourceContext.new(controller: controller, site: site)

  @sources.each_with_object({}) do |(handle, source), hash|
    hash[handle] = resolve_source(handle, source, context)
  end
end