Class: Valkyrie::Persistence::CustomQueryContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/persistence/custom_query_container.rb

Overview

Allows for implementors to register and use custom queries on a

per persister basis

Examples:

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

Registration


# in config/initializers/valkyrie.rb
[FindByStringProperty].each do |query_handler|
  Valkyrie.config..query_service.custom_queries.register_query_handler(query_handler)
end

See Also:

  • for use of this class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_service:) ⇒ CustomQueryContainer

Returns a new instance of CustomQueryContainer.



40
41
42
43
# File 'lib/valkyrie/persistence/custom_query_container.rb', line 40

def initialize(query_service:)
  @query_service = query_service
  @query_handlers = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth_name, *args, &block) ⇒ Object



49
50
51
52
53
# File 'lib/valkyrie/persistence/custom_query_container.rb', line 49

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

Instance Attribute Details

#query_handlersObject (readonly)

Returns the value of attribute query_handlers.



39
40
41
# File 'lib/valkyrie/persistence/custom_query_container.rb', line 39

def query_handlers
  @query_handlers
end

#query_serviceObject (readonly)

Returns the value of attribute query_service.



39
40
41
# File 'lib/valkyrie/persistence/custom_query_container.rb', line 39

def query_service
  @query_service
end

Instance Method Details

#find_query_handler(method) ⇒ Object



55
56
57
# File 'lib/valkyrie/persistence/custom_query_container.rb', line 55

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

#register_query_handler(query_handler) ⇒ Object



45
46
47
# File 'lib/valkyrie/persistence/custom_query_container.rb', line 45

def register_query_handler(query_handler)
  @query_handlers << query_handler
end

#respond_to_missing?(meth_name, _args) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/valkyrie/persistence/custom_query_container.rb', line 59

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