Class: BlacklightOaiProvider::SolrSet

Inherits:
Set
  • Object
show all
Defined in:
lib/blacklight_oai_provider/solr_set.rb

Instance Attribute Summary collapse

Attributes inherited from Set

#description, #label, #value

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ SolrSet

Build a set object with, at minimum, a set spec string

Raises:

  • (OAI::ArgumentException)


67
68
69
70
71
72
73
# File 'lib/blacklight_oai_provider/solr_set.rb', line 67

def initialize(spec)
  super(spec)
  config = self.class.field_config_for(label)
  @solr_field = config[:solr_field]
  @description = config[:description]
  raise OAI::ArgumentException if @solr_field.blank?
end

Instance Attribute Details

#solr_fieldObject

OAI Set properties



64
65
66
# File 'lib/blacklight_oai_provider/solr_set.rb', line 64

def solr_field
  @solr_field
end

Class Method Details

.allObject

Return an array of all sets, or nil if sets are not supported



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/blacklight_oai_provider/solr_set.rb', line 5

def all
  return if @fields.nil?

  params = { rows: 0, facet: true, 'facet.field' => solr_fields }
  solr_fields.each { |field| params["f.#{field}.facet.limit"] = -1 } # override any potential blacklight limits

  builder = @controller.search_builder.merge(params)
  response = @controller.repository.search(builder)

  sets_from_facets(response.facet_fields) if response.facet_fields
end

.field_config_for(label) ⇒ Object



42
43
44
# File 'lib/blacklight_oai_provider/solr_set.rb', line 42

def field_config_for(label)
  Array.wrap(@fields).find { |f| f[:label] == label } || {}
end

.fields=(value) ⇒ Object

The Solr fields to map to OAI sets. Must be indexed



31
32
33
34
35
36
37
38
39
40
# File 'lib/blacklight_oai_provider/solr_set.rb', line 31

def fields=(value) # The Solr fields to map to OAI sets. Must be indexed
  if value.respond_to?(:each)
    value.each do |config|
      raise ArgumentException, 'OAI sets must define a solr_field' if config[:solr_field].blank?
      config[:label] ||= config[:solr_field]
    end
  end

  super(value)
end

.from_spec(spec) ⇒ Object

Return a Solr filter query given a set spec



18
19
20
# File 'lib/blacklight_oai_provider/solr_set.rb', line 18

def from_spec(spec)
  new(spec).solr_filter
end

.sets_for(record) ⇒ Object

Returns array of sets for a solr document, or empty array if none are available.



23
24
25
26
27
28
29
# File 'lib/blacklight_oai_provider/solr_set.rb', line 23

def sets_for(record)
  Array.wrap(@fields).map do |field|
    record.fetch(field[:solr_field], []).map do |value|
      new("#{field[:label]}:#{value}")
    end
  end.flatten
end

Instance Method Details

#nameObject



75
76
77
# File 'lib/blacklight_oai_provider/solr_set.rb', line 75

def name
  spec.titleize.gsub(':', ': ')
end

#solr_filterObject



83
84
85
# File 'lib/blacklight_oai_provider/solr_set.rb', line 83

def solr_filter
  "#{@solr_field}:\"#{@value}\""
end

#specObject



79
80
81
# File 'lib/blacklight_oai_provider/solr_set.rb', line 79

def spec
  "#{@label}:#{@value}"
end