Class: Valkyrie::Persistence::Solr::ORMConverter
- Inherits:
-
Object
- Object
- Valkyrie::Persistence::Solr::ORMConverter
- Defined in:
- lib/valkyrie/persistence/solr/orm_converter.rb
Overview
Responsible for converting hashes from Solr into a Resource
Defined Under Namespace
Classes: BooleanValue, DateTimeValue, EnumerableValue, FloatValue, IDValue, IntegerValue, NestedEnumerable, NestedResourceConverter, NestedResourceHash, NestedResourceID, NestedResourceLiteral, NestedResourceURI, NestedResourceValue, Property, PropertyValue, RDFLiteralPropertyValue, SolrValue, URIValue
Instance Attribute Summary collapse
-
#resource_factory ⇒ Object
readonly
Returns the value of attribute resource_factory.
-
#solr_document ⇒ Object
readonly
Returns the value of attribute solr_document.
Instance Method Summary collapse
-
#attribute_hash ⇒ Hash
Construct the Hash containing the Valkyrie Resource attributes using the Solr Document.
-
#attributes ⇒ Hash
Derive the Valkyrie attributes from the Solr Document.
-
#build_literals(hsh) ⇒ Hash
Populates an existing Hash with SolrValue objects keyed to the existing Hash keys.
-
#convert! ⇒ Valkyrie::Resource
Converts the Solr Document into a Valkyrie Resource.
-
#created_at ⇒ Time
Construct a Time object from the datestamp for the resource creation date indexed in Solr.
-
#id ⇒ String
Retrieve the ID for the Valkyrie Resource in the Solr Document.
-
#initialize(solr_document, resource_factory:) ⇒ ORMConverter
constructor
A new instance of ORMConverter.
-
#internal_resource ⇒ String
Access the String specifying the Valkyrie Resource type in the Solr Document.
-
#resource ⇒ Valkyrie::Resource
Construct the Valkyrie Resource using attributes derived from the Solr Document.
-
#resource_klass ⇒ Class
Access the Class for the Valkyrie Resource.
-
#strip_tsim(hsh) ⇒ Hash
Removes the substring “_tsim” within Hash keys This is used when mapping Solr Document Hashes into Valkyrie Resource attributes.
-
#token ⇒ Valkyrie::Persistence::OptimisticLockToken
Construct the OptimisticLockToken object using the “version” field value in the Solr Document.
-
#updated_at ⇒ Time
Construct a Time object from the datestamp for the date of the last resource update indexed in Solr.
-
#version ⇒ String
Access the “version” field value within the Solr Document.
Constructor Details
#initialize(solr_document, resource_factory:) ⇒ ORMConverter
Returns a new instance of ORMConverter.
9 10 11 12 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 9 def initialize(solr_document, resource_factory:) @solr_document = solr_document @resource_factory = resource_factory end |
Instance Attribute Details
#resource_factory ⇒ Object (readonly)
Returns the value of attribute resource_factory.
5 6 7 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 5 def resource_factory @resource_factory end |
#solr_document ⇒ Object (readonly)
Returns the value of attribute solr_document.
5 6 7 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 5 def solr_document @solr_document end |
Instance Method Details
#attribute_hash ⇒ Hash
this filters for attributes which have been indexed as stored multivalued texts (tsim)
Construct the Hash containing the Valkyrie Resource attributes using the Solr Document
85 86 87 88 89 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 85 def attribute_hash build_literals(strip_tsim(solr_document.select do |k, _v| k.end_with?("tsim") end)) end |
#attributes ⇒ Hash
Derive the Valkyrie attributes from the Solr Document
40 41 42 43 44 45 46 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 40 def attributes attribute_hash.merge("id" => id, internal_resource: internal_resource, created_at: created_at, updated_at: updated_at, Valkyrie::Persistence::Attributes::OPTIMISTIC_LOCK => token) end |
#build_literals(hsh) ⇒ Hash
Populates an existing Hash with SolrValue objects keyed to the existing Hash keys
121 122 123 124 125 126 127 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 121 def build_literals(hsh) hsh.each_with_object({}) do |(key, value), output| next if key.end_with?("_lang") next if key.end_with?("_type") output[key] = SolrValue.for(Property.new(key, value, hsh)).result end end |
#convert! ⇒ Valkyrie::Resource
Converts the Solr Document into a Valkyrie Resource
16 17 18 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 16 def convert! resource end |
#created_at ⇒ Time
Construct a Time object from the datestamp for the resource creation date indexed in Solr
50 51 52 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 50 def created_at DateTime.parse(solr_document.fetch("created_at_dtsi").to_s).new_offset(0) end |
#id ⇒ String
this assumes that the ID has been prepended with the string “id-”
Retrieve the ID for the Valkyrie Resource in the Solr Document
76 77 78 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 76 def id solr_document.fetch('id').sub(/^id-/, '') end |
#internal_resource ⇒ String
Access the String specifying the Valkyrie Resource type in the Solr Document
34 35 36 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 34 def internal_resource solr_document.fetch(Valkyrie::Persistence::Solr::Queries::MODEL).first end |
#resource ⇒ Valkyrie::Resource
Construct the Valkyrie Resource using attributes derived from the Solr Document
22 23 24 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 22 def resource resource_klass.new(attributes.symbolize_keys.merge(new_record: false)) end |
#resource_klass ⇒ Class
Access the Class for the Valkyrie Resource
28 29 30 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 28 def resource_klass Valkyrie.config.resource_class_resolver.call(internal_resource) end |
#strip_tsim(hsh) ⇒ Hash
Removes the substring “_tsim” within Hash keys This is used when mapping Solr Document Hashes into Valkyrie Resource attributes
96 97 98 99 100 101 102 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 96 def strip_tsim(hsh) Hash[ hsh.map do |k, v| [k.sub("_tsim", ""), v] end ] end |
#token ⇒ Valkyrie::Persistence::OptimisticLockToken
Construct the OptimisticLockToken object using the “version” field value in the Solr Document
63 64 65 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 63 def token Valkyrie::Persistence::OptimisticLockToken.new(adapter_id: resource_factory.adapter_id, token: version) end |
#updated_at ⇒ Time
Construct a Time object from the datestamp for the date of the last resource update indexed in Solr
56 57 58 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 56 def updated_at DateTime.parse((solr_document["updated_at_dtsi"] || solr_document["timestamp"] || solr_document["created_at_dtsi"]).to_s).utc end |
#version ⇒ String
Access the “version” field value within the Solr Document
69 70 71 |
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 69 def version solr_document.fetch('_version_', nil) end |