Class: ContentfulRedis::ModelBase

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful_redis/model_base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ ModelBase

Returns a new instance of ModelBase.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/contentful_redis/model_base.rb', line 71

def initialize(model)
  @id = model['items'].first.dig('sys', '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')] }.compact
            when Hash
              extract_object_from_hash(model, value, entries)
            else
              value
            end

    instance_variable_set("@#{key.underscore}", value) unless value.nil?
  end

  create_searchable_attribute_links if self.class.searchable_fields.any?
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



12
13
14
# File 'lib/contentful_redis/model_base.rb', line 12

def id
  @id
end

Class Method Details

.content_modelObject



50
51
52
53
54
# File 'lib/contentful_redis/model_base.rb', line 50

def content_model
  model_name = name.demodulize

  "#{model_name[0].downcase}#{model_name[1..-1]}"
end

.define_searchable_fields(*fields) ⇒ Object



60
61
62
# File 'lib/contentful_redis/model_base.rb', line 60

def define_searchable_fields(*fields)
  instance_eval("def searchable_fields; #{fields}; end")
end

.destroy(id, env = nil) ⇒ Object



42
43
44
# File 'lib/contentful_redis/model_base.rb', line 42

def destroy(id, env = nil)
  find(id, env).destroy
end

.find(id, env = nil) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/contentful_redis/model_base.rb', line 15

def find(id, env = nil)
  raise ContentfulRedis::Error::ArgumentError, 'Expected Contentful model ID' unless id.is_a?(String)

  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



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/contentful_redis/model_base.rb', line 23

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_fieldsObject



56
57
58
# File 'lib/contentful_redis/model_base.rb', line 56

def searchable_fields
  []
end

.spaceObject



46
47
48
# File 'lib/contentful_redis/model_base.rb', line 46

def space
  ContentfulRedis.configuration.spaces.first[1]
end

.update(id, env = nil) ⇒ Object



36
37
38
39
40
# File 'lib/contentful_redis/model_base.rb', line 36

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_typeObject



110
111
112
# File 'lib/contentful_redis/model_base.rb', line 110

def content_type
  self.class.name.demodulize.underscore
end

#destroyObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/contentful_redis/model_base.rb', line 92

def destroy
  keys = [
    ContentfulRedis::KeyManager.content_model_key(
      self.class.space,
      endpoint,
      'sys.id': id,
      content_type: self.class.content_model,
      include: 1
    )
  ]

  self.class.send(:searchable_fields).each do |field|
    keys << ContentfulRedis::KeyManager.attribute_index(self.class, send(field.to_sym))
  end

  ContentfulRedis.redis.del(*keys)
end