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

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

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.



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)

Note:

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_handlersObject (readonly)

Note:

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_queriesValkyrie::Persistence::CustomQueryContainer

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



138
139
140
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 138

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

#find_allArray<Valkyrie::Resource>

Get all objects.



59
60
61
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 59

def find_all
  cache.values
end

#find_all_of_model(model:) ⇒ Array<Valkyrie::Resource>

Get all objects of a given model.



67
68
69
70
71
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 67

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.

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.

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



36
37
38
39
40
# 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 { |_key, resource| resource['alternate_ids'].include?(alternate_identifier) }.values.first || raise(::Valkyrie::Persistence::ObjectNotFoundError)
end

#find_inverse_references_by(resource:, property:) ⇒ Array<Valkyrie::Resource>

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

Raises:

  • (ArgumentError)

    Raised when the ID is not in the persistence backend.



113
114
115
116
117
118
119
120
121
122
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 113

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>

Get a batch of resources by ID.

Raises:

  • (ArgumentError)

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



46
47
48
49
50
51
52
53
54
55
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 46

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.



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

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.



130
131
132
133
134
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 130

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.



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/valkyrie/persistence/memory/query_service.rb', line 92

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