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

#clientObject

Shared instance of the API client



114
115
116
# File 'lib/contentful/management/resource.rb', line 114

def client
  Contentful::Management::Client.shared_instance
end

#nested_locale_fields?Boolean

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

Returns:

  • (Boolean)


105
106
107
# File 'lib/contentful/management/resource.rb', line 105

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



129
130
131
132
133
134
135
136
137
138
# File 'lib/contentful/management/resource.rb', line 129

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



109
110
111
# File 'lib/contentful/management/resource.rb', line 109

def property_coercions
  @property_coercions ||= {}
end

#update_coercions!Object

Ensure inherited classes pick up coercions



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/contentful/management/resource.rb', line 141

def update_coercions!
  return if @coercions_updated

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

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

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

  @coercions_updated = true
end