Class: ContentfulModel::Base

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations

included

Methods included from ChainableQueries

included

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

use method_missing to call fields on the model



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

def method_missing(method, *args, &block)
  result = fields[:"#{method.to_s.camelize(:lower)}"]
  if result.nil?
    super
  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.



49
50
51
# File 'lib/contentful_model/base.rb', line 49

def coercions
  @coercions
end

.content_type_idObject

Returns the value of attribute content_type_id.



49
50
51
# File 'lib/contentful_model/base.rb', line 49

def content_type_id
  @content_type_id
end

Class Method Details

.add_entry_mappingObject



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

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



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

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

.coerce_field(coercions_hash) ⇒ Object



70
71
72
# File 'lib/contentful_model/base.rb', line 70

def coerce_field(coercions_hash)
  self.coercions = coercions_hash
end

.content_typeObject



66
67
68
# File 'lib/contentful_model/base.rb', line 66

def content_type
  client.content_type(@content_type_id)
end

.descendentsObject



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

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

Instance Method Details

#cache_key(*timestamp_names) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/contentful_model/base.rb', line 33

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

#respond_to_missing?(method, private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(method, private=false)
  if fields[:"#{method.to_s.camelize(:lower)}"].nil?
     super
  else
    true
  end
end

#saveObject Also known as: create

Raises:

  • (NotImplementedError)


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

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