-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #345 from samvera-labs/uris
Compare properties rather than predicates when doing casting.
- Loading branch information
Showing
4 changed files
with
43 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters