Module: WCC::Contentful::EntryLocaleTransformer
- Extended by:
- EntryLocaleTransformer
- Included in:
- EntryLocaleTransformer, Middleware::Store::LocaleMiddleware
- Defined in:
- lib/wcc/contentful/entry_locale_transformer.rb
Overview
This class provides helper methods to transform Entry and Asset hashes from the “locale=*” format to a specific locale, and vice versa.
Instance Method Summary collapse
-
#configuration ⇒ Object
Attribute reader falling back to WCC::Contentful configuration needed for locale fallbacks.
-
#reduce_to_star(memo, entry) ⇒ Object
Takes an entry in a specific ‘sys.locale’ and merges it into an entry that is in the ‘locale=*’ format.
-
#transform_to_locale(entry, locale) ⇒ Object
Takes an entry in the ‘locale=*’ format and transforms it to a specific locale.
-
#transform_to_star(entry) ⇒ Object
Takes an entry which represents a specific ‘sys.locale’ and transforms it to the ‘locale=*’ format.
Instance Method Details
#configuration ⇒ Object
Attribute reader falling back to WCC::Contentful configuration needed for locale fallbacks
11 12 13 |
# File 'lib/wcc/contentful/entry_locale_transformer.rb', line 11 def configuration @configuration || WCC::Contentful.configuration end |
#reduce_to_star(memo, entry) ⇒ Object
Takes an entry in a specific ‘sys.locale’ and merges it into an entry that is in the ‘locale=*’ format
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/wcc/contentful/entry_locale_transformer.rb', line 86 def reduce_to_star(memo, entry) if memo_locale = memo.dig('sys', 'locale') raise WCC::Contentful::LocaleMismatchError, "expected locale: * but was #{memo_locale}" end unless entry_locale = entry.dig('sys', 'locale') raise WCC::Contentful::LocaleMismatchError, 'expected a specific locale but got locale: *' end if memo.dig('sys', 'id') != entry.dig('sys', 'id') raise ArgumentError, "IDs of memo and entry must match! were (#{memo.dig('sys', 'id').inspect} and #{entry.dig('sys', 'id').inspect})" end entry['fields'].each do |key, value| memo_field = memo['fields'][key] ||= {} memo_field[entry_locale] = value end memo end |
#transform_to_locale(entry, locale) ⇒ Object
Takes an entry in the ‘locale=*’ format and transforms it to a specific locale
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/wcc/contentful/entry_locale_transformer.rb', line 44 def transform_to_locale(entry, locale) # If the backing store already returned a localized entry, nothing to do if entry_locale = entry.dig('sys', 'locale') unless entry_locale == locale raise WCC::Contentful::LocaleMismatchError, "expected #{locale} but was #{entry_locale}" end return entry end # Transform the store's "locale=*" entry into a localized one locale ||= default_locale new_entry = entry.dup new_entry['sys'] = entry['sys'].deep_dup new_entry['sys']['locale'] = locale if entry.key?('fields') new_entry['fields'] = entry['fields']&.transform_values do |value| next if value.nil? # replace the all-locales value with the localized value l = locale v = nil while l v = value[l] break if v l = configuration.locale_fallbacks[l] end v end end new_entry end |
#transform_to_star(entry) ⇒ Object
Takes an entry which represents a specific ‘sys.locale’ and transforms it to the ‘locale=*’ format
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/wcc/contentful/entry_locale_transformer.rb', line 18 def transform_to_star(entry) # locale=* entries have a nil sys.locale unless entry_locale = entry.dig('sys', 'locale') # nothing to do return entry end new_entry = entry.dup new_entry['sys'] = entry['sys'].except('locale').merge({ 'WCC::Contentful::EntryLocaleTransformer:locales_included' => [entry_locale] }) if entry.key?('fields') new_entry['fields'] = entry['fields'].transform_values do |value| h = {} h[entry_locale] = value h end end new_entry end |