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, parameters = {}) ⇒ Contentful::Management::Array<Contentful::Management::Resource>
Gets a collection of resources.
-
#create(client, space_id, attributes) ⇒ Contentful::Management::Resource
Creates a resource.
-
#find(client, space_id, resource_id) ⇒ 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, parameters = {}) ⇒ Contentful::Management::Array<Contentful::Management::Resource>
Gets a collection of resources.
178 179 180 |
# File 'lib/contentful/management/resource.rb', line 178 def all(client, space_id, parameters = {}) ResourceRequester.new(client, self).all({ space_id: space_id }, parameters) end |
#create(client, space_id, attributes) ⇒ Contentful::Management::Resource
Creates a resource.
201 202 203 204 205 206 207 208 |
# File 'lib/contentful/management/resource.rb', line 201 def create(client, space_id, attributes) = { space_id: space_id } [:resource_id] = attributes[:id] if attributes.key?(:id) ResourceRequester.new(client, self).create( , attributes ) end |
#find(client, space_id, resource_id) ⇒ Contentful::Management::Resource
Gets a specific resource.
189 190 191 |
# File 'lib/contentful/management/resource.rb', line 189 def find(client, space_id, resource_id) ResourceRequester.new(client, self).find(space_id: space_id, resource_id: resource_id) end |
#nested_locale_fields? ⇒ Boolean
By default, fields come flattened in the current locale. This is different for sync
226 227 228 |
# File 'lib/contentful/management/resource.rb', line 226 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
246 247 248 249 250 251 252 253 254 255 |
# File 'lib/contentful/management/resource.rb', line 246 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
231 232 233 |
# File 'lib/contentful/management/resource.rb', line 231 def property_coercions @property_coercions ||= {} end |
#update_coercions! ⇒ Object
Ensure inherited classes pick up coercions
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/contentful/management/resource.rb', line 258 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 |