Class: Contentful::FieldsResource

Inherits:
BaseResource show all
Defined in:
lib/contentful/fields_resource.rb

Overview

Base definition of a Contentful Resource containing Field properties

Direct Known Subclasses

Asset, Entry

Instance Attribute Summary collapse

Attributes inherited from BaseResource

#default_locale, #raw, #sys

Instance Method Summary collapse

Methods inherited from BaseResource

#==, #reload

Constructor Details

#initialize(item, _configuration, localized = false, includes = [], entries = {}, depth = 0, errors = []) ⇒ FieldsResource

rubocop:disable Metrics/ParameterLists



12
13
14
15
16
17
18
19
# File 'lib/contentful/fields_resource.rb', line 12

def initialize(item, _configuration, localized = false, includes = [], entries = {}, depth = 0, errors = [])
  super

  @configuration[:errors] = errors
  @localized = localized
  @fields = hydrate_fields(includes, entries, errors)
  define_fields_methods!
end

Instance Attribute Details

#localizedObject (readonly)

Returns the value of attribute localized.



9
10
11
# File 'lib/contentful/fields_resource.rb', line 9

def localized
  @localized
end

Instance Method Details

#fields(wanted_locale = nil) ⇒ Hash

Returns all fields of the asset

Returns:

  • (Hash)

    fields for Resource on selected locale



24
25
26
27
# File 'lib/contentful/fields_resource.rb', line 24

def fields(wanted_locale = nil)
  wanted_locale = internal_resource_locale if wanted_locale.nil?
  @fields.fetch(wanted_locale.to_s, {})
end

#fields_with_localesHash

Returns all fields of the asset with locales nested by field

Returns:

  • (Hash)

    fields for Resource grouped by field name



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/contentful/fields_resource.rb', line 32

def fields_with_locales
  remapped_fields = {}
  locales.each do |locale|
    fields(locale).each do |name, value|
      remapped_fields[name] ||= {}
      remapped_fields[name][locale.to_sym] = value
    end
  end

  remapped_fields
end

#localesObject

Provides a list of the available locales for a Resource



45
46
47
# File 'lib/contentful/fields_resource.rb', line 45

def locales
  @fields.keys
end