Sha256: 375f399b5fe9b89265cc1d92107f094fd9a7de46ab3eec65249026c9ade73e92

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

module PhraseApp
  module InContextEditor
    class KeyNamesCache
      attr_reader :api_wrapper

      def initialize(api_wrapper)
        @api_wrapper = api_wrapper
      end

      def pre_cached(key_names)
        unless cache.cached?(:translation_key_names)
          cache.set(:translation_key_names, prefetched_key_names)
        end

        return key_names.select { |key_name| key_name_precached?(key_name) }
      end

      def pre_fetched(key_names)
        key_names.select { |key_name| covered_by_initial_caching?(key_name) }
      end

      private

      def prefetched_key_names
        prefetched = Set.new
        PhraseApp::InContextEditor.cache_key_segments_initial.each do |prefix|
          api_wrapper.keys_with_prefix(prefix).each do |key|
            prefetched.add(key.name)
          end
        end
        prefetched
      end

      def covered_by_initial_caching?(key_name)
        key_name.start_with?(*PhraseApp::InContextEditor.cache_key_segments_initial)
      end

      def key_name_is_in_cache?(key_name)
        cache.get(:translation_key_names).include?(key_name)
      end

      def key_name_precached?(key_name)
        covered = covered_by_initial_caching?(key_name)
        in_cache = key_name_is_in_cache?(key_name)
        return covered && in_cache
      end

      def cache
        RequestStore.store[:phraseapp_cache] ||= PhraseApp::InContextEditor::Cache.new
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
phraseapp-in-context-editor-ruby-1.4.0 lib/phraseapp-in-context-editor-ruby/key_names_cache.rb
phraseapp-in-context-editor-ruby-1.3.1 lib/phraseapp-in-context-editor-ruby/key_names_cache.rb
phraseapp-in-context-editor-ruby-1.3.0 lib/phraseapp-in-context-editor-ruby/key_names_cache.rb