Module: Contentful::Resource::ClassMethods
- Defined in:
- lib/contentful/resource.rb
Overview
Register the resources properties on class level by using the #property method
Instance Method Summary collapse
-
#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
By default, fields come flattened in the current locale.
-
#update_coercions! ⇒ Object
Ensure inherited classes pick up coercions.
Instance Method Details
#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
170 171 172 173 174 175 |
# File 'lib/contentful/resource.rb', line 170 def property(name, property_class = nil) property_coercions[name.to_sym] = property_class define_method Contentful::Support.snakify(name) do properties[name.to_sym] end end |
#property_coercions ⇒ Object
By default, fields come flattened in the current locale. This is different for sync
155 156 157 |
# File 'lib/contentful/resource.rb', line 155 def property_coercions @property_coercions ||= {} end |
#update_coercions! ⇒ Object
Ensure inherited classes pick up coercions
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/contentful/resource.rb', line 178 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 |