Class: Valkyrie::Persistence::Fedora::Persister::ModelConverter::OrderedProperties

Inherits:
ValueMapper
  • Object
show all
Defined in:
lib/valkyrie/persistence/fedora/persister/model_converter.rb

Defined Under Namespace

Classes: NestedProperty

Instance Attribute Summary

Attributes inherited from ValueMapper

#calling_mapper, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ValueMapper

for, #initialize, register

Constructor Details

This class inherits a constructor from Valkyrie::ValueMapper

Class Method Details

.handles?(value) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/valkyrie/persistence/fedora/persister/model_converter.rb', line 199

def self.handles?(value)
  value.is_a?(Property) && ordered?(value) && !OrderedMembers.handles?(value) && Array(value.value).present? && value.value.is_a?(Array)
end

.ordered?(value) ⇒ Boolean

Returns:

  • (Boolean)


203
204
205
206
# File 'lib/valkyrie/persistence/fedora/persister/model_converter.rb', line 203

def self.ordered?(value)
  return false unless value.resource.class.attribute_names.include?(value.key)
  value.resource.ordered_attribute?(value.key)
end

Instance Method Details

#append_to_graph(obj:, index:, property:) ⇒ Object



278
279
280
281
282
283
# File 'lib/valkyrie/persistence/fedora/persister/model_converter.rb', line 278

def append_to_graph(obj:, index:, property:)
  proxy_node = obj.graph.query([nil, property.predicate, nil]).objects[0]
  obj.graph.delete([nil, property.predicate, nil])
  ordered_list.insert_proxy_for_at(index, proxy_node)
  obj.to_graph(graph)
end

#apply_first_and_lastObject



220
221
222
223
224
225
# File 'lib/valkyrie/persistence/fedora/persister/model_converter.rb', line 220

def apply_first_and_last
  return if ordered_list.to_a.empty?
  graph << RDF::Statement.new(subject, predicate, node_id)
  graph << RDF::Statement.new(node_id, ::RDF::Vocab::IANA.first, ordered_list.head.next.rdf_subject)
  graph << RDF::Statement.new(node_id, ::RDF::Vocab::IANA.last, ordered_list.tail.prev.rdf_subject)
end

#graphObject



216
217
218
# File 'lib/valkyrie/persistence/fedora/persister/model_converter.rb', line 216

def graph
  @graph ||= ordered_list.to_graph
end

#initialize_listObject



235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/valkyrie/persistence/fedora/persister/model_converter.rb', line 235

def initialize_list
  Array(value.value).each_with_index do |val, index|
    property = NestedProperty.new(value: val, scope: value)
    obj = calling_mapper.for(property.property).result
    # Append value directly if possible.
    if obj.respond_to?(:value)
      ordered_list.insert_proxy_for_at(index, proxy_for_value(obj.value))
    # If value is a nested object, take its graph and append it.
    elsif obj.respond_to?(:graph)
      append_to_graph(obj: obj, index: index, property: property.property)
    end
    graph << ordered_list.to_graph
  end
end

#node_idObject



227
228
229
# File 'lib/valkyrie/persistence/fedora/persister/model_converter.rb', line 227

def node_id
  @node_id ||= ordered_list.send(:new_node_subject)
end

#ordered_listObject



285
286
287
# File 'lib/valkyrie/persistence/fedora/persister/model_converter.rb', line 285

def ordered_list
  @ordered_list ||= OrderedList.new(RDF::Graph.new, nil, nil, value.adapter)
end

#predicateObject



231
232
233
# File 'lib/valkyrie/persistence/fedora/persister/model_converter.rb', line 231

def predicate
  value.schema.predicate_for(resource: value.resource, property: value.key)
end

#proxy_for_value(value) ⇒ Object



250
251
252
253
254
255
256
# File 'lib/valkyrie/persistence/fedora/persister/model_converter.rb', line 250

def proxy_for_value(value)
  if value.is_a?(RDF::Literal) && value.datatype == PermissiveSchema.valkyrie_id
    ordered_list.adapter.id_to_uri(value)
  else
    value
  end
end

#resultObject



210
211
212
213
214
# File 'lib/valkyrie/persistence/fedora/persister/model_converter.rb', line 210

def result
  initialize_list
  apply_first_and_last
  GraphProperty.new(value.subject, value.key, graph, value.adapter, value.resource)
end