Class: ContentfulModel::Base
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
included
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 self.class.coercions[method].nil?
return result
elsif self.class.coercions[method].is_a?(Proc)
return self.class.coercions[method].call(result)
elsif !self.class::COERCIONS[self.class.coercions[method]].nil?
return self.class::COERCIONS[self.class.coercions[method]].call(result)
else
return result
end
end
end
|
Class Attribute Details
.coercions ⇒ Object
Returns the value of attribute coercions.
59
60
61
|
# File 'lib/contentful_model/base.rb', line 59
def coercions
@coercions
end
|
.content_type_id ⇒ Object
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_mapping ⇒ Object
.client ⇒ Object
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_type ⇒ Object
76
77
78
|
# File 'lib/contentful_model/base.rb', line 76
def content_type
client.content_type(@content_type_id)
end
|
.descendents ⇒ Object
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
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
|
#save ⇒ Object
Also known as:
create
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
|