app/models/concerns/blacklight/document.rb in blacklight-7.33.1 vs app/models/concerns/blacklight/document.rb in blacklight-7.34.0

- old
+ new

@@ -19,10 +19,11 @@ extend ActiveSupport::Concern include Blacklight::Document::SchemaOrg include Blacklight::Document::SemanticFields include Blacklight::Document::CacheKey include Blacklight::Document::Export + include Blacklight::Document::Attributes included do extend ActiveModel::Naming include Blacklight::Document::Extensions include GlobalID::Identification @@ -68,17 +69,24 @@ end end alias has_field? has? alias has_key? key? - def fetch key, *default + NO_DEFAULT_PROVIDED = Object.new # :nodoc: + def fetch key, default = NO_DEFAULT_PROVIDED if key? key self[key] - elsif default.empty? && !block_given? - raise KeyError, "key not found \"#{key}\"" + elsif block_given? + yield(self) if block_given? + elsif default != NO_DEFAULT_PROVIDED + if default.respond_to?(:call) + default.call(self) + else + default + end else - (yield(self) if block_given?) || default.first + raise KeyError, "key not found \"#{key}\"" end end def first key Array(self[key]).first @@ -113,24 +121,8 @@ class_methods do attr_writer :unique_key def unique_key @unique_key ||= 'id' - end - - # Define an attribute reader on a document model - # @example - # class SolrDocument - # include Blacklight::Solr::Document - # attribute :title, Blacklight::Types::String, 'title_tesim' - # end - # - # doc = SolrDocument.new(title_tesim: ["One flew over the cuckoo's nest"]) - # doc.title - # #=> "One flew over the cuckoo's nest" - def attribute(name, type, field) - define_method name do - type.coerce(self[field]) - end end end end