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

Instance Method Details

#nested_locale_fields?Boolean

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

Returns:

  • (Boolean)


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

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



128
129
130
131
132
133
# File 'lib/contentful/resource.rb', line 128

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_coercionsObject



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

def property_coercions
  @property_coercions ||= {}
end

#update_coercions!Object

Ensure inherited classes pick up coercions



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/contentful/resource.rb', line 136

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