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

#_metadata, #default_locale, #raw, #sys

Instance Method Summary collapse

Methods inherited from BaseResource

#==, #inspect, #reload

Constructor Details

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

rubocop:disable Metrics/ParameterLists



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

def initialize(item, _configuration, localized = false, includes = Includes.new, 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.



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

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



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

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



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

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



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

def locales
  @fields.keys
end

#marshal_dumpObject



51
52
53
# File 'lib/contentful/fields_resource.rb', line 51

def marshal_dump
  super.merge(raw: raw_with_links, localized: localized)
end

#marshal_load(raw_object) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/contentful/fields_resource.rb', line 56

def marshal_load(raw_object)
  super(raw_object)
  @localized = raw_object[:localized]
  @fields = hydrate_fields(
    raw_object[:configuration].fetch(:includes_for_single, Includes.new),
    {},
    raw_object[:configuration].fetch(:errors, [])
  )
  define_fields_methods!
end


68
69
70
71
72
73
74
75
76
77
78
# File 'lib/contentful/fields_resource.rb', line 68

def raw_with_links
  links = fields.keys.select { |property| known_link?(property) }
  processed_raw = raw.clone
  processed_raw['fields'] = raw['fields'].clone
  raw['fields'].each do |k, v|
    links_key = Support.snakify(k, @configuration[:use_camel_case])
    processed_raw['fields'][k] = links.include?(links_key.to_sym) ? send(links_key) : v
  end

  processed_raw
end