Module: ContentfulLite::CommonData
Overview
Parses data common to all Contentful resources.
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#default_locale ⇒ Object
readonly
Returns the value of attribute default_locale.
-
#environment_id ⇒ Object
readonly
Returns the value of attribute environment_id.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#locales ⇒ Object
readonly
Returns the value of attribute locales.
-
#localized_fields ⇒ Object
readonly
Returns the value of attribute localized_fields.
-
#retrieved_at ⇒ Object
readonly
Returns the value of attribute retrieved_at.
-
#revision ⇒ Object
readonly
Returns the value of attribute revision.
-
#space_id ⇒ Object
readonly
Returns the value of attribute space_id.
-
#sys ⇒ Object
readonly
Returns the value of attribute sys.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Instance Method Summary collapse
-
#as_json(args = {}) ⇒ Hash
Provided for compatibility with Rails JSON serializer.
-
#fields(locale: nil) ⇒ Hash
Returns a hash with field => value format using specified locale.
- #initialize(raw) ⇒ Object private
-
#locale ⇒ Object
Provides access to the locale being used to read fields.
-
#locale=(value) ⇒ Object
Sets the locale that will be used to read fields.
-
#to_link ⇒ ContentfulLite::Link
Gets a ContentfulLite::Link to the entry.
-
#with_locale(locale) { ... } ⇒ Object
Executes a block with #locale set to the received locale.
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
7 8 9 |
# File 'lib/contentful_lite/common_data.rb', line 7 def created_at @created_at end |
#default_locale ⇒ Object (readonly)
Returns the value of attribute default_locale.
7 8 9 |
# File 'lib/contentful_lite/common_data.rb', line 7 def default_locale @default_locale end |
#environment_id ⇒ Object (readonly)
Returns the value of attribute environment_id.
7 8 9 |
# File 'lib/contentful_lite/common_data.rb', line 7 def environment_id @environment_id end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/contentful_lite/common_data.rb', line 7 def id @id end |
#locales ⇒ Object (readonly)
Returns the value of attribute locales.
7 8 9 |
# File 'lib/contentful_lite/common_data.rb', line 7 def locales @locales end |
#localized_fields ⇒ Object (readonly)
Returns the value of attribute localized_fields.
7 8 9 |
# File 'lib/contentful_lite/common_data.rb', line 7 def localized_fields @localized_fields end |
#retrieved_at ⇒ Object (readonly)
Returns the value of attribute retrieved_at.
7 8 9 |
# File 'lib/contentful_lite/common_data.rb', line 7 def retrieved_at @retrieved_at end |
#revision ⇒ Object (readonly)
Returns the value of attribute revision.
7 8 9 |
# File 'lib/contentful_lite/common_data.rb', line 7 def revision @revision end |
#space_id ⇒ Object (readonly)
Returns the value of attribute space_id.
7 8 9 |
# File 'lib/contentful_lite/common_data.rb', line 7 def space_id @space_id end |
#sys ⇒ Object (readonly)
Returns the value of attribute sys.
7 8 9 |
# File 'lib/contentful_lite/common_data.rb', line 7 def sys @sys end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
7 8 9 |
# File 'lib/contentful_lite/common_data.rb', line 7 def updated_at @updated_at end |
Instance Method Details
#as_json(args = {}) ⇒ Hash
Provided for compatibility with Rails JSON serializer
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/contentful_lite/common_data.rb', line 80 def as_json(args = {}) args[:serialized_ids] ||= [] return to_link.as_json if args[:serialized_ids].include?(id) { "sys" => sys, "fields" => fields.transform_values do |value| if value.respond_to?(:as_json) value.as_json(**args.merge(serialized_ids: args[:serialized_ids] + [id])) else value end end } end |
#fields(locale: nil) ⇒ Hash
Returns a hash with field => value format using specified locale
52 53 54 |
# File 'lib/contentful_lite/common_data.rb', line 52 def fields(locale: nil) @localized_fields.fetch(locale || self.locale, {}) end |
#initialize(raw) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/contentful_lite/common_data.rb', line 11 def initialize(raw) @sys = raw['sys'] @id = sys['id'] @created_at = DateTime.parse sys['createdAt'] @updated_at = DateTime.parse sys['updatedAt'] @locale = sys['locale'] @revision = sys['revision'] @space_id = sys['space']['sys']['id'] @environment_id = sys['environment']['sys']['id'] @retrieved_at = DateTime.now if locale @locales = [locale] @localized_fields = { locale => raw['fields'] } else @locales = raw.fetch('fields', {}).values.collect_concat(&:keys).uniq @localized_fields = @locales.each_with_object({}) do |locale, hash| hash[locale] = raw['fields'].transform_values { |value| value[locale] } end end @default_locale = @locales.first end |
#locale ⇒ Object
Provides access to the locale being used to read fields
36 37 38 |
# File 'lib/contentful_lite/common_data.rb', line 36 def locale @locale || @default_locale end |
#locale=(value) ⇒ Object
Sets the locale that will be used to read fields
43 44 45 46 47 |
# File 'lib/contentful_lite/common_data.rb', line 43 def locale=(value) raise 'Invalid Locale' unless value.in?(locales) @locale = value end |
#to_link ⇒ ContentfulLite::Link
Gets a ContentfulLite::Link to the entry
72 73 74 |
# File 'lib/contentful_lite/common_data.rb', line 72 def to_link ContentfulLite::Link.new(self) end |
#with_locale(locale) { ... } ⇒ Object
Executes a block with #locale set to the received locale. Then sets it back to current locale.
60 61 62 63 64 65 66 67 68 |
# File 'lib/contentful_lite/common_data.rb', line 60 def with_locale(locale) old_locale = @locale @locale = locale unless locale.nil? begin yield ensure @locale = old_locale end end |