Class: Valkyrie::Persistence::CustomQueryContainer
- Inherits:
-
Object
- Object
- Valkyrie::Persistence::CustomQueryContainer
- Defined in:
- lib/valkyrie/persistence/custom_query_container.rb
Overview
Allows for implementors to register and use custom queries on a
per persister basis
Instance Attribute Summary collapse
-
#query_handlers ⇒ Object
readonly
Returns the value of attribute query_handlers.
-
#query_service ⇒ Object
readonly
Returns the value of attribute query_service.
Instance Method Summary collapse
- #find_query_handler(method) ⇒ Object
-
#initialize(query_service:) ⇒ CustomQueryContainer
constructor
A new instance of CustomQueryContainer.
- #method_missing(meth_name, *args, &block) ⇒ Object
- #register_query_handler(query_handler) ⇒ Object
- #respond_to_missing?(meth_name, _args) ⇒ Boolean
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_handlers ⇒ Object (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_service ⇒ Object (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
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 |