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
-
#store ⇒ Object
Returns the value of attribute store.
Instance Method Summary collapse
- #find(id, **options) ⇒ Object
- #find_all(options: nil, **args) ⇒ Object
- #find_by(options: nil, **args) ⇒ Object
-
#has_select? ⇒ Boolean
rubocop:disable Naming/PredicateName.
- #resolve_includes(entry, depth, options) ⇒ Object
- #resolve_link(val, options) ⇒ Object
- #resolved_link?(value) ⇒ Boolean
-
#transform(entry, _options) ⇒ Object
The default version of ‘#transform` just returns the entry.
Methods included from Store::Interface
Instance Attribute Details
#store ⇒ Object
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, **) found = store.find(id, **) return transform(found, ) if found && (!has_select? || select?(found, )) 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) ||= {} DelegatingQuery.new( store.find_all(**args.merge(options: )), middleware: self, 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) ||= {} result = store.find_by(**args.merge(options: )) return unless result && (!has_select? || select?(result, )) result = resolve_includes(result, [:include], ) if && [:include] transform(result, ) end |
#has_select? ⇒ Boolean
rubocop:disable Naming/PredicateName
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, ) 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, ) end end |
#resolve_link(val, options) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/wcc/contentful/middleware/store.rb', line 66 def resolve_link(val, ) return val unless resolved_link?(val) if !has_select? || select?(val, ) transform(val, ) 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
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, ) entry end |