Class: Valkyrie::Persistence::Fedora::QueryService
- Inherits:
-
Object
- Object
- Valkyrie::Persistence::Fedora::QueryService
- Defined in:
- lib/valkyrie/persistence/fedora/query_service.rb
Overview
Query Service for Fedora MetadataAdapter
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
Instance Method Summary collapse
- #content_with_inbound(id:) ⇒ Object
- #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`.
- #include_uris ⇒ Object
-
#initialize(adapter:) ⇒ QueryService
constructor
A new instance of QueryService.
Constructor Details
#initialize(adapter:) ⇒ QueryService
Returns a new instance of QueryService.
7 8 9 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 7 def initialize(adapter:) @adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
5 6 7 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 5 def adapter @adapter end |
Instance Method Details
#content_with_inbound(id:) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 93 def content_with_inbound(id:) uri = adapter.id_to_uri(id) connection.get(uri) do |req| prefer_headers = Ldp::PreferHeaders.new(req.headers["Prefer"]) prefer_headers.include = prefer_headers.include | include_uris req.headers["Prefer"] = prefer_headers.to_s end end |
#custom_queries ⇒ Object
113 114 115 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 113 def custom_queries @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self) end |
#find_all ⇒ Array<Valkyrie::Resource>
This requires iterating over every resource in Fedora.
Returns All objects in the persistence backend.
67 68 69 70 71 72 73 74 75 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 67 def find_all resource = Ldp::Resource.for(connection, adapter.base_path, connection.get(adapter.base_path)) ids = resource.graph.query([nil, RDF::Vocab::LDP.contains, nil]).map(&:object).map { |x| adapter.uri_to_id(x) } ids.lazy.map do |id| find_by(id: id) end rescue ::Ldp::NotFound [] end |
#find_all_of_model(model:) ⇒ Array<Valkyrie::Resource>
This requires iterating over every resource in Fedora.
Returns All objects in the persistence backend with the given class.
80 81 82 83 84 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 80 def find_all_of_model(model:) find_all.select do |m| m.is_a?(model) end end |
#find_by(id:) ⇒ Valkyrie::Resource
Returns The object being searched for.
12 13 14 15 16 17 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 12 def find_by(id:) validate_id(id) uri = adapter.id_to_uri(id) resource_from_uri(uri) end |
#find_by_alternate_identifier(alternate_identifier:) ⇒ Valkyrie::Resource
Returns The object being searched for.
20 21 22 23 24 25 26 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 20 def find_by_alternate_identifier(alternate_identifier:) validate_id(alternate_identifier) uri = adapter.id_to_uri(alternate_identifier) alternate_id = resource_from_uri(uri).references find_by(id: alternate_id) 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.
103 104 105 106 107 108 109 110 111 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 103 def find_inverse_references_by(resource:, property:) ensure_persisted(resource) content = content_with_inbound(id: resource.id) property_uri = adapter.schema.predicate_for(property: property, resource: nil) ids = content.graph.query([nil, property_uri, nil]).map(&:subject).map { |x| x.to_s.gsub(/#.*/, '') }.map { |x| adapter.uri_to_id(x) } ids.lazy.map do |id| find_by(id: id) end end |
#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>
Returns All requested objects that were found.
29 30 31 32 33 34 35 36 37 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 29 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.
55 56 57 58 59 60 61 62 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 55 def find_members(resource:, model: nil) return [] unless resource.respond_to? :member_ids result = Array(resource.member_ids).lazy.map do |id| find_by(id: id) end.select(&:present?) 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.
40 41 42 43 44 45 46 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 40 def find_parents(resource:) content = content_with_inbound(id: resource.id) parent_ids = content.graph.query([nil, RDF::Vocab::ORE.proxyFor, nil]).map(&:subject).map { |x| x.to_s.gsub(/#.*/, '') }.map { |x| adapter.uri_to_id(x) } parent_ids.lazy.map do |id| find_by(id: 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.
87 88 89 90 91 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 87 def find_references_by(resource:, property:) (resource[property] || []).select { |x| x.is_a?(Valkyrie::ID) }.lazy.map do |id| find_by(id: id) end end |
#include_uris ⇒ Object
48 49 50 51 52 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 48 def include_uris [ ::RDF::Vocab::Fcrepo4.InboundReferences ] end |