Class: ContentfulModel::Base

Inherits:
Contentful::Entry
  • Object
show all
Includes:
Associations, Manageable, Queries, Validations
Defined in:
lib/contentful_model/base.rb

Overview

Wrapper for Contentful::Entry which provides ActiveModel-like syntax

Constant Summary collapse

TIME_FORMAT =

Time format for Cache Key

'%Y%m%d%H%M%S%6N'.freeze

Class Attribute Summary collapse

Attributes included from Manageable

#dirty

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Queries

included

Methods included from Manageable

included, #publish, #refetch_management_entry, #save, #save!, #to_management

Methods included from Validations

included, #invalid?, #valid?, #validate

Methods included from Associations

included

Constructor Details

#initializeBase

Returns a new instance of Base.



19
20
21
22
# File 'lib/contentful_model/base.rb', line 19

def initialize(*)
  super
  override_getters
end

Class Attribute Details

.clientObject

Returns the value of attribute client.



87
88
89
# File 'lib/contentful_model/base.rb', line 87

def client
  @client
end

.coercionsObject

Returns the value of attribute coercions.



87
88
89
# File 'lib/contentful_model/base.rb', line 87

def coercions
  @coercions
end

.content_type_idObject

Returns the value of attribute content_type_id.



87
88
89
# File 'lib/contentful_model/base.rb', line 87

def content_type_id
  @content_type_id
end

.return_nil_for_empty_attribute_fieldsObject

Returns the value of attribute return_nil_for_empty_attribute_fields.



87
88
89
# File 'lib/contentful_model/base.rb', line 87

def return_nil_for_empty_attribute_fields
  @return_nil_for_empty_attribute_fields
end

Class Method Details

.add_entry_mappingObject



127
128
129
# File 'lib/contentful_model/base.rb', line 127

def add_entry_mapping
  ContentfulModel.configuration.entry_mapping[@content_type_id] = to_s.constantize unless mapping?
end

.coerce_field(*coercions) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/contentful_model/base.rb', line 145

def coerce_field(*coercions)
  @coercions ||= {}
  coercions.each do |coercions_hash|
    @coercions.merge!(coercions_hash)
  end
  @coercions
end

.coerce_value(field_name, value) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/contentful_model/base.rb', line 153

def coerce_value(field_name, value)
  return value if coercions.nil?

  coercion = coercions[field_name]

  case coercion
  when Symbol, String
    coercion = Contentful::Field::KNOWN_TYPES[coercion.to_s]
    return coercion.new(value).coerce unless coercion.nil?
  when Proc
    coercion[value]
  else
    value
  end
end

.content_typeObject



141
142
143
# File 'lib/contentful_model/base.rb', line 141

def content_type
  client.content_type(@content_type_id)
end

.descendentsObject



89
90
91
# File 'lib/contentful_model/base.rb', line 89

def descendents
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

.discovered_include_levelObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/contentful_model/base.rb', line 97

def discovered_include_level
  @discovered_include_level ||= nil
  return @discovered_include_level unless @discovered_include_level.nil?

  # Recreate content type tree
  includes = {}
  discovered_includes.each do |klass|
    # Recursively find includes - remove self from reference lists
    includes[klass] = klass.constantize.discovered_includes.reject { |i| i == to_s } + [klass]
  end

  include_level = includes.values.map(&:size).max # Longest include chain
  return @discovered_include_level = 1 if include_level.nil? || include_level.zero?
  return @discovered_include_level = 10 if include_level >= 10
  @discovered_include_level = include_level + 1
end

.discovered_includesObject



93
94
95
# File 'lib/contentful_model/base.rb', line 93

def discovered_includes
  @discovered_includes ||= []
end

.include_discovered(klass) ⇒ Object

Add a class to the known referenced classes



115
116
117
118
119
120
121
# File 'lib/contentful_model/base.rb', line 115

def include_discovered(klass)
  # This should be nil already in most cases,
  # but if another class is defined in Runtime after a query was already run, we want to make sure this is reset
  @discovered_include_level = nil

  discovered_includes << klass unless discovered_includes.include?(klass)
end

.mapping?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/contentful_model/base.rb', line 123

def mapping?
  ContentfulModel.configuration.entry_mapping.key?(@content_type_id)
end

.return_nil_for_empty(*nillable_fields) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/contentful_model/base.rb', line 169

def return_nil_for_empty(*nillable_fields)
  @return_nil_for_empty_attribute_fields ||= []

  nillable_fields.each do |field|
    define_method field do
      begin
        result = super()
        result.present? ? result : nil
      rescue NoMethodError
        nil
      end
    end

    @return_nil_for_empty_attribute_fields.push(field)
  end
end

Instance Method Details

#cache_key(*timestamp_names) ⇒ Object



24
25
26
27
28
# File 'lib/contentful_model/base.rb', line 24

def cache_key(*timestamp_names)
  fail ArgumentError, "ContentfulModel::Base models don't support named timestamps." if timestamp_names.present?

  "#{self.class.to_s.underscore}/#{id}-#{updated_at.utc.strftime(TIME_FORMAT)}"
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/contentful_model/base.rb', line 34

def eql?(other)
  super || other.instance_of?(self.class) && sys[:id].present? && other.sys[:id] == sys[:id]
end

#hashObject



30
31
32
# File 'lib/contentful_model/base.rb', line 30

def hash
  "#{sys[:content_type].id}-#{sys[:id]}".hash
end