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
|
# 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
.coercions ⇒ Object
Returns the value of attribute coercions.
49
50
51
|
# File 'lib/contentful_model/base.rb', line 49
def coercions
@coercions
end
|
.content_type_id ⇒ Object
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_mapping ⇒ Object
.client ⇒ Object
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_type ⇒ Object
66
67
68
|
# File 'lib/contentful_model/base.rb', line 66
def content_type
client.content_type(@content_type_id)
end
|
.descendents ⇒ Object
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
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
|
#save ⇒ Object
Also known as:
create
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
|