Class: ContentfulLite::Entry

Inherits:
Object
  • Object
show all
Includes:
CommonData, EntryMapping, Validations::Entry
Defined in:
lib/contentful_lite/entry.rb

Direct Known Subclasses

DeletedEntry

Instance Attribute Summary collapse

Attributes included from CommonData

#created_at, #default_locale, #environment_id, #id, #locales, #localized_fields, #retrieved_at, #revision, #space_id, #sys, #updated_at

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations::Entry

#errors_for_all_locales, #valid_for_all_locales?, validates_included_asset, validates_included_entry

Methods included from CommonData

#as_json, #fields, #locale, #locale=, #to_link, #with_locale

Constructor Details

#initialize(raw) ⇒ Entry

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.

Returns a new instance of Entry.

Parameters:

  • raw (Hash)

    raw response from Contentful API



12
13
14
15
16
17
18
# File 'lib/contentful_lite/entry.rb', line 12

def initialize(raw)
  super(raw)
  @content_type_id = raw['sys']['contentType']['sys']['id']
  @localized_fields.values.each do |fields|
    fields.transform_values! { |value| build_link(value) }
  end
end

Instance Attribute Details

#content_type_idObject (readonly)

The id for the content type of this entry



8
9
10
# File 'lib/contentful_lite/entry.rb', line 8

def content_type_id
  @content_type_id
end

Class Method Details

.field_reader(*attrs, default: nil, localizable: false) ⇒ Object

Defines a field existing on the content type. This macro registers the accessor for that field

Examples:

Defines two string localizable localized fields

field_reader :first_name, :last_name, localizable: true

Parameters:

  • attrs (Array<Symbol,String>)

    The field names

  • default (Object, nil) (defaults to: nil)

    The default value to return if field is not present on API response

  • localizable (Boolean) (defaults to: false)

    If the field is marked as localizable

See Also:



34
35
36
37
38
39
40
41
# File 'lib/contentful_lite/entry.rb', line 34

def self.field_reader(*attrs, default: nil, localizable: false)
  attrs.each do |k|
    define_method(k) do |locale: nil|
      field = fields(locale: localizable ? locale : default_locale)[k.to_s]
      field.nil? ? default : field
    end
  end
end

Instance Method Details

Gets the URL to view/edit the entry on Contentful webapp

Returns:

  • (String)


22
23
24
# File 'lib/contentful_lite/entry.rb', line 22

def contentful_link
  "https://app.contentful.com/spaces/#{space_id}/entries/#{id}"
end