Class: Valkyrie::Persistence::Memory::QueryService
- Inherits:
-
Object
- Object
- Valkyrie::Persistence::Memory::QueryService
- Defined in:
- lib/valkyrie/persistence/memory/query_service.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Query Service for the memory metadata adapter.
-
#query_handlers ⇒ Object
readonly
Query Service for the memory metadata adapter.
Instance Method Summary collapse
-
#custom_queries ⇒ Valkyrie::Persistence::CustomQueryContainer
Get the set of custom queries configured for this query service.
-
#find_all ⇒ Array<Valkyrie::Resource>
Get all objects.
-
#find_all_of_model(model:) ⇒ Array<Valkyrie::Resource>
Get all objects of a given model.
-
#find_by(id:) ⇒ Valkyrie::Resource
Get a single resource by ID.
-
#find_by_alternate_identifier(alternate_identifier:) ⇒ Valkyrie::Resource
Get a single resource by ‘alternate_identifier`.
-
#find_inverse_references_by(resource: nil, id: nil, property:) ⇒ Array<Valkyrie::Resource>
Get all resources which link to a resource with a given property.
-
#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>
Get a batch of resources by ID.
-
#find_members(resource:, model: nil) ⇒ Array<Valkyrie::Resource>
Get all members of a given resource.
-
#find_parents(resource:) ⇒ Array<Valkyrie::Resource>
Find all parents of a given resource.
-
#find_references_by(resource:, property:) ⇒ Array<Valkyrie::Resource>
Get all resources referenced from a resource with a given property.
-
#initialize(adapter:) ⇒ QueryService
constructor
A new instance of QueryService.
Constructor Details
#initialize(adapter:) ⇒ QueryService
Many query service methods are part of Valkyrie’s public API, but instantiation itself is not
Returns a new instance of QueryService.
13 14 15 16 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 13 def initialize(adapter:) @adapter = adapter @query_handlers = [] end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Documentation for Query Services in general is maintained here.
Query Service for the memory metadata adapter.
7 8 9 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 7 def adapter @adapter end |
#query_handlers ⇒ Object (readonly)
Documentation for Query Services in general is maintained here.
Query Service for the memory metadata adapter.
7 8 9 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 7 def query_handlers @query_handlers end |
Instance Method Details
#custom_queries ⇒ Valkyrie::Persistence::CustomQueryContainer
Get the set of custom queries configured for this query service.
139 140 141 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 139 def custom_queries @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self) end |
#find_all ⇒ Array<Valkyrie::Resource>
Get all objects.
62 63 64 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 62 def find_all cache.values end |
#find_all_of_model(model:) ⇒ Array<Valkyrie::Resource>
Get all objects of a given model.
70 71 72 73 74 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 70 def find_all_of_model(model:) cache.values.select do |obj| obj.is_a?(model) end end |
#find_by(id:) ⇒ Valkyrie::Resource
Get a single resource by ID.
24 25 26 27 28 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 24 def find_by(id:) id = Valkyrie::ID.new(id.to_s) if id.is_a?(String) validate_id(id) cache[id] || raise(::Valkyrie::Persistence::ObjectNotFoundError) end |
#find_by_alternate_identifier(alternate_identifier:) ⇒ Valkyrie::Resource
Get a single resource by ‘alternate_identifier`.
36 37 38 39 40 41 42 43 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 36 def find_by_alternate_identifier(alternate_identifier:) alternate_identifier = Valkyrie::ID.new(alternate_identifier.to_s) if alternate_identifier.is_a?(String) validate_id(alternate_identifier) cache.select do |_key, resource| next unless resource[:alternate_ids] resource[:alternate_ids].include?(alternate_identifier) end.values.first || raise(::Valkyrie::Persistence::ObjectNotFoundError) end |
#find_inverse_references_by(resource: nil, id: nil, property:) ⇒ Array<Valkyrie::Resource>
Get all resources which link to a resource with a given property.
116 117 118 119 120 121 122 123 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 116 def find_inverse_references_by(resource: nil, id: nil, property:) raise ArgumentError, "Provide resource or id" unless resource || id ensure_persisted(resource) if resource id ||= resource.id find_all.select do |obj| Array.wrap(obj[property]).include?(id) end end |
#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>
Get a batch of resources by ID.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 49 def find_many_by_ids(ids:) ids = ids.uniq if adapter.standardize_query_result? ids.map do |id| begin find_by(id: id) rescue ::Valkyrie::Persistence::ObjectNotFoundError nil end end.reject(&:nil?) end |
#find_members(resource:, model: nil) ⇒ Array<Valkyrie::Resource>
Get all members of a given resource.
81 82 83 84 85 86 87 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 81 def find_members(resource:, model: nil) result = member_ids(resource: resource).map do |id| find_by(id: id) end return result unless model result.select { |obj| obj.is_a?(model) } end |
#find_parents(resource:) ⇒ Array<Valkyrie::Resource>
Find all parents of a given resource.
131 132 133 134 135 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 131 def find_parents(resource:) cache.values.select do |record| member_ids(resource: record).include?(resource.id) end end |
#find_references_by(resource:, property:) ⇒ Array<Valkyrie::Resource>
Get all resources referenced from a resource with a given property.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 95 def find_references_by(resource:, property:) refs = Array.wrap(resource[property]).map do |id| begin find_by(id: id) rescue ::Valkyrie::Persistence::ObjectNotFoundError nil end end.reject(&:nil?) refs.uniq! if adapter.standardize_query_result? && !ordered_property?(resource: resource, property: property) refs end |