Class: Valkyrie::Persistence::Solr::ORMConverter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(solr_document, resource_factory:) ⇒ ORMConverter

Returns a new instance of ORMConverter.

Parameters:



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_factoryObject (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_documentObject (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_hashHash

Note:

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

#attributesHash

Derive the Valkyrie attributes from the Solr Document

Returns:

  • (Hash)


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

Parameters:

  • hsh (Hash)

Returns:

  • (Hash)


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

Returns:



16
17
18
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 16

def convert!
  resource
end

#created_atTime

Construct a Time object from the datestamp for the resource creation date indexed in Solr

Returns:

  • (Time)


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

#idString

Note:

this assumes that the ID has been prepended with the string “id-”

Retrieve the ID for the Valkyrie Resource in the Solr Document

Returns:

  • (String)


76
77
78
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 76

def id
  solr_document.fetch('id').sub(/^id-/, '')
end

#internal_resourceString

Access the String specifying the Valkyrie Resource type in the Solr Document

Returns:

  • (String)


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

#resourceValkyrie::Resource

Construct the Valkyrie Resource using attributes derived from the Solr Document

Returns:



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_klassClass

Access the Class for the Valkyrie Resource

Returns:

  • (Class)


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

Parameters:

  • hsh (Hash)

Returns:

  • (Hash)

See Also:



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

#tokenValkyrie::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_atTime

Construct a Time object from the datestamp for the date of the last resource update indexed in Solr

Returns:

  • (Time)


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

#versionString

Access the “version” field value within the Solr Document

Returns:

  • (String)


69
70
71
# File 'lib/valkyrie/persistence/solr/orm_converter.rb', line 69

def version
  solr_document.fetch('_version_', nil)
end