module Contentful::Management::Resource
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
@see _ examples/custom_classes.rb Custom Class as Resource
@see _ examples/resource_mapping.rb Mapping a Custom Class
Constants
- COERCIONS
-
@private rubocop:disable Style/DoubleNegation
Attributes
rubocop:enable Style/DoubleNegation
rubocop:enable Style/DoubleNegation
rubocop:enable Style/DoubleNegation
Public Class Methods
Source
# File lib/contentful/management/resource.rb, line 50 def self.included(base) base.extend(ClassMethods) end
@private
Source
# File lib/contentful/management/resource.rb, line 36 def initialize(object = nil, request = nil, client = nil, nested_locale_fields = false) self.class.update_coercions! @nested_locale_fields = nested_locale_fields @properties = extract_from_object object, :property, self.class.property_coercions.keys @request = request @client = client @raw_object = object end
@private
Public Instance Methods
Source
# File lib/contentful/management/resource.rb, line 55 def after_create(_attributes); end
@private
Source
# File lib/contentful/management/resource.rb, line 101 def array? false end
Returns true for resources that behave like an array
Source
# File lib/contentful/management/resource.rb, line 138 def default_locale client.default_locale end
Get default_locale
from client
Source
# File lib/contentful/management/resource.rb, line 86 def destroy ResourceRequester.new(client, self.class).destroy( space_id: space.id, environment_id: environment_id, resource_id: id ) end
Destroys a resource.
@return [true, Contentful::Management::Error
] success
Source
# File lib/contentful/management/resource.rb, line 133 def environment_id nil end
Returns the Environment
ID
Source
# File lib/contentful/management/resource.rb, line 118 def fields nil end
Resources that don’t include Fields
or AssetFields
return nil for fields
Source
# File lib/contentful/management/resource.rb, line 95 def inspect(info = nil) properties_info = properties.empty? ? '' : " @properties=#{properties.inspect}" "#<#{self.class}:#{properties_info}#{info}>" end
@private
Source
# File lib/contentful/management/resource.rb, line 106 def nested_locale_fields? # rubocop:disable Style/DoubleNegation !!@nested_locale_fields # rubocop:enable Style/DoubleNegation end
By default, fields come flattened in the current locale. This is different for syncs
Source
# File lib/contentful/management/resource.rb, line 128 def resource? true end
@return [true]
Source
# File lib/contentful/management/resource.rb, line 79 def save update({}) end
Creates or updates a resource.
@return [Contentful::Management::Resource]
Source
# File lib/contentful/management/resource.rb, line 113 def sys nil end
Resources that don’t include SystemProperties
return nil for sys
Source
# File lib/contentful/management/resource.rb, line 64 def update(attributes) headers = self.class.create_headers(client, attributes, self) headers = headers.merge(update_headers) ResourceRequester.new(client, self.class).update( self, update_url_attributes, query_attributes(attributes), headers ) end
Updates a resource.
@param [Hash] attributes
@see _ README for more information on how to create each resource
@return [Contentful::Management::Resource]
Protected Instance Methods
Source
# File lib/contentful/management/resource.rb, line 156 def query_attributes(attributes) attributes end
Source
# File lib/contentful/management/resource.rb, line 144 def update_headers { version: sys[:version] } end
Source
# File lib/contentful/management/resource.rb, line 148 def update_url_attributes { space_id: space.id, environment_id: environment_id, resource_id: id } end
Private Instance Methods
Source
# File lib/contentful/management/resource.rb, line 188 def coerce_or_create_class(value, what) case what when Symbol COERCIONS[what] ? COERCIONS[what][value] : value when Class what.new(value, client) when Proc what[value] else value end end
Source
# File lib/contentful/management/resource.rb, line 180 def coerce_value_or_array(value, what = nil) if value.is_a? ::Array value.map { |row| coerce_or_create_class(row, what) } else coerce_or_create_class(value, what) end end
Source
# File lib/contentful/management/resource.rb, line 166 def extract_from_object(object, namespace, keys = nil) if object keys ||= object.keys keys.each.with_object({}) do |name, res| res[name.to_sym] = coerce_value_or_array( object.is_a?(::Array) ? object : object[name.to_s], self.class.public_send(:"#{namespace}_coercions")[name.to_sym] ) end else {} end end
Source
# File lib/contentful/management/resource.rb, line 162 def internal_resource_locale sys.fetch(:locale, nil) || default_locale end