Module: Blacklight::Document

Extended by:
ActiveSupport::Concern, Deprecation
Includes:
CacheKey, Export, SchemaOrg, SemanticFields
Included in:
Solr::Document
Defined in:
app/models/concerns/blacklight/document.rb,
app/models/concerns/blacklight/document/semantic_fields.rb,
app/models/concerns/blacklight/document/active_model_shim.rb

Overview

Introduction

Blacklight::Document is the module with logic for a class representing an individual document returned from Solr results. It can be added in to any local class you want, but in default Blacklight a SolrDocument class is provided for you which is pretty much a blank class “include”ing Blacklight::Document.

Blacklight::Document provides some DefaultFinders.

It also provides support for Document Extensions, which advertise supported transformation formats.

Defined Under Namespace

Modules: ActiveModelShim, CacheKey, ClassMethods, DublinCore, Email, Export, Extensions, SchemaOrg, SemanticFields, Sms

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Export

#export_as, #export_formats, #exports_as?, #will_export_as

Methods included from CacheKey

#cache_key, #cache_version_key

Methods included from SemanticFields

#to_semantic_values

Methods included from SchemaOrg

#itemtype

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &b) ⇒ Object

the wrapper method to the @_source object. If a method is missing, it gets sent to @_source with all of the original params and block



62
63
64
65
66
67
68
69
70
71
# File 'app/models/concerns/blacklight/document.rb', line 62

def method_missing(m, *args, &b)
  return super if m == :to_hash

  if _source_responds_to?(m)
    Deprecation.warn(Blacklight::Solr::Document, "Blacklight::Document##{m} is deprecated; use obj.to_h.#{m} instead.")
    _source.send(m, *args, &b)
  else
    super
  end
end

Instance Attribute Details

#_sourceObject (readonly)

Returns the value of attribute _source.



44
45
46
# File 'app/models/concerns/blacklight/document.rb', line 44

def _source
  @_source
end

#responseObject (readonly) Also known as: solr_response

Returns the value of attribute response.



44
45
46
# File 'app/models/concerns/blacklight/document.rb', line 44

def response
  @response
end

Instance Method Details

#fetch(key, *default) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'app/models/concerns/blacklight/document.rb', line 107

def fetch key, *default
  if key? key
    self[key]
  elsif default.empty? and !block_given?
    raise KeyError, "key not found \"#{key}\""
  else
    (yield(self) if block_given?) || default.first
  end
end

#first(key) ⇒ Object



117
118
119
# File 'app/models/concerns/blacklight/document.rb', line 117

def first key
  Array(self[key]).first
end

#has?(k, *values) ⇒ Boolean Also known as: has_field?

Helper method to check if value/multi-values exist for a given key. The value can be a string, or a RegExp Multiple “values” can be given; only one needs to match.

Example: doc.has?(:location_facet) doc.has?(:location_facet, 'Clemons') doc.has?(:id, 'h009', /^u/i)

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/concerns/blacklight/document.rb', line 86

def has?(k, *values)
  if !key?(k)
    false
  elsif values.empty?
    self[k].present?
  else
    Array(values).any? do |expected|
      Array(self[k]).any? do |actual|
        case expected
        when Regexp
          actual =~ expected
        else
          actual == expected
        end
      end
    end
  end
end

#has_highlight_field?(k) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'app/models/concerns/blacklight/document.rb', line 125

def has_highlight_field? k
  false
end

#highlight_field(k) ⇒ Object



129
130
131
# File 'app/models/concerns/blacklight/document.rb', line 129

def highlight_field k
  nil
end

#initialize(source_doc = {}, response = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'app/models/concerns/blacklight/document.rb', line 48

def initialize(source_doc={}, response=nil)
  @_source = if source_doc.respond_to?(:to_hash)
               ActiveSupport::HashWithIndifferentAccess.new(source_doc)
             else
               Deprecation.warn(Blacklight::Document, "Blacklight::Document#initialize expects a hash-like object, received #{source_doc.class}.")
               ActiveSupport::HashWithIndifferentAccess.new(source_doc.to_h)
             end
  @response = response
  apply_extensions
end

#more_like_thisObject

Implementations that support More-Like-This should override this method to return an array of documents that are like this one.



136
137
138
# File 'app/models/concerns/blacklight/document.rb', line 136

def more_like_this
  []
end

#respond_to_missing?(m, *args) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'app/models/concerns/blacklight/document.rb', line 73

def respond_to_missing? m, *args
  return super if %i(empty? to_hash).include?(m)
  _source_responds_to?(m, *args) || super
end

#to_partial_pathObject



121
122
123
# File 'app/models/concerns/blacklight/document.rb', line 121

def to_partial_path
  'catalog/document'
end