Sha256: 42658df706575841dc747c8fda55de729132cb8ed4161c06fa745d61c9b78324

Contents?: true

Size: 1.77 KB

Versions: 16

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module Blacklight::Document
  module SemanticFields
    extend ActiveSupport::Concern

    class_methods do
      # Class-level method for accessing/setting semantic mappings
      # for solr stored fields. Can be set by local app, key is
      # a symbol for a semantic, value is a solr _stored_ field.
      #
      # Stored field can be single or multi-value. In some cases
      # clients may only use the first value from a multi-value field.
      #
      # Currently documented semantic tokens, not all may be
      # used by core BL, but some may be used by plugins present
      # or future.
      # :title, :author, :year, :language => User-presentable strings.
      def field_semantics
        @field_semantics ||= {}
      end
    end

    # Returns a hash keyed by semantic tokens, value is an array of
    # strings. (Array to handle multi-value fields). If no value(s)
    # available, empty array is returned.
    #
    # Default implementation here uses field_semantics
    # to just take values from Solr stored fields.
    # Extensions can over-ride this method to provide better/different lookup,
    # but extensions should call super and modify hash returned, to avoid
    # unintentionally erasing values provided by other extensions.
    def to_semantic_values
      @semantic_value_hash ||= self.class.field_semantics.each_with_object(Hash.new([])) do |(key, field_names), hash|
        ##
        # Handles single string field_name or an array of field_names
        value = Array.wrap(field_names).map { |field_name| self[field_name] }.flatten.compact

        # Make single and multi-values all arrays, so clients
        # don't have to know.
        hash[key] = value unless value.empty?
      end

      @semantic_value_hash ||= {}
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/blacklight-8.3.0/app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.3.0 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.2.2 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.2.1 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.2.0 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.1.0 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.0.1 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.0.0 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.0.0.beta8 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.0.0.beta7 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.0.0.beta6 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.0.0.beta5 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.0.0.beta4 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.0.0.beta3 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.0.0.beta2 app/models/concerns/blacklight/document/semantic_fields.rb
blacklight-8.0.0.beta1 app/models/concerns/blacklight/document/semantic_fields.rb