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.
Class Method Summary collapse
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 = nil, request = nil, client = nil, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale]) ⇒ Object
- #inspect(info = nil) ⇒ Object
- #localized?(value) ⇒ Boolean
-
#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.
23 24 25 |
# File 'lib/contentful/resource.rb', line 23 def client @client end |
#default_locale ⇒ Object (readonly)
Returns the value of attribute default_locale.
23 24 25 |
# File 'lib/contentful/resource.rb', line 23 def default_locale @default_locale end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
23 24 25 |
# File 'lib/contentful/resource.rb', line 23 def properties @properties end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
23 24 25 |
# File 'lib/contentful/resource.rb', line 23 def request @request end |
Class Method Details
.included(base) ⇒ Object
197 198 199 |
# File 'lib/contentful/resource.rb', line 197 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#array? ⇒ Boolean
Returns true for resources that behave like an array
42 43 44 |
# File 'lib/contentful/resource.rb', line 42 def array? false end |
#fields ⇒ Object
Resources that don’t include Fields or AssetFields return nil for #fields
57 58 59 |
# File 'lib/contentful/resource.rb', line 57 def fields nil end |
#initialize(object = nil, request = nil, client = nil, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale]) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/contentful/resource.rb', line 25 def initialize(object = nil, request = nil, client = nil, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale]) self.class.update_coercions! @default_locale = default_locale @properties = extract_from_object(object, :property, self.class.property_coercions.keys) @request = request @client = client @api_object = object end |
#inspect(info = nil) ⇒ Object
36 37 38 39 |
# File 'lib/contentful/resource.rb', line 36 def inspect(info = nil) properties_info = properties.empty? ? '' : " @properties=#{properties.inspect}" "#<#{self.class}:#{properties_info}#{info}>" end |
#localized?(value) ⇒ Boolean
46 47 48 49 |
# File 'lib/contentful/resource.rb', line 46 def localized?(value) return false unless value.is_a? ::Hash value.keys.any? { |possible_locale| Contentful::Constants::KNOWN_LOCALES.include?(possible_locale) } end |
#reload ⇒ Object
Issues the request that was made to fetch this response again. Only works for top-level resources
63 64 65 66 67 68 69 |
# File 'lib/contentful/resource.rb', line 63 def reload if request request.get else false end end |
#sys ⇒ Object
Resources that don’t include SystemProperties return nil for #sys
52 53 54 |
# File 'lib/contentful/resource.rb', line 52 def sys nil end |