Module: WCC::Contentful::Middleware::Store

Extended by:
ActiveSupport::Concern
Includes:
Store::Interface
Included in:
CachingMiddleware, LocaleMiddleware, Store::InstrumentationMiddleware
Defined in:
lib/wcc/contentful/middleware/store.rb,
lib/wcc/contentful/middleware/store/locale_middleware.rb,
lib/wcc/contentful/middleware/store/caching_middleware.rb

Overview

A Store middleware wraps the Store interface to perform any desired transformations on the Contentful entries coming back from the store. A Store middleware must implement the Store interface as well as a ‘store=` attribute writer, which is used to inject the next store or middleware in the chain.

The Store interface can be seen on the WCC::Contentful::Store::Base class. It consists of the ‘#find, #find_by, #find_all, #set, #delete,` and `#index` methods.

Including this concern will define those methods to pass through to the next store. Any of those methods can be overridden on the implementing middleware. It will also expose two overridable methods, ‘#select?` and `#transform`. These methods are applied when reading values out of the store, and can be used to apply a filter or transformation to each entry in the store.

Defined Under Namespace

Classes: CachingMiddleware, DelegatingQuery, LocaleMiddleware

Constant Summary

Constants included from Store::Interface

Store::Interface::INTERFACE_METHODS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Store::Interface

#index, #index?

Instance Attribute Details

#storeObject

Returns the value of attribute store.



22
23
24
# File 'lib/wcc/contentful/middleware/store.rb', line 22

def store
  @store
end

Instance Method Details

#find(id, **options) ⇒ Object



34
35
36
37
# File 'lib/wcc/contentful/middleware/store.rb', line 34

def find(id, **options)
  found = store.find(id, **options)
  return transform(found, options) if found && (!has_select? || select?(found, options))
end

#find_all(options: nil, **args) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/wcc/contentful/middleware/store.rb', line 48

def find_all(options: nil, **args)
  options ||= {}
  DelegatingQuery.new(
    store.find_all(**args.merge(options: options)),
    middleware: self,
    options: options
  )
end

#find_by(options: nil, **args) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/wcc/contentful/middleware/store.rb', line 39

def find_by(options: nil, **args)
  options ||= {}
  result = store.find_by(**args.merge(options: options))
  return unless result && (!has_select? || select?(result, options))

  result = resolve_includes(result, options[:include], options) if options && options[:include]
  transform(result, options)
end

#has_select?Boolean

rubocop:disable Naming/PredicateName

Returns:

  • (Boolean)


82
83
84
# File 'lib/wcc/contentful/middleware/store.rb', line 82

def has_select? # rubocop:disable Naming/PredicateName
  respond_to?(:select?)
end

#resolve_includes(entry, depth, options) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/wcc/contentful/middleware/store.rb', line 57

def resolve_includes(entry, depth, options)
  return entry unless entry && depth && depth > 0

  # We only care about entries (see #resolved_link?)
  WCC::Contentful::LinkVisitor.new(entry, :Entry, :Asset, depth: depth).map! do |val|
    resolve_link(val, options)
  end
end


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/wcc/contentful/middleware/store.rb', line 66

def resolve_link(val, options)
  return val unless resolved_link?(val)

  if !has_select? || select?(val, options)
    transform(val, options)
  else
    # Pretend it's an unresolved link -
    # matches the behavior of a store when the link cannot be retrieved
    WCC::Contentful::Link.new(val.dig('sys', 'id'), val.dig('sys', 'type')).to_h
  end
end

#resolved_link?(value) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/wcc/contentful/middleware/store.rb', line 78

def resolved_link?(value)
  value.is_a?(Hash) && %w[Entry Asset].include?(value.dig('sys', 'type'))
end

#transform(entry, _options) ⇒ Object

The default version of ‘#transform` just returns the entry. Override this with your own implementation.



88
89
90
# File 'lib/wcc/contentful/middleware/store.rb', line 88

def transform(entry, _options)
  entry
end