Class: LD4L::OreRDF::FindAggregations

Inherits:
Object
  • Object
show all
Defined in:
lib/ld4l/ore_rdf/services/aggregation/find.rb

Class Method Summary collapse

Class Method Details

.call(options = {}) ⇒ Object

Find aggregations matching the specified criteria.

Examples:

Search for all aggregations

all_aggs = LD4L::OreRDF.FindAggregations

Parameters:

  • options (Hash) (defaults to: {})

    the options to use to find aggregations

Options Hash (options):

  • :criteria (Hash<Object,Object>)

    for finding aggregations (ex. RDF::DC.title=>‘My Aggregation’) (default - nil for all aggregations)

  • :properties (Hash<Symbol><Object>)

    to return with the aggregation uri (ex. :first_proxy=>RDFVocabularies::IANA.first) (default - nil aggregation uri only)

  • :repository (Symbol)

    to search (default - :default)

  • :resume (TrueClass, FalseClass)

    if true, find and resume; otherwise, find only (default - false)

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ld4l/ore_rdf/services/aggregation/find.rb', line 20

def self.call( options={} )
  repository = options[:repository] || :default
  raise ArgumentError, "repository (#{repository}) is not a symbol" unless repository.kind_of?(Symbol)
  raise ArgumentError, "repository (#{repository}) is not a registered repository" unless
      ActiveTriples::Repositories.repositories.has_key?(repository)

  resume = options[:resume] || false
  raise ArgumentError, 'resume must be true or false' unless
      resume.kind_of?(TrueClass) || resume.kind_of?(FalseClass)

  criteria = options[:criteria] || nil
  raise ArgumentError, 'criteria must be a hash of attribute-value pairs for searching for aggregations'  unless
      criteria.nil? || criteria.kind_of?(Hash)
  criteria = criteria ? criteria.dup : {}
  criteria[RDF.type] = RDFVocabularies::ORE.Aggregation

  # properties are ignored when resume==true because all properties are returned as part of the resumed aggregations
  properties = options[:properties] || nil
  raise ArgumentError, 'properties must be an array of predicates'  unless
      properties.nil? || properties.kind_of?(Hash) || resume
  properties.each_key { |k| criteria[properties[k]] = k unless criteria.has_key?(properties[k]) }  unless
      properties.nil? || resume
  process_properties = properties.nil? || resume ? false : true

  graph = ActiveTriples::Repositories.repositories[repository]
  query = RDF::Query.new({ :aggregation => criteria })

  process_properties || resume ? aggregations = {} : aggregations = []
  results = query.execute(graph)
  results.each do |r|
    h = r.to_hash
    uri = h[:aggregation]
    if resume
      # if resume, return Hash of aggregation uri => resumed aggregation for each found
      aggregations[uri] = LD4L::OreRDF::ResumeAggregation.call(uri)
    elsif process_properties
      # if properties, return Hash of aggregation uri => Hash of property => value for each found
      properties = h
      properties.delete(:aggregation)
      aggregations[uri] = properties
    else
      # if no properties && not resumed, return array of aggregation uris
      aggregations << uri
    end
  end

  aggregations
end