Class: ContentfulModel::Base

Inherits:
Contentful::Entry
  • Object
show all
Includes:
ChainableQueries
Defined in:
lib/contentful_model/base.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChainableQueries

included

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



5
6
7
8
# File 'lib/contentful_model/base.rb', line 5

def initialize(*args)
  super
  self.class.coercions ||= {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

use method_missing to call fields on the model



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/contentful_model/base.rb', line 11

def method_missing(method)
  result = fields[:"#{method.to_s.camelize(:lower)}"]
  if result.nil?
    raise NoMethodError, "No method or attribute #{method} for #{self}"
  else
    if self.class.coercions[method].nil?
      return result
    else
      return self.class::COERCIONS[self.class.coercions[method]].call(result)
    end
  end
end

Class Attribute Details

.coercionsObject

Returns the value of attribute coercions.



40
41
42
# File 'lib/contentful_model/base.rb', line 40

def coercions
  @coercions
end

.content_type_idObject

Returns the value of attribute content_type_id.



40
41
42
# File 'lib/contentful_model/base.rb', line 40

def content_type_id
  @content_type_id
end

Class Method Details

.add_entry_mappingObject



46
47
48
49
50
# File 'lib/contentful_model/base.rb', line 46

def add_entry_mapping
  unless ContentfulModel.configuration.entry_mapping.has_key?(@content_type_id)
    ContentfulModel.configuration.entry_mapping[@content_type_id] = Object.const_get(self.to_s.to_sym)
  end
end

.clientObject



52
53
54
55
# File 'lib/contentful_model/base.rb', line 52

def client
  self.add_entry_mapping
  @@client ||= Contentful::Client.new(ContentfulModel.configuration.to_hash)
end

.coerce_field(coercions_hash) ⇒ Object



61
62
63
# File 'lib/contentful_model/base.rb', line 61

def coerce_field(coercions_hash)
  self.coercions = coercions_hash
end

.content_typeObject



57
58
59
# File 'lib/contentful_model/base.rb', line 57

def content_type
  client.content_type(@content_type_id)
end

.descendentsObject



42
43
44
# File 'lib/contentful_model/base.rb', line 42

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

Instance Method Details

#cache_key(*timestamp_names) ⇒ Object



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

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

  "#{self.class.to_s.underscore}/#{self.id}-#{self.updated_at.utc.to_s(:number)}"

end

#saveObject Also known as: create

Raises:

  • (NotImplementedError)


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

def save
  raise NotImplementedError, "Saving models isn't implemented; we need to use the Contentful Management API for that. Pull requests welcome!"
end