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 ⇒ Object
-
#find_all ⇒ Array<Valkyrie::Resource>
All objects in the persistence backend.
-
#find_all_of_model(model:) ⇒ Array<Valkyrie::Resource>
All objects in the persistence backend with the given class.
-
#find_by(id:) ⇒ Valkyrie::Resource
The object being searched for.
-
#find_by_alternate_identifier(alternate_identifier:) ⇒ Valkyrie::Resource
The object being searched for.
-
#find_inverse_references_by(resource:, property:) ⇒ Array<Valkyrie::Resource>
All resources in the persistence backend which have the ID of the given ‘resource` in their `property` property.
-
#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>
All requested objects that were found.
-
#find_members(resource:, model: nil) ⇒ Array<Valkyrie::Resource>
Child objects of type ‘model` referenced by `resource`’s ‘member_ids` method.
-
#find_parents(resource:) ⇒ Array<Valkyrie::Resource>
All resources which are parents of the given ‘resource`.
-
#find_references_by(resource:, property:) ⇒ Array<Valkyrie::Resource>
All objects which are referenced by the ‘property` property on `resource`.
-
#initialize(adapter:) ⇒ QueryService
constructor
A new instance of QueryService.
Constructor Details
#initialize(adapter:) ⇒ QueryService
Returns a new instance of QueryService.
11 12 13 14 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 11 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 ⇒ Object
118 119 120 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 118 def custom_queries @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self) end |
#find_all ⇒ Array<Valkyrie::Resource>
Returns All objects in the persistence backend.
52 53 54 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 52 def find_all cache.values end |
#find_all_of_model(model:) ⇒ Array<Valkyrie::Resource>
Returns All objects in the persistence backend with the given class.
59 60 61 62 63 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 59 def find_all_of_model(model:) cache.values.select do |obj| obj.is_a?(model) end end |
#find_by(id:) ⇒ Valkyrie::Resource
Returns The object being searched for.
21 22 23 24 25 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 21 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
Returns The object being searched for.
32 33 34 35 36 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 32 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 { |_key, resource| resource['alternate_ids'].include?(alternate_identifier) }.values.first || raise(::Valkyrie::Persistence::ObjectNotFoundError) end |
#find_inverse_references_by(resource:, property:) ⇒ Array<Valkyrie::Resource>
Returns All resources in the persistence backend which have the ID of the given ‘resource` in their `property` property. Not in order.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 96 def find_inverse_references_by(resource:, property:) ensure_persisted(resource) find_all.select do |obj| begin Array.wrap(obj[property]).include?(resource.id) rescue false end end end |
#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>
Returns All requested objects that were found.
41 42 43 44 45 46 47 48 49 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 41 def find_many_by_ids(ids:) 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>
Returns child objects of type ‘model` referenced by `resource`’s ‘member_ids` method. Returned in order.
69 70 71 72 73 74 75 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 69 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>
Returns All resources which are parents of the given ‘resource`. This means the resource’s ‘id` appears in their `member_ids` array.
112 113 114 115 116 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 112 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>
Returns All objects which are referenced by the ‘property` property on `resource`. Not necessarily in order.
82 83 84 85 86 |
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 82 def find_references_by(resource:, property:) Array.wrap(resource[property]).map do |id| find_by(id: id) end end |