Module: Contentful::Management::Resource

Included in:
Array, Asset, ContentType, Entry, Field, File, Link, Locale, Location, Space, Validation, Webhook
Defined in:
lib/contentful/management/resource.rb,
lib/contentful/management/resource/fields.rb,
lib/contentful/management/resource/refresher.rb,
lib/contentful/management/resource/array_like.rb,
lib/contentful/management/resource/asset_fields.rb,
lib/contentful/management/resource/entry_fields.rb,
lib/contentful/management/resource/system_properties.rb

Overview

Include this module to declare a class to be a contentful resource. This is done by the default in the existing resource classes

You can define your own classes that behave like contentful resources: See examples/custom_classes.rb to see how.

Take a look at examples/resource_mapping.rb on how to register them to be returned by the client by default

Defined Under Namespace

Modules: ArrayLike, AssetFields, ClassMethods, EntryFields, Fields, Refresher, SystemProperties

Constant Summary collapse

COERCIONS =
{
  string: ->(value) { value.to_s },
  integer: ->(value) { value.to_i },
  float: ->(value) { value.to_f },
  boolean: ->(value) { !!value },
  date: ->(value) { !value.nil? ? DateTime.parse(value) : nil }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject (readonly)

Shared instance of the API client



63
64
65
# File 'lib/contentful/management/resource.rb', line 63

def client
  @client
end

#default_localeObject (readonly)

Returns the value of attribute default_locale.



24
25
26
# File 'lib/contentful/management/resource.rb', line 24

def default_locale
  @default_locale
end

#propertiesObject (readonly)

Returns the value of attribute properties.



24
25
26
# File 'lib/contentful/management/resource.rb', line 24

def properties
  @properties
end

#raw_objectObject (readonly)

Returns the value of attribute raw_object.



24
25
26
# File 'lib/contentful/management/resource.rb', line 24

def raw_object
  @raw_object
end

#requestObject (readonly)

Returns the value of attribute request.



24
25
26
# File 'lib/contentful/management/resource.rb', line 24

def request
  @request
end

Instance Method Details

#array?Boolean

Returns true for resources that behave like an array

Returns:

  • (Boolean)


43
44
45
# File 'lib/contentful/management/resource.rb', line 43

def array?
  false
end

#fieldsObject

Resources that don’t include Fields or AssetFields return nil for #fields



58
59
60
# File 'lib/contentful/management/resource.rb', line 58

def fields
  nil
end

#initialize(object = nil, request = nil, client = nil, nested_locale_fields = false, default_locale = Contentful::Management::Client::DEFAULT_CONFIGURATION[:default_locale]) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/contentful/management/resource.rb', line 26

def initialize(object = nil, request = nil, client = nil, nested_locale_fields = false, default_locale = Contentful::Management::Client::DEFAULT_CONFIGURATION[:default_locale])
  self.class.update_coercions!
  @nested_locale_fields = nested_locale_fields
  @default_locale = default_locale

  @properties = extract_from_object object, :property, self.class.property_coercions.keys
  @request = request
  @client = client
  @raw_object = object
end

#inspect(info = nil) ⇒ Object



37
38
39
40
# File 'lib/contentful/management/resource.rb', line 37

def inspect(info = nil)
  properties_info = properties.empty? ? '' : " @properties=#{ properties.inspect }"
  "#<#{ self.class }:#{ properties_info }#{ info }>"
end

#nested_locale_fields?Boolean

By default, fields come flattened in the current locale. This is different for syncs

Returns:

  • (Boolean)


48
49
50
# File 'lib/contentful/management/resource.rb', line 48

def nested_locale_fields?
  !!@nested_locale_fields
end

#sysObject

Resources that don’t include SystemProperties return nil for #sys



53
54
55
# File 'lib/contentful/management/resource.rb', line 53

def sys
  nil
end