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
24
25
26
27
28
29
30
31
32
33
# File 'lib/contentful_model/base.rb', line 12

def method_missing(method, *args, &block)
  result = fields[:"#{method.to_s.camelize(:lower)}"]
  if result.nil?
    raise ContentfulModel::AttributeNotFoundError, "no attribute #{method} found"
  else
    # if there's no coercion specified, return the result
    if self.class.coercions[method].nil?
      return result

    #if there's a coercion specified for the field and it's a proc, pass the result
    #to the proc
    elsif self.class.coercions[method].is_a?(Proc)
      return self.class.coercions[method].call(result)
    #provided the coercion is in the COERCIONS constant, call the proc on that
    elsif !self.class::COERCIONS[self.class.coercions[method]].nil?
      return self.class::COERCIONS[self.class.coercions[method]].call(result)
    else
      #... or just return the result
      return result
    end
  end
end

Class Attribute Details

.coercionsObject

Returns the value of attribute coercions.



59
60
61
# File 'lib/contentful_model/base.rb', line 59

def coercions
  @coercions
end

.content_type_idObject

Returns the value of attribute content_type_id.



59
60
61
# File 'lib/contentful_model/base.rb', line 59

def content_type_id
  @content_type_id
end

Class Method Details

.add_entry_mappingObject



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

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



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

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

.coerce_field(*coercions) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/contentful_model/base.rb', line 80

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

.content_typeObject



76
77
78
# File 'lib/contentful_model/base.rb', line 76

def content_type
  client.content_type(@content_type_id)
end

.descendentsObject



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

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

Instance Method Details

#cache_key(*timestamp_names) ⇒ Object



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

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)


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

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)


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

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