Sha256: 64ec973a6fb8500e5350a54f95281216231f9a52d472ec678eec9b9357708655

Contents?: true

Size: 1.84 KB

Versions: 37

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module Valkyrie::Persistence
  # Allows for implementors to register and use custom queries on a
  #  per persister basis
  #
  # @example Custom Query Class
  #
  #     # Snippet from custom query class see: https://github.com/pulibrary/figgy/blob/d0b1305a1564c2aa4e7d6c1e99f0c2a88ed673f4/app/queries/find_by_string_property.rb
  #     class FindByStringProperty
  #       def self.queries
  #         [:find_by_string_property]
  #       end
  #
  #       ...
  #
  #       def initialize(query_service:)
  #         @query_service = query_service
  #       end
  #       ...
  #
  #       def find_by_string_property(property:, value:)
  #         internal_array = "{\"#{property}\": [\"#{value}\"]}"
  #         run_query(query, internal_array)
  #       end
  #       ...
  #     end
  #
  # @example Registration
  #
  #   # in config/initializers/valkyrie.rb
  #   [FindByStringProperty].each do |query_handler|
  #     Valkyrie.config.metadata_adapter.query_service.custom_queries.register_query_handler(query_handler)
  #   end
  #
  # @see lib/valkyrie/persistence/solr/query_service.rb for use of this class
  #
  class CustomQueryContainer
    attr_reader :query_service, :query_handlers
    def initialize(query_service:)
      @query_service = query_service
      @query_handlers = []
    end

    def register_query_handler(query_handler)
      @query_handlers << query_handler
    end

    def method_missing(meth_name, *args, &block)
      query_handler = find_query_handler(meth_name).new(query_service: query_service)
      return super unless query_handler
      query_handler.__send__(meth_name, *args, &block)
    end

    def find_query_handler(method)
      query_handlers.find { |x| x.queries.include?(method) }
    end

    def respond_to_missing?(meth_name, _args)
      find_query_handler(meth_name).present?
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
valkyrie-2.2.0 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.1.2 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.1.1 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.1.0 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.0.2 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.0.1 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.0.0 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.0.0.RC10 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.0.0.RC9 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-1.7.1 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.0.0.RC8 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-1.7.0 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.0.0.RC7 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.0.0.RC6 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.0.0.RC5 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.0.0.RC4 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-1.6.0 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-1.5.1 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-2.0.0.RC3 lib/valkyrie/persistence/custom_query_container.rb
valkyrie-1.5.0 lib/valkyrie/persistence/custom_query_container.rb