Module: Contentful::Resource
- Included in:
- Array, Asset, ContentType, DeletedAsset, DeletedEntry, Entry, Field, File, Link, Locale, Location, Space, SyncPage
- Defined in:
- lib/contentful/resource.rb,
lib/contentful/resource/fields.rb,
lib/contentful/resource/array_like.rb,
lib/contentful/resource/asset_fields.rb,
lib/contentful/resource/system_properties.rb
Overview
Include this module to declare a class to be a contentful resource. This is done by the default in the existing resource classes
You can define your own classes that behave like contentful resources: See examples/custom_classes.rb to see how.
Take a look at examples/resource_mapping.rb on how to register them to be returned by the client by default
Defined Under Namespace
Modules: ArrayLike, AssetFields, ClassMethods, Fields, SystemProperties
Constant Summary collapse
- COERCIONS =
{ string: ->(v) { v.to_s }, integer: ->(v) { v.to_i }, float: ->(v) { v.to_f }, boolean: ->(v) { !!v }, date: ->(v) { DateTime.parse(v) } }
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#default_locale ⇒ Object
readonly
Returns the value of attribute default_locale.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Instance Method Summary collapse
-
#array? ⇒ Boolean
Returns true for resources that behave like an array.
-
#fields ⇒ Object
Resources that don’t include Fields or AssetFields return nil for #fields.
- #initialize(object, request = nil, client = nil, nested_locale_fields = false, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale]) ⇒ Object
- #inspect(info = nil) ⇒ Object
-
#nested_locale_fields? ⇒ Boolean
By default, fields come flattened in the current locale.
-
#reload ⇒ Object
Issues the request that was made to fetch this response again.
-
#sys ⇒ Object
Resources that don’t include SystemProperties return nil for #sys.
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
22 23 24 |
# File 'lib/contentful/resource.rb', line 22 def client @client end |
#default_locale ⇒ Object (readonly)
Returns the value of attribute default_locale.
22 23 24 |
# File 'lib/contentful/resource.rb', line 22 def default_locale @default_locale end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
22 23 24 |
# File 'lib/contentful/resource.rb', line 22 def properties @properties end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
22 23 24 |
# File 'lib/contentful/resource.rb', line 22 def request @request end |
Instance Method Details
#array? ⇒ Boolean
Returns true for resources that behave like an array
41 42 43 |
# File 'lib/contentful/resource.rb', line 41 def array? false end |
#fields ⇒ Object
Resources that don’t include Fields or AssetFields return nil for #fields
56 57 58 |
# File 'lib/contentful/resource.rb', line 56 def fields nil end |
#initialize(object, request = nil, client = nil, nested_locale_fields = false, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale]) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/contentful/resource.rb', line 24 def initialize(object, request = nil, client = nil, nested_locale_fields = false, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale]) self.class.update_coercions! @nested_locale_fields = nested_locale_fields @default_locale = default_locale @properties = extract_from_object(object, :property, self.class.property_coercions.keys) @request = request @client = client end |
#inspect(info = nil) ⇒ Object
35 36 37 38 |
# File 'lib/contentful/resource.rb', line 35 def inspect(info = nil) properties_info = properties.empty? ? '' : " @properties=#{properties.inspect}" "#<#{self.class}:#{properties_info}#{info}>" end |
#nested_locale_fields? ⇒ Boolean
By default, fields come flattened in the current locale. This is different for syncs
46 47 48 |
# File 'lib/contentful/resource.rb', line 46 def nested_locale_fields? !! @nested_locale_fields end |
#reload ⇒ Object
Issues the request that was made to fetch this response again. Only works for top-level resources
62 63 64 65 66 67 68 |
# File 'lib/contentful/resource.rb', line 62 def reload if request request.get else false end end |
#sys ⇒ Object
Resources that don’t include SystemProperties return nil for #sys
51 52 53 |
# File 'lib/contentful/resource.rb', line 51 def sys nil end |