Class: Valkyrie::Persistence::Solr::ModelConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/persistence/solr/model_converter.rb

Overview

Responsible for converting a Resource into hashes for indexing into Solr.

Defined Under Namespace

Classes: BooleanPropertyValue, CompositeSolrRow, DateTimePropertyValue, EnumerableValue, FloatPropertyValue, IDPropertyValue, IntegerPropertyValue, LiteralPropertyValue, NestedObjectValue, Property, SharedStringPropertyValue, SolrMapperValue, SolrRow, StringPropertyValue, URIPropertyValue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, resource_factory:) ⇒ ModelConverter

Returns a new instance of ModelConverter.

Parameters:



11
12
13
14
# File 'lib/valkyrie/persistence/solr/model_converter.rb', line 11

def initialize(resource, resource_factory:)
  @resource = resource
  @resource_factory = resource_factory
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



6
7
8
# File 'lib/valkyrie/persistence/solr/model_converter.rb', line 6

def resource
  @resource
end

#resource_factoryObject (readonly)

Returns the value of attribute resource_factory.



6
7
8
# File 'lib/valkyrie/persistence/solr/model_converter.rb', line 6

def resource_factory
  @resource_factory
end

Instance Method Details

#convert!Hash

Note:

this modifies the Solr Document for the conversion

Converts the Valkyrie Resource to the Solr Document

Returns:

  • (Hash)

    the Solr Document for the Valkyrie Resource



19
20
21
22
23
# File 'lib/valkyrie/persistence/solr/model_converter.rb', line 19

def convert!
  # Appends the resource type to the Solr Document
  to_h.merge(Valkyrie::Persistence::Solr::Queries::MODEL.to_sym => [resource.internal_resource])
      .merge(indexer_solr(resource))
end

#created_atString

Returns ISO-8601 timestamp in UTC of the created_at for this solr document.

Returns:

  • (String)

    ISO-8601 timestamp in UTC of the created_at for this solr document.



40
41
42
43
44
45
46
# File 'lib/valkyrie/persistence/solr/model_converter.rb', line 40

def created_at
  if resource_attributes[:created_at]
    DateTime.parse(resource_attributes[:created_at].to_s).utc.iso8601
  else
    Time.current.utc.iso8601(6)
  end
end

#idString

Access the ID for the Valkyrie Resource being converted to a Solr Document

Returns:

  • (String)

    The solr document ID



34
35
36
# File 'lib/valkyrie/persistence/solr/model_converter.rb', line 34

def id
  resource.id.to_s
end

#indexer_solr(resource) ⇒ Hash

Generate the Solr Document for a Valkyrie Resource using the indexer

Parameters:

Returns:

  • (Hash)

    the Solr Document as a Hash



28
29
30
# File 'lib/valkyrie/persistence/solr/model_converter.rb', line 28

def indexer_solr(resource)
  resource_indexer.new(resource: resource).to_solr
end

#to_hHash

Returns Solr document to index.

Returns:

  • (Hash)

    Solr document to index.



57
58
59
60
61
62
63
64
# File 'lib/valkyrie/persistence/solr/model_converter.rb', line 57

def to_h
  {
    "id": id,
    "join_id_ssi": "id-#{id}",
    "created_at_dtsi": created_at,
    "updated_at_dtsi": updated_at
  }.merge(add_single_values(attribute_hash)).merge(lock_hash)
end

#updated_atString

Note:

Solr stores its own updated_at timestamp, but for performance

reasons we’re generating our own. Without doing so, every time we add a new document we’d have to do a GET to find out the timestamp.

Returns:

  • (String)

    ISO-8601 timestamp in UTC of the updated_at for solr



52
53
54
# File 'lib/valkyrie/persistence/solr/model_converter.rb', line 52

def updated_at
  Time.current.utc.iso8601(6)
end