Class: Valkyrie::Persistence::Memory::QueryService

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/persistence/memory/query_service.rb

Overview

Note:

Documentation for Query Services in general is maintained here.

Query Service for the memory metadata adapter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:) ⇒ QueryService

Note:

Many query service methods are part of Valkyrie’s public API, but instantiation itself is not

Returns a new instance of QueryService.

Parameters:



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

#adapterObject (readonly)

Returns the value of attribute adapter.



7
8
9
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 7

def adapter
  @adapter
end

#query_handlersObject (readonly)

Returns the value of attribute query_handlers.



7
8
9
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 7

def query_handlers
  @query_handlers
end

Instance Method Details

#count_all_of_model(model:) ⇒ Object

Count all objects of a given model.

Parameters:

  • model (Class)

    Class to query for.

Returns:

  • integer. Count objects in the persistence backend with the given class.



80
81
82
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 80

def count_all_of_model(model:)
  cache.values.count { |obj| obj.is_a?(model) }
end

#custom_queriesValkyrie::Persistence::CustomQueryContainer

Get the set of custom queries configured for this query service.

Returns:



152
153
154
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 152

def custom_queries
  @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self)
end

#find_allArray<Valkyrie::Resource>

Get all objects.

Returns:



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.

Parameters:

  • model (Class)

    Class to query for.

Returns:

  • (Array<Valkyrie::Resource>)

    All objects in the persistence backend with the given class.



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.

Parameters:

Returns:

Raises:



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`. Alternate identifiers are identifiers (like NOIDs, DOIs, ARKs, etc.) that are not the system-generated ID, but might be used to identify a resource in an application (e.g., to make shorter URLs).

Parameters:

  • alternate_identifier (Valkyrie::ID)

    The alternate identifier to query for.

Returns:

Raises:

  • (Valkyrie::Persistence::ObjectNotFoundError)

    Raised when the alternate identifier isn’t in the persistence backend.

  • (ArgumentError)

    Raised when alternate identifier is not a String or a Valkyrie::ID



38
39
40
41
42
43
44
45
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 38

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:, model: nil) ⇒ Array<Valkyrie::Resource>

Get all resources which link to a resource with a given property.

Parameters:

  • resource (Valkyrie::Resource) (defaults to: nil)

    The resource which is being referenced by other resources. Requires either resource or id parameter to be specified.

  • id (Valkyrie::ID) (defaults to: nil)

    ID of the resource which is being reference by other resources. Requires either resource or id parameter to be specified.

  • property (Symbol)

    The property which, on other resources, is referencing the given ‘resource`

  • model (Class) (defaults to: nil)

    Filter results to include only instances of this model. (optional)

Returns:

  • (Array<Valkyrie::Resource>)

    All resources in the persistence backend which have the ID of the given ‘resource` in their `property` property. Not in order.

Raises:

  • (ArgumentError)

    Raised when the ID is not in the persistence backend.



127
128
129
130
131
132
133
134
135
136
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 127

def find_inverse_references_by(resource: nil, id: nil, property:, model: nil)
  raise ArgumentError, "Provide resource or id" unless resource || id
  ensure_persisted(resource) if resource
  id ||= resource.id
  result = find_all.select do |obj|
    Array.wrap(obj[property]).include?(id)
  end
  return result unless model
  result.select { |obj| obj.is_a?(model) }
end

#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>

Get a batch of resources by ID.

Parameters:

  • ids (Array<Valkyrie::ID, String>)

    The IDs to query for.

Returns:

Raises:

  • (ArgumentError)

    Raised when any ID is not a String or a Valkyrie::ID



51
52
53
54
55
56
57
58
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 51

def find_many_by_ids(ids:)
  ids = ids.uniq
  ids.map do |id|
    find_by(id: id)
  rescue ::Valkyrie::Persistence::ObjectNotFoundError
    nil
  end.reject(&:nil?)
end

#find_members(resource:, model: nil) ⇒ Array<Valkyrie::Resource>

Get all members of a given resource.

Parameters:

  • resource (Valkyrie::Resource)

    Model whose members are being searched for.

  • model (Class) (defaults to: nil)

    Filter results to include only instances of this model. (optional)

Returns:

  • (Array<Valkyrie::Resource>)

    child objects of type ‘model` referenced by `resource`’s ‘member_ids` method. Returned in order.



89
90
91
92
93
94
95
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 89

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.

Parameters:

Returns:

  • (Array<Valkyrie::Resource>)

    All resources which are parents of the given ‘resource`. This means the resource’s ‘id` appears in their `member_ids` array.



144
145
146
147
148
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 144

def find_parents(resource:)
  cache.values.select do |record|
    member_ids(resource: record).include?(resource.id)
  end
end

#find_references_by(resource:, property:, model: nil) ⇒ Array<Valkyrie::Resource>

Get all resources referenced from a resource with a given property.

Parameters:

  • resource (Valkyrie::Resource)

    Model whose property is being searched.

  • property (Symbol)

    Property which, on the ‘resource`, contains IDs which are to be de-referenced.

  • model (Class) (defaults to: nil)

    Filter results to include only instances of this model. (optional)

Returns:

  • (Array<Valkyrie::Resource>)

    All objects which are referenced by the ‘property` property on `resource`. Not necessarily in order.



104
105
106
107
108
109
110
111
112
113
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 104

def find_references_by(resource:, property:, model: nil)
  refs = Array.wrap(resource[property]).map do |id|
    find_by(id: id)
         rescue ::Valkyrie::Persistence::ObjectNotFoundError
           nil
  end.reject(&:nil?)
  refs.uniq! unless ordered_property?(resource: resource, property: property)
  return refs unless model
  refs.select { |obj| obj.is_a?(model) }
end