Class: WCC::Contentful::Middleware::Store::LocaleMiddleware

Inherits:
Object
  • Object
show all
Includes:
EntryLocaleTransformer, WCC::Contentful::Middleware::Store
Defined in:
lib/wcc/contentful/middleware/store/locale_middleware.rb

Overview

This middleware enforces that all entries returned by the store layer are properly localized. It does this by transforming entries from the store’s “locale=*” format into the specified locale (or default).

Stores keep entries in the “locale=*” format, which is a hash of all locales for each field. This is convenient because the Sync API returns them in this format. However, the Model layer requires localized entries. So, to separate concerns, this middleware handles the transformation.

Constant Summary

Constants included from Store::Interface

Store::Interface::INTERFACE_METHODS

Instance Attribute Summary collapse

Attributes included from WCC::Contentful::Middleware::Store

#store

Instance Method Summary collapse

Methods included from EntryLocaleTransformer

#reduce_to_star, #transform_to_locale, #transform_to_star

Methods included from WCC::Contentful::Middleware::Store

#find, #find_all, #find_by, #has_select?, #resolve_includes, #resolve_link, #resolved_link?

Methods included from Store::Interface

#find, #find_all, #find_by, #index, #index?

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



15
16
17
# File 'lib/wcc/contentful/middleware/store/locale_middleware.rb', line 15

def configuration
  @configuration
end

Instance Method Details

#default_localeObject



17
18
19
# File 'lib/wcc/contentful/middleware/store/locale_middleware.rb', line 17

def default_locale
  @default_locale ||= configuration&.default_locale&.to_s || 'en-US'
end

#transform(entry, options) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/wcc/contentful/middleware/store/locale_middleware.rb', line 21

def transform(entry, options)
  locale = options[:locale]&.to_s || default_locale
  if locale == '*'
    transform_to_star(entry)
  else
    transform_to_locale(entry, locale)
  end
end