Module: Contentful::Resource

Included in:
Array, Asset, ContentType, DeletedAsset, DeletedEntry, Entry, Field, File, Link, Locale, Location, Space, SyncPage
Defined in:
lib/contentful/resource.rb,
lib/contentful/resource/fields.rb,
lib/contentful/resource/array_like.rb,
lib/contentful/resource/asset_fields.rb,
lib/contentful/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, Fields, SystemProperties

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



22
23
24
# File 'lib/contentful/resource.rb', line 22

def client
  @client
end

#default_localeObject (readonly)

Returns the value of attribute default_locale.



22
23
24
# File 'lib/contentful/resource.rb', line 22

def default_locale
  @default_locale
end

#propertiesObject (readonly)

Returns the value of attribute properties.



22
23
24
# File 'lib/contentful/resource.rb', line 22

def properties
  @properties
end

#requestObject (readonly)

Returns the value of attribute request.



22
23
24
# File 'lib/contentful/resource.rb', line 22

def request
  @request
end

Instance Method Details

#array?Boolean

Returns true for resources that behave like an array

Returns:

  • (Boolean)


40
41
42
# File 'lib/contentful/resource.rb', line 40

def array?
  false
end

#fieldsObject

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



55
56
57
# File 'lib/contentful/resource.rb', line 55

def fields
  nil
end

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



24
25
26
27
28
29
30
31
32
# File 'lib/contentful/resource.rb', line 24

def initialize(object, request = nil, client = nil, nested_locale_fields = false, default_locale = Contentful::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
end

#inspect(info = nil) ⇒ Object



34
35
36
37
# File 'lib/contentful/resource.rb', line 34

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)


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

def nested_locale_fields?
  !! @nested_locale_fields
end

#reloadObject

Issues the request that was made to fetch this response again. Only works for top-level resources



61
62
63
64
65
66
67
# File 'lib/contentful/resource.rb', line 61

def reload
  if request
    request.get
  else
    false
  end
end

#sysObject

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



50
51
52
# File 'lib/contentful/resource.rb', line 50

def sys
  nil
end