Class: ContentfulRedis::ModelBase
- Inherits:
-
Object
- Object
- ContentfulRedis::ModelBase
- Defined in:
- lib/contentful_redis/model_base.rb
Class Method Summary collapse
- .content_model ⇒ Object
- .define_searchable_fields(*fields) ⇒ Object
- .destroy(id, env = nil) ⇒ Object
- .find(id, env = nil) ⇒ Object
- .find_by(args, env = ContentfulRedis.configuration.default_env || :published) ⇒ Object
- .searchable_fields ⇒ Object
- .space ⇒ Object
- .update(id, env = nil) ⇒ Object
Instance Method Summary collapse
- #content_type ⇒ Object
-
#initialize(model) ⇒ ModelBase
constructor
A new instance of ModelBase.
Constructor Details
#initialize(model) ⇒ ModelBase
Returns a new instance of ModelBase.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/contentful_redis/model_base.rb', line 74 def initialize(model) instance_variable_set(:@id, model['items'].first.dig('sys', 'id')) self.class.send(:attr_reader, :id) entries = entries_as_objects(model) model['items'].first['fields'].each do |key, value| value = case value when Array value.map { |val| entries[val.dig('sys', 'id')] || val } when Hash extract_object_from_hash(model, value, entries) else value end instance_variable_set("@#{key.underscore}", value) end create_searchable_attribute_links if self.class.searchable_fields.any? end |
Class Method Details
.content_model ⇒ Object
53 54 55 56 57 |
# File 'lib/contentful_redis/model_base.rb', line 53 def content_model model_name = name.demodulize "#{model_name[0].downcase}#{model_name[1..-1]}" end |
.define_searchable_fields(*fields) ⇒ Object
63 64 65 |
# File 'lib/contentful_redis/model_base.rb', line 63 def define_searchable_fields(*fields) instance_eval("def searchable_fields; #{fields}; end") end |
.destroy(id, env = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/contentful_redis/model_base.rb', line 38 def destroy(id, env = nil) keys = [] keys << ContentfulRedis::KeyManager.content_model_key(space, request_env(env), 'sys.id': id, content_type: content_model) searchable_fields.each do |field| keys << ContentfulRedis::KeyManager.attribute_index(self, field) end ContentfulRedis.redis.del(*keys) end |
.find(id, env = nil) ⇒ Object
13 14 15 16 17 |
# File 'lib/contentful_redis/model_base.rb', line 13 def find(id, env = nil) parameters = { 'sys.id': id, content_type: content_model } new(ContentfulRedis::Request.new(space, parameters, :get, request_env(env)).call) end |
.find_by(args, env = ContentfulRedis.configuration.default_env || :published) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/contentful_redis/model_base.rb', line 19 def find_by(args, env = ContentfulRedis.configuration.default_env || :published) raise ContentfulRedis::Error::ArgumentError, "#{args} contain fields which are not a declared as a searchable field" unless (args.keys - searchable_fields).empty? id = args.values.map do |value| key = ContentfulRedis::KeyManager.attribute_index(self, value) key.nil? || key.empty? ? nil : ContentfulRedis.redis.get(key) end.compact.first raise ContentfulRedis::Error::RecordNotFound, 'Missing attribute in glossary' if id.nil? find(id, env) end |
.searchable_fields ⇒ Object
59 60 61 |
# File 'lib/contentful_redis/model_base.rb', line 59 def searchable_fields [] end |
.space ⇒ Object
49 50 51 |
# File 'lib/contentful_redis/model_base.rb', line 49 def space ContentfulRedis.configuration.spaces.first[1] end |
.update(id, env = nil) ⇒ Object
32 33 34 35 36 |
# File 'lib/contentful_redis/model_base.rb', line 32 def update(id, env = nil) parameters = { 'sys.id': id, content_type: content_model } new(ContentfulRedis::Request.new(space, parameters, :update, request_env(env)).call) end |
Instance Method Details
#content_type ⇒ Object
96 97 98 |
# File 'lib/contentful_redis/model_base.rb', line 96 def content_type self.class.name.demodulize.underscore end |