Sha256: d684490c0c234cc85df04ba058b5d12ee80c2551434078e3a4a2a343b4279aed

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module WCC::Contentful::Store
  class MemoryStore < Base
    def initialize
      super
      @hash = {}
    end

    def set(key, value)
      value = value.deep_dup.freeze
      mutex.with_write_lock do
        old = @hash[key]
        @hash[key] = value
        old
      end
    end

    def delete(key)
      mutex.with_write_lock do
        @hash.delete(key)
      end
    end

    def keys
      mutex.with_read_lock { @hash.keys }
    end

    def find(key)
      mutex.with_read_lock do
        @hash[key]
      end
    end

    def find_all(content_type:)
      relation = mutex.with_read_lock { @hash.values }

      relation =
        relation.reject do |v|
          value_content_type = v.dig('sys', 'contentType', 'sys', 'id')
          value_content_type.nil? || value_content_type != content_type
        end
      Query.new(relation)
    end

    class Query < Base::Query
      def result
        @relation.dup
      end

      def initialize(relation)
        @relation = relation
      end

      def eq(field, expected, context = nil)
        locale = context[:locale] if context.present?
        locale ||= 'en-US'
        Query.new(@relation.select do |v|
          val = v.dig('fields', field, locale)
          if val.is_a? Array
            val.include?(expected)
          else
            val == expected
          end
        end)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wcc-contentful-0.2.2 lib/wcc/contentful/store/memory_store.rb
wcc-contentful-0.2.1 lib/wcc/contentful/store/memory_store.rb
wcc-contentful-0.2.0 lib/wcc/contentful/store/memory_store.rb