Class: Valkyrie::Persistence::Fedora::PermissiveSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/persistence/fedora/permissive_schema.rb

Overview

Default schema for Fedora MetadataAdapter. It’s used to generate a mapping of Resource attributes to predicates. This implementation will make up a URI if one doesn’t exist in a passed in schema.

Examples:

Passing in a mapping

schema = Valkyrie::Persistence::Fedora::PermissiveSchema.new(member_ids:
  RDF::URI("http://example.com/member_ids"))
schema.predicate_for(resource: Resource.new, property: :member_ids) # => RDF::URI<"http://example.com/member_ids">
schema.predicate_for(resource: Resource.new, property: :unknown) # => RDF::URI<"http://example.com/predicate/unknown">

Constant Summary collapse

URI_PREFIX =
'http://example.com/predicate/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema = {}) ⇒ PermissiveSchema

Returns a new instance of PermissiveSchema.

Parameters:

  • schema (Hash) (defaults to: {})

    the structure used to store the mapping between property names and predicates



70
71
72
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 70

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

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



67
68
69
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 67

def schema
  @schema
end

Class Method Details

.idRDF::URI

Returns:



21
22
23
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 21

def self.id
  uri_for(:id)
end

.member_idsRDF::URI

Returns:



26
27
28
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 26

def self.member_ids
  uri_for(:member_ids)
end

.optimistic_lock_tokenRDF::URI

Returns:



56
57
58
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 56

def self.optimistic_lock_token
  uri_for(:optimistic_lock_token)
end

.uri_for(property) ⇒ RDF::URI

Cast the property to a URI in the namespace

Parameters:

  • property (Symbol)

Returns:



63
64
65
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 63

def self.uri_for(property)
  RDF::URI("#{URI_PREFIX}#{property}")
end

.valkyrie_boolRDF::URI

Returns:



31
32
33
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 31

def self.valkyrie_bool
  uri_for(:valkyrie_bool)
end

.valkyrie_datetimeRDF::URI

Returns:



36
37
38
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 36

def self.valkyrie_datetime
  uri_for(:valkyrie_datetime)
end

.valkyrie_floatRDF::URI

Returns:



41
42
43
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 41

def self.valkyrie_float
  uri_for(:valkyrie_float)
end

.valkyrie_idRDF::URI

Returns:



16
17
18
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 16

def self.valkyrie_id
  uri_for('valkyrie_id')
end

.valkyrie_intRDF::URI

Returns:



46
47
48
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 46

def self.valkyrie_int
  uri_for(:valkyrie_int)
end

.valkyrie_timeRDF::URI

Returns:



51
52
53
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 51

def self.valkyrie_time
  uri_for(:valkyrie_time)
end

Instance Method Details

#predicate_for(resource:, property:) ⇒ RDF::URI

Find the predicate in the schema for the Valkyrie property If this does not exist, a URI using the property name prefixed by URI_PREFIX generates it

Parameters:

Returns:



79
80
81
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 79

def predicate_for(resource:, property:)
  schema.fetch(property) { self.class.uri_for(property) }
end

#property_for(resource:, predicate:) ⇒ String

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'

Parameters:

Returns:

  • (String)


91
92
93
94
95
96
97
# File 'lib/valkyrie/persistence/fedora/permissive_schema.rb', line 91

def property_for(resource:, predicate:)
  existing_predicates = schema.find { |_k, v| v == RDF::URI(predicate.to_s) }
  predicate_name = predicate.to_s.gsub(URI_PREFIX, '')

  return predicate_name if existing_predicates.blank?
  existing_predicates.first
end