Sha256: a7c1e3ecec84cb5561d76913b12c1159b09ccc2a6cfae5356ea6eac0b69d35e4

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 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
        Thread.current[: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.2.1 lib/phraseapp-in-context-editor-ruby/key_names_cache.rb
phraseapp-in-context-editor-ruby-1.2.0 lib/phraseapp-in-context-editor-ruby/key_names_cache.rb
phraseapp-in-context-editor-ruby-1.1.0 lib/phraseapp-in-context-editor-ruby/key_names_cache.rb