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
-
#all(client, space_id, environment_id = nil, parameters = {}) ⇒ Contentful::Management::Array<Contentful::Management::Resource>
Gets a collection of resources.
-
#create(client, space_id, environment_id = nil, attributes = {}) ⇒ Contentful::Management::Resource
Creates a resource.
-
#find(client, space_id, environment_id = nil, resource_id = nil) ⇒ Contentful::Management::Resource
Gets a specific resource.
-
#nested_locale_fields? ⇒ Boolean
By default, fields come flattened in the current locale.
-
#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.
-
#property_coercions ⇒ Object
Default property coercions.
-
#update_coercions! ⇒ Object
Ensure inherited classes pick up coercions.
Instance Method Details
#all(client, space_id, environment_id = nil, parameters = {}) ⇒ Contentful::Management::Array<Contentful::Management::Resource>
Gets a collection of resources.
229 230 231 |
# File 'lib/contentful/management/resource.rb', line 229 def all(client, space_id, environment_id = nil, parameters = {}) ResourceRequester.new(client, self).all({ space_id: space_id, environment_id: environment_id }, parameters) end |
#create(client, space_id, environment_id = nil, attributes = {}) ⇒ Contentful::Management::Resource
Creates a resource.
253 254 255 256 257 258 259 260 |
# File 'lib/contentful/management/resource.rb', line 253 def create(client, space_id, environment_id = nil, attributes = {}) = { space_id: space_id, environment_id: environment_id } [:resource_id] = attributes[:id] if attributes.respond_to?(:key) && attributes.key?(:id) ResourceRequester.new(client, self).create( , attributes ) end |
#find(client, space_id, environment_id = nil, resource_id = nil) ⇒ Contentful::Management::Resource
Gets a specific resource.
240 241 242 |
# File 'lib/contentful/management/resource.rb', line 240 def find(client, space_id, environment_id = nil, resource_id = nil) ResourceRequester.new(client, self).find(space_id: space_id, environment_id: environment_id, resource_id: resource_id) end |
#nested_locale_fields? ⇒ Boolean
By default, fields come flattened in the current locale. This is different for sync
278 279 280 |
# File 'lib/contentful/management/resource.rb', line 278 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
298 299 300 301 302 303 304 305 306 307 |
# File 'lib/contentful/management/resource.rb', line 298 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_coercions ⇒ Object
Default property coercions
283 284 285 |
# File 'lib/contentful/management/resource.rb', line 283 def property_coercions @property_coercions ||= {} end |
#update_coercions! ⇒ Object
Ensure inherited classes pick up coercions
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/contentful/management/resource.rb', line 310 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 |