class WCC::Contentful::Store::MemoryStore

The MemoryStore is the most naiive store implementation and a good reference point for more useful implementations. It only implements equality queries and does not support querying through an association.

Public Class Methods

new() click to toggle source
Calls superclass method WCC::Contentful::Store::Base::new
# File lib/wcc/contentful/store/memory_store.rb, line 10
def initialize
  super
  @hash = {}
end

Public Instance Methods

delete(key) click to toggle source
# File lib/wcc/contentful/store/memory_store.rb, line 25
def delete(key)
  mutex.with_write_lock do
    @hash.delete(key)
  end
end
execute(query) click to toggle source
# File lib/wcc/contentful/store/memory_store.rb, line 41
def execute(query)
  relation = mutex.with_read_lock { @hash.values }

  # relation is an enumerable that we apply conditions to in the form of
  #  Enumerable#select and Enumerable#reject.
  relation =
    relation.lazy.reject do |v|
      value_content_type = v.try(:dig, 'sys', 'contentType', 'sys', 'id')
      if query.content_type == 'Asset'
        !value_content_type.nil?
      else
        value_content_type != query.content_type
      end
    end

  # For each condition, we apply a new Enumerable#select with a block that
  # enforces the condition.
  query.conditions.reduce(relation) do |memo, condition|
    memo.select do |entry|
      # Our naiive implementation only supports equality operator
      raise ArgumentError, "Operator #{condition.op} not supported" unless condition.op == :eq

      # The condition's path tells us where to find the value in the JSON object
      val = entry.dig(*condition.path)

      # For arrays, equality is defined as does the array include the expected value.
      # See https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/array-equality-inequality
      if val.is_a? Array
        val.include?(condition.expected)
      else
        val == condition.expected
      end
    end
  end
end
find(key, **_options) click to toggle source
# File lib/wcc/contentful/store/memory_store.rb, line 35
def find(key, **_options)
  mutex.with_read_lock do
    @hash[key]
  end
end
keys() click to toggle source
# File lib/wcc/contentful/store/memory_store.rb, line 31
def keys
  mutex.with_read_lock { @hash.keys }
end
set(key, value) click to toggle source
# File lib/wcc/contentful/store/memory_store.rb, line 15
def set(key, value)
  value = value.deep_dup.freeze
  ensure_hash value
  mutex.with_write_lock do
    old = @hash[key]
    @hash[key] = value
    old
  end
end