app/models/concerns/blacklight/document.rb in blacklight-6.25.0 vs app/models/concerns/blacklight/document.rb in blacklight-7.0.0.rc1

- old
+ new

@@ -1,10 +1,8 @@ # frozen_string_literal: true - -require 'globalid' - ## +## # = 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 @@ -14,13 +12,10 @@ # # It also provides support for Document Extensions, which advertise supported # transformation formats. # module Blacklight::Document - extend Deprecation - self.deprecation_horizon = 'blacklight 7.0' - autoload :ActiveModelShim, 'blacklight/document/active_model_shim' autoload :SchemaOrg, 'blacklight/document/schema_org' autoload :CacheKey, 'blacklight/document/cache_key' autoload :DublinCore, 'blacklight/document/dublin_core' autoload :Email, 'blacklight/document/email' @@ -41,42 +36,19 @@ include GlobalID::Identification end attr_reader :response, :_source alias_method :solr_response, :response - delegate :[], :key?, :keys, :to_h, to: :_source - 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 + delegate :[], :key?, :keys, :to_h, :as_json, to: :_source + + def initialize(source_doc = {}, response = nil) + @_source = ActiveSupport::HashWithIndifferentAccess.new(source_doc).freeze @response = response apply_extensions end - # 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 - 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 - - def respond_to_missing? m, *args - return super if %i(empty? to_hash).include?(m) - _source_responds_to?(m, *args) || super - end - # 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: @@ -105,11 +77,11 @@ alias has_key? key? def fetch key, *default if key? key self[key] - elsif default.empty? and !block_given? + elsif default.empty? && !block_given? raise KeyError, "key not found \"#{key}\"" else (yield(self) if block_given?) || default.first end end @@ -120,15 +92,15 @@ def to_partial_path 'catalog/document' end - def has_highlight_field? k + def has_highlight_field? _k false end - def highlight_field k + def highlight_field _k nil end ## # Implementations that support More-Like-This should override this method @@ -158,13 +130,7 @@ def attribute(name, type, field) define_method name do type.coerce(self[field]) end end - end - - private - - def _source_responds_to? *args - _source && self != _source && _source.respond_to?(*args) end end