class ContentfulModel::Base
Wrapper for Contentful::Entry which provides ActiveModel-like syntax
Constants
- TIME_FORMAT
Time format for Cache Key
Attributes
client[RW]
coercions[RW]
content_type_id[RW]
return_nil_for_empty_attribute_fields[RW]
Public Class Methods
new(*)
click to toggle source
Calls superclass method
ContentfulModel::Manageable::new
# File lib/contentful_model/base.rb, line 19 def initialize(*) super override_getters end
Private Class Methods
add_entry_mapping()
click to toggle source
# File lib/contentful_model/base.rb, line 127 def add_entry_mapping ContentfulModel.configuration.entry_mapping[@content_type_id] = to_s.constantize unless mapping? end
coerce_field(*coercions)
click to toggle source
# File lib/contentful_model/base.rb, line 145 def coerce_field(*coercions) @coercions ||= {} coercions.each do |coercions_hash| @coercions.merge!(coercions_hash) end @coercions end
coerce_value(field_name, value)
click to toggle source
# File lib/contentful_model/base.rb, line 153 def coerce_value(field_name, value) return value if coercions.nil? coercion = coercions[field_name] case coercion when Symbol, String coercion = Contentful::Field::KNOWN_TYPES[coercion.to_s] return coercion.new(value).coerce unless coercion.nil? when Proc coercion[value] else value end end
content_type()
click to toggle source
# File lib/contentful_model/base.rb, line 141 def content_type client.content_type(@content_type_id) end
descendents()
click to toggle source
# File lib/contentful_model/base.rb, line 89 def descendents ObjectSpace.each_object(Class).select { |klass| klass < self } end
discovered_include_level()
click to toggle source
# File lib/contentful_model/base.rb, line 97 def discovered_include_level @discovered_include_level ||= nil return @discovered_include_level unless @discovered_include_level.nil? # Recreate content type tree includes = {} discovered_includes.each do |klass| # Recursively find includes - remove self from reference lists includes[klass] = klass.constantize.discovered_includes.reject { |i| i == to_s } + [klass] end include_level = includes.values.map(&:size).max # Longest include chain return @discovered_include_level = 1 if include_level.nil? || include_level.zero? return @discovered_include_level = 10 if include_level >= 10 @discovered_include_level = include_level + 1 end
discovered_includes()
click to toggle source
# File lib/contentful_model/base.rb, line 93 def discovered_includes @discovered_includes ||= [] end
include_discovered(klass)
click to toggle source
Add a class to the known referenced classes
# File lib/contentful_model/base.rb, line 115 def include_discovered(klass) # This should be nil already in most cases, # but if another class is defined in Runtime after a query was already run, we want to make sure this is reset @discovered_include_level = nil discovered_includes << klass unless discovered_includes.include?(klass) end
mapping?()
click to toggle source
# File lib/contentful_model/base.rb, line 123 def mapping? ContentfulModel.configuration.entry_mapping.key?(@content_type_id) end
return_nil_for_empty(*nillable_fields)
click to toggle source
Calls superclass method
# File lib/contentful_model/base.rb, line 169 def return_nil_for_empty(*nillable_fields) @return_nil_for_empty_attribute_fields ||= [] nillable_fields.each do |field| define_method field do begin result = super() result.present? ? result : nil rescue NoMethodError nil end end @return_nil_for_empty_attribute_fields.push(field) end end
Public Instance Methods
cache_key(*timestamp_names)
click to toggle source
# File lib/contentful_model/base.rb, line 24 def cache_key(*timestamp_names) fail ArgumentError, "ContentfulModel::Base models don't support named timestamps." if timestamp_names.present? "#{self.class.to_s.underscore}/#{id}-#{updated_at.utc.strftime(TIME_FORMAT)}" end
eql?(other)
click to toggle source
Calls superclass method
# File lib/contentful_model/base.rb, line 34 def eql?(other) super || other.instance_of?(self.class) && sys[:id].present? && other.sys[:id] == sys[:id] end
hash()
click to toggle source
# File lib/contentful_model/base.rb, line 30 def hash "#{sys[:content_type].id}-#{sys[:id]}".hash end
Private Instance Methods
define_fields_methods()
click to toggle source
# File lib/contentful_model/base.rb, line 61 def define_fields_methods fields.keys.each do |name| define_singleton_method name do result = filter_invalids( self.class.coerce_value(name, fields[name]) ) return nil if nillable?(name, result) result end end end
define_sys_methods()
click to toggle source
# File lib/contentful_model/base.rb, line 40 def define_sys_methods sys.keys.each do |name| define_singleton_method name do self.class.coerce_value(name, sys[name]) end end end
filter_invalids(value)
click to toggle source
# File lib/contentful_model/base.rb, line 48 def filter_invalids(value) return value.reject { |v| v.is_a?(Contentful::Link) || (v.respond_to?(:invalid?) && v.invalid?) } if value.is_a?(Array) return nil if value.is_a?(Contentful::Link) || value.respond_to?(:fields) && value.fields.empty? value end
nillable?(name, value)
click to toggle source
# File lib/contentful_model/base.rb, line 55 def nillable?(name, value) value.nil? && self.class.return_nil_for_empty_attribute_fields && self.class.return_nil_for_empty_attribute_fields.include?(name) end
override_getters()
click to toggle source
# File lib/contentful_model/base.rb, line 75 def override_getters define_sys_methods define_fields_methods end
respond_to_missing?(method, private = false)
click to toggle source
Calls superclass method
# File lib/contentful_model/base.rb, line 80 def respond_to_missing?(method, private = false) key = fields.keys.detect { |k| k.to_s.underscore == method.to_s.underscore } return super if key.nil? true end