Skip to content

Commit

Permalink
Merge pull request #345 from samvera-labs/uris
Browse files Browse the repository at this point in the history
Compare properties rather than predicates when doing casting.
  • Loading branch information
Trey Pendragon committed Jan 12, 2018
2 parents 1258d55 + e413ef6 commit 0755256
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
33 changes: 31 additions & 2 deletions lib/valkyrie/persistence/fedora/permissive_schema.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
# frozen_string_literal: true
module Valkyrie::Persistence::Fedora
class PermissiveSchema
URI_PREFIX = 'http://example.com/predicate/'

# @return [RDF::URI]
def self.valkyrie_id
uri_for('valkyrie_id')
end

# @return [RDF::URI]
def self.id
uri_for(:id)
end

# @return [RDF::URI]
def self.member_ids
uri_for(:member_ids)
end

# Cast the property to a URI in the namespace
# @param property [Symbol]
# @return [RDF::URI]
def self.uri_for(property)
RDF::URI("#{URI_PREFIX}#{property}")
end

attr_reader :schema
def initialize(schema = {})
@schema = schema
end

def predicate_for(resource:, property:)
schema.fetch(property, ::RDF::URI("http://example.com/predicate/#{property}"))
schema.fetch(property) { self.class.uri_for(property) }
end

# Find the property in the schema. If it's not there check to see
# if this prediate is in the URI_PREFIX namespace, return the suffix as the property
# @example:
# property_for(resource: nil, predicate: "http://example.com/predicate/internal_resource")
# #=> 'internal_resource'
def property_for(resource:, predicate:)
(schema.find { |_k, v| v == RDF::URI(predicate.to_s) } || []).first || predicate.to_s.gsub("http://example.com/predicate/", "")
(schema.find { |_k, v| v == RDF::URI(predicate.to_s) } || []).first || predicate.to_s.gsub(URI_PREFIX, '')
end
end
end
4 changes: 2 additions & 2 deletions lib/valkyrie/persistence/fedora/persister/model_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def result
value.key,
RDF::Literal.new(
value.value,
datatype: RDF::URI("http://example.com/predicate/valkyrie_id")
datatype: PermissiveSchema.valkyrie_id
),
value.adapter,
value.resource
Expand Down Expand Up @@ -205,7 +205,7 @@ def result
value.key,
RDF::Literal.new(
value.value,
datatype: RDF::URI("http://example.com/predicate/valkyrie_id")
datatype: PermissiveSchema.valkyrie_id
),
value.adapter,
value.resource
Expand Down
17 changes: 9 additions & 8 deletions lib/valkyrie/persistence/fedora/persister/orm_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def id

def id_property
return unless object.subject_uri.to_s.include?("#")
object.graph.query([RDF::URI(""), RDF::URI("http://example.com/predicate/id"), nil]).to_a.first.try(:object).to_s
object.graph.query([RDF::URI(""), PermissiveSchema.id, nil]).to_a.first.try(:object).to_s
end

class GraphToAttributes
Expand Down Expand Up @@ -96,7 +96,7 @@ def self.handles?(value)
end

def result
value.statement.predicate = ::RDF::URI("http://example.com/predicate/member_ids")
value.statement.predicate = PermissiveSchema.member_ids
values = OrderedList.new(scope, head, tail, adapter).to_a.map(&:proxy_for)
values = values.map do |val|
calling_mapper.for(Property.new(statement: RDF::Statement.new(value.statement.subject, value.statement.predicate, val), scope: value.scope, adapter: value.adapter)).result
Expand Down Expand Up @@ -199,7 +199,7 @@ def result
class ValkyrieIDValue < ::Valkyrie::ValueMapper
FedoraValue.register(self)
def self.handles?(value)
value.statement.object.is_a?(RDF::Literal) && value.statement.object.datatype == RDF::URI("http://example.com/predicate/valkyrie_id")
value.statement.object.is_a?(RDF::Literal) && value.statement.object.datatype == PermissiveSchema.valkyrie_id
end

def result
Expand All @@ -222,8 +222,9 @@ def result

class InternalModelValue < ::Valkyrie::ValueMapper
FedoraValue.register(self)

def self.handles?(value)
value.statement.predicate.to_s == "http://example.com/predicate/internal_resource"
value.statement.predicate == value.adapter.schema.predicate_for(property: :internal_resource, resource: nil)
end

def result
Expand All @@ -234,7 +235,7 @@ def result
class CreatedAtValue < ::Valkyrie::ValueMapper
FedoraValue.register(self)
def self.handles?(value)
value.statement.predicate.to_s == "http://example.com/predicate/created_at"
value.statement.predicate == value.adapter.schema.predicate_for(property: :created_at, resource: nil)
end

def result
Expand All @@ -245,7 +246,7 @@ def result
class UpdatedAtValue < ::Valkyrie::ValueMapper
FedoraValue.register(self)
def self.handles?(value)
value.statement.predicate.to_s == "http://example.com/predicate/updated_at"
value.statement.predicate == value.adapter.schema.predicate_for(property: :updated_at, resource: nil)
end

def result
Expand All @@ -272,8 +273,8 @@ def apply_to(hsh)
end

def key
key = statement.predicate.to_s
key = schema.property_for(resource: nil, predicate: key)
predicate = statement.predicate.to_s
key = schema.property_for(resource: nil, predicate: predicate)
namespaces.each do |namespace|
key = key.to_s.gsub(/^#{namespace}/, '')
end
Expand Down
2 changes: 1 addition & 1 deletion lib/valkyrie/persistence/fedora/query_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def content_with_inbound(id:)

def find_inverse_references_by(resource:, property:)
content = content_with_inbound(id: resource.id)
property_uri = RDF::URI("http://example.com/predicate/#{property}")
property_uri = adapter.schema.predicate_for(property: property, resource: nil)
ids = content.graph.query([nil, property_uri, nil]).map(&:subject).map { |x| x.to_s.gsub(/#.*/, '') }.map { |x| adapter.uri_to_id(x) }
ids.lazy.map do |id|
find_by(id: id)
Expand Down

0 comments on commit 0755256

Please sign in to comment.