Sha256: 8922c42b431d748386f05a61bf44c515e32301c891d6dc125db55488e14a16ab

Contents?: true

Size: 1.67 KB

Versions: 7

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module Blacklight
  class FieldRetriever
    # @param [SolrDocument] document
    # @param [Blacklight::Configuration::Field] field_config solr field configuration
    def initialize(document, field_config)
      @document = document
      @field_config = field_config
    end

    attr_reader :document, :field_config
    delegate :field, to: :field_config

    # @return [Array]
    def fetch
      Array.wrap(
        if field_config.highlight
          retrieve_highlight
        elsif field_config.accessor
          retieve_using_accessor
        elsif field_config
          retrieve_simple
        end
      )
    end

    private

    def retrieve_simple
      # regular document field
      if field_config.default && field_config.default.is_a?(Proc)
        document.fetch(field_config.field, &field_config.default)
      else
        document.fetch(field_config.field, field_config.default)
      end
    end

    def retieve_using_accessor
      # implicit method call
      if field_config.accessor == true
        document.send(field)
      # arity-1 method call (include the field name in the call)
      elsif !field_config.accessor.is_a?(Array) && document.method(field_config.accessor).arity.nonzero?
        document.send(field_config.accessor, field)
      # chained method calls
      else
        Array(field_config.accessor).inject(document) do |result, method|
          result.send(method)
        end
      end
    end

    def retrieve_highlight
      # retrieve the document value from the highlighting response
      document.highlight_field(field_config.field).map(&:html_safe) if document.has_highlight_field? field_config.field
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
blacklight-7.3.0 app/services/blacklight/field_retriever.rb
blacklight-7.2.0 app/services/blacklight/field_retriever.rb
blacklight-7.1.0 app/services/blacklight/field_retriever.rb
blacklight-7.1.0.alpha app/services/blacklight/field_retriever.rb
blacklight-7.0.1 app/services/blacklight/field_retriever.rb
blacklight-7.0.0 app/services/blacklight/field_retriever.rb
blacklight-7.0.0.rc2 app/services/blacklight/field_retriever.rb