Class: Wings::Valkyrie::QueryService
- Inherits:
-
Object
- Object
- Wings::Valkyrie::QueryService
- Extended by:
- Forwardable
- Defined in:
- lib/wings/valkyrie/query_service.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
Instance Method Summary collapse
-
#custom_queries ⇒ Valkyrie::Persistence::CustomQueryContainer
Constructs a Valkyrie::Persistence::CustomQueryContainer using this query service.
-
#find_all ⇒ Array<Valkyrie::Resource>
Find all work/collection records, and map to Valkyrie Resources.
-
#find_all_of_model(model:) ⇒ Array<Valkyrie::Resource>
Find all work/collection records of a given model, and map to Valkyrie Resources.
-
#find_by(id:) ⇒ Valkyrie::Resource
WARNING: In general, prefer find_by_alternate_identifier over this method.
-
#find_by_alternate_identifier(alternate_identifier:, use_valkyrie: true) ⇒ Valkyrie::Resource
Find a record using an alternate ID, and map it to a Valkyrie Resource.
-
#find_inverse_references_by(resource: nil, id: nil, property:) ⇒ Array<Valkyrie::Resource>
Get all resources which link to a resource or id with a given property.
-
#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>
Find an array of record using Valkyrie IDs, and map them to Valkyrie Resources maintaining order based on given ids.
-
#find_members(resource:, model: nil) ⇒ Array<Valkyrie::Resource>
Find all members of a given resource, and map to Valkyrie Resources.
-
#find_parents(resource:) ⇒ Array<Valkyrie::Resource>
Find all parents of a given resource.
-
#find_references_by(resource:, property:) ⇒ Array<Valkyrie::Resource>
Find the Valkyrie Resources referenced by another Valkyrie Resource.
-
#initialize(adapter:) ⇒ QueryService
constructor
A new instance of QueryService.
Constructor Details
#initialize(adapter:) ⇒ QueryService
11 12 13 |
# File 'lib/wings/valkyrie/query_service.rb', line 11 def initialize(adapter:) @adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
6 7 8 |
# File 'lib/wings/valkyrie/query_service.rb', line 6 def adapter @adapter end |
Instance Method Details
#custom_queries ⇒ Valkyrie::Persistence::CustomQueryContainer
Constructs a Valkyrie::Persistence::CustomQueryContainer using this query service
157 158 159 |
# File 'lib/wings/valkyrie/query_service.rb', line 157 def custom_queries @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self) end |
#find_all ⇒ Array<Valkyrie::Resource>
Find all work/collection records, and map to Valkyrie Resources
35 36 37 38 39 |
# File 'lib/wings/valkyrie/query_service.rb', line 35 def find_all ::ActiveFedora::Base.all.map do |obj| resource_factory.to_resource(object: obj) end end |
#find_all_of_model(model:) ⇒ Array<Valkyrie::Resource>
Find all work/collection records of a given model, and map to Valkyrie Resources
44 45 46 47 48 |
# File 'lib/wings/valkyrie/query_service.rb', line 44 def find_all_of_model(model:) model_class_for(model).all.map do |obj| resource_factory.to_resource(object: obj) end end |
#find_by(id:) ⇒ Valkyrie::Resource
WARNING: In general, prefer find_by_alternate_identifier over this method.
Hyrax uses a shortened noid in place of an id, and this is what is stored in ActiveFedora, which is still the storage backend for Hyrax.
If you do not heed this warning, then switch to Valyrie’s Postgres MetadataAdapter, but continue passing noids to find_by, you will start getting ObjectNotFoundErrors instead of the objects you wanted
Find a record using a Valkyrie ID, and map it to a Valkyrie Resource
29 30 31 |
# File 'lib/wings/valkyrie/query_service.rb', line 29 def find_by(id:) find_by_alternate_identifier(alternate_identifier: id) end |
#find_by_alternate_identifier(alternate_identifier:, use_valkyrie: true) ⇒ Valkyrie::Resource
Find a record using an alternate ID, and map it to a Valkyrie Resource
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/wings/valkyrie/query_service.rb', line 81 def find_by_alternate_identifier(alternate_identifier:, use_valkyrie: true) raise(ArgumentError, 'id must be a Valkyrie::ID') unless alternate_identifier.respond_to?(:to_str) af_object = ActiveFedora::Base.find(alternate_identifier.to_s) use_valkyrie ? resource_factory.to_resource(object: af_object) : af_object rescue ActiveFedora::ObjectNotFoundError, Ldp::Gone => err raise err unless use_valkyrie raise Hyrax::ObjectNotFoundError end |
#find_inverse_references_by(resource: nil, id: nil, property:) ⇒ Array<Valkyrie::Resource>
Get all resources which link to a resource or id with a given property.
132 133 134 135 136 137 138 139 140 |
# File 'lib/wings/valkyrie/query_service.rb', line 132 def find_inverse_references_by(resource: nil, id: nil, property:) raise ArgumentError, "Provide resource or id" unless resource || id id ||= resource.alternate_ids.first raise ArgumentError, "Resource has no id; is it persisted?" unless id uri = Hyrax::Base.id_to_uri(id.to_s) ActiveFedora::Base.where("+(#{property}_ssim: \"#{uri}\" OR #{property}_ssim: \"#{id}\")").map do |obj| resource_factory.to_resource(object: obj) end end |
#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>
ignores non-existent ids.
Find an array of record using Valkyrie IDs, and map them to Valkyrie Resources maintaining order based on given ids
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/wings/valkyrie/query_service.rb', line 60 def find_many_by_ids(ids:) ids.all? { |i| i.respond_to?(:to_str) } || raise(ArgumentError, 'id must be a Valkyrie::ID') return enum_for(:find_many_by_ids, ids: ids) unless block_given? ids.map(&:to_s).uniq.each do |id| begin af_object = ActiveFedora::Base.find(id) yield resource_factory.to_resource(object: af_object) rescue ::ActiveFedora::ObjectNotFoundError, Ldp::Gone next end end end |
#find_members(resource:, model: nil) ⇒ Array<Valkyrie::Resource>
Find all members of a given resource, and map to Valkyrie Resources
97 98 99 100 101 102 103 104 |
# File 'lib/wings/valkyrie/query_service.rb', line 97 def find_members(resource:, model: nil) return [] if resource.try(:member_ids).blank? return find_many_by_ids(ids: resource.member_ids) unless model find_model = model_class_for(model) find_many_by_ids(ids: resource.member_ids) .select { |member_resource| model_class_for(member_resource.class) == find_model } end |
#find_parents(resource:) ⇒ Array<Valkyrie::Resource>
Find all parents of a given resource.
148 149 150 151 152 153 |
# File 'lib/wings/valkyrie/query_service.rb', line 148 def find_parents(resource:) id = resource.alternate_ids.first ActiveFedora::Base.where("member_ids_ssim: \"#{id}\"").map do |obj| resource_factory.to_resource(object: obj) end end |
#find_references_by(resource:, property:) ⇒ Array<Valkyrie::Resource>
Find the Valkyrie Resources referenced by another Valkyrie Resource
110 111 112 113 114 115 116 117 118 |
# File 'lib/wings/valkyrie/query_service.rb', line 110 def find_references_by(resource:, property:) object = resource_factory.from_resource(resource: resource) object.send(property).map do |reference| af_id = find_id_for(reference) resource_factory.to_resource(object: ::ActiveFedora::Base.find(af_id)) end rescue ActiveFedora::ObjectNotFoundError [] end |