Sha256: 6f6f01a86aa50fb3c40bc545ea68c58d69dccc1df2ef7d3caaa2e3c5c7520c94

Contents?: true

Size: 1.65 KB

Versions: 7

Compression:

Stored size: 1.65 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
      ensure_hash value
      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, **_options)
      mutex.with_read_lock do
        @hash[key]
      end
    end

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

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

    class Query < Base::Query
      def result
        return @relation.dup unless @options[:include]

        @relation.map { |e| resolve_includes(e, @options[:include]) }
      end

      def initialize(store, relation, options = nil)
        super(store)
        @relation = relation
        @options = options || {}
      end

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
wcc-contentful-0.4.0.pre.rc lib/wcc/contentful/store/memory_store.rb
wcc-contentful-0.4.0.pre.beta lib/wcc/contentful/store/memory_store.rb
wcc-contentful-0.4.0.pre.alpha lib/wcc/contentful/store/memory_store.rb
wcc-contentful-0.3.0 lib/wcc/contentful/store/memory_store.rb
wcc-contentful-0.3.0.pre.rc3 lib/wcc/contentful/store/memory_store.rb
wcc-contentful-0.3.0.pre.rc2 lib/wcc/contentful/store/memory_store.rb
wcc-contentful-0.3.0.pre.rc lib/wcc/contentful/store/memory_store.rb