Module: Contentful::Management::Resource::ClassMethods

Defined in:
lib/contentful/management/resource.rb

Overview

Register the resources properties on class level by using the #property method

Instance Method Summary collapse

Instance Method Details

#all(client, space_id, environment_id = nil, parameters = {}, organization_id = nil) ⇒ Contentful::Management::Array<Contentful::Management::Resource>

Gets a collection of resources.

Parameters:

Returns:

See Also:

  • For complete option list: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters


232
233
234
235
236
# File 'lib/contentful/management/resource.rb', line 232

def all(client, space_id, environment_id = nil, parameters = {}, organization_id = nil)
  ResourceRequester.new(client, self).all(
    { space_id: space_id, environment_id: environment_id, organization_id: organization_id }, parameters
  )
end

#create(client, space_id, environment_id = nil, attributes = {}) ⇒ Contentful::Management::Resource

Creates a resource.

Parameters:

Returns:

See Also:

  • README for full attribute list for each resource.


259
260
261
262
263
264
265
266
# File 'lib/contentful/management/resource.rb', line 259

def create(client, space_id, environment_id = nil, attributes = {})
  endpoint_options = { space_id: space_id, environment_id: environment_id }
  endpoint_options[:resource_id] = attributes[:id] if attributes.respond_to?(:key) && attributes.key?(:id)
  ResourceRequester.new(client, self).create(
    endpoint_options,
    attributes
  )
end

#find(client, space_id, environment_id = nil, resource_id = nil, organization_id = nil) ⇒ Contentful::Management::Resource

Gets a specific resource.

Parameters:

Returns:



245
246
247
248
# File 'lib/contentful/management/resource.rb', line 245

def find(client, space_id, environment_id = nil, resource_id = nil, organization_id = nil)
  ResourceRequester.new(client, self).find(space_id: space_id, environment_id: environment_id,
                                           resource_id: resource_id, organization_id: organization_id)
end

#nested_locale_fields?Boolean

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

Returns:

  • (Boolean)


284
285
286
# File 'lib/contentful/management/resource.rb', line 284

def nested_locale_fields?
  false
end

#property(name, property_class = nil) ⇒ Object

Defines which properties of a resource your class expects Define them in :camelCase, they will be available as #snake_cased methods

You can pass in a second “type” argument:

  • If it is a class, it will be initialized for the property

  • Symbols are looked up in the COERCION constant for a lambda that defines a type conversion to apply

Note: This second argument is not meant for contentful sub-resources, but for structured objects (like locales in a space) Sub-resources are handled by the resource builder



304
305
306
307
308
309
310
311
312
313
# File 'lib/contentful/management/resource.rb', line 304

def property(name, property_class = nil)
  property_coercions[name.to_sym] = property_class
  accessor_name = Contentful::Management::Support.snakify(name)
  define_method accessor_name do
    properties[name.to_sym]
  end
  define_method "#{accessor_name}=" do |value|
    properties[name.to_sym] = value
  end
end

#property_coercionsObject

Default property coercions



289
290
291
# File 'lib/contentful/management/resource.rb', line 289

def property_coercions
  @property_coercions ||= {}
end

#update_coercions!Object

Ensure inherited classes pick up coercions



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/contentful/management/resource.rb', line 316

def update_coercions!
  return if @coercions_updated

  if superclass.respond_to? :property_coercions
    @property_coercions = superclass.property_coercions.dup.merge(@property_coercions || {})
  end

  @sys_coercions = superclass.sys_coercions.dup.merge(@sys_coercions || {}) if superclass.respond_to? :sys_coercions

  if superclass.respond_to? :fields_coercions
    @fields_coercions = superclass.fields_coercions.dup.merge(@fields_coercions || {})
  end

  @coercions_updated = true
end