Module: ActiveFedora::Predicates

Defined in:
lib/active_fedora/predicates.rb

Class Method Summary collapse

Class Method Details

.default_predicate_namespaceObject



88
89
90
# File 'lib/active_fedora/predicates.rb', line 88

def self.default_predicate_namespace
  predicate_config[:default_namespace]
end

.find_graph_predicate(predicate) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_fedora/predicates.rb', line 16

def self.find_graph_predicate(predicate)
  Deprecation.warn("find_graph_predicate has been deprecated and will be removed in ActiveFedora 10.0")
  # TODO, these could be cached
  case predicate
  when :has_model, "hasModel", :hasModel
    xmlns = "http://fedora.info/definitions/v4/model#"
    begin
      rel_predicate = predicate_lookup(predicate, xmlns)
    rescue UnregisteredPredicateError
      xmlns = nil
      rel_predicate = nil
    end
  else
    xmlns = "info:fedora/fedora-system:def/relations-external#"
    begin
      rel_predicate = predicate_lookup(predicate, xmlns)
    rescue UnregisteredPredicateError
      xmlns = nil
      rel_predicate = nil
    end
  end

  unless xmlns && rel_predicate
    rel_predicate, xmlns = find_predicate(predicate)
  end

  vocabularies[xmlns][rel_predicate]
end

.find_predicate(predicate) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/active_fedora/predicates.rb', line 92

def self.find_predicate(predicate)
  predicate_mappings.each do |namespace, predicates|
    if predicates.fetch(predicate, nil)
      return predicates[predicate], namespace
    end
  end
  raise ActiveFedora::UnregisteredPredicateError, "Unregistered predicate: #{predicate.inspect}"
end

.predicate_configObject



75
76
77
# File 'lib/active_fedora/predicates.rb', line 75

def self.predicate_config
  @@predicate_config ||= ActiveFedora.predicate_config
end

.predicate_config=(value) ⇒ Object



68
69
70
71
72
73
# File 'lib/active_fedora/predicates.rb', line 68

def self.predicate_config=(value)
  unless value.nil? || (value.is_a?(Hash) && [:predicate_mapping, :default_namespace].all? { |key| value.key? key })
    raise TypeError, "predicate_config must specify :predicate_mapping and :default_namespace"
  end
  @@predicate_config = value
end

.predicate_lookup(predicate, namespace = "info:fedora/fedora-system:def/relations-external#") ⇒ Object

If predicate is a symbol, looks up the predicate in the predicate_mappings If predicate is not a Symbol, returns the predicate untouched

Raises:

  • UnregisteredPredicateError if the predicate is a symbol but is not found in the predicate_mappings



57
58
59
60
61
62
63
64
65
66
# File 'lib/active_fedora/predicates.rb', line 57

def self.predicate_lookup(predicate, namespace = "info:fedora/fedora-system:def/relations-external#")
  if predicate.class == Symbol
    if predicate_mappings[namespace].key?(predicate)
      return predicate_mappings[namespace][predicate]
    else
      raise ActiveFedora::UnregisteredPredicateError
    end
  end
  predicate
end

.predicate_mappingsObject



84
85
86
# File 'lib/active_fedora/predicates.rb', line 84

def self.predicate_mappings
  predicate_config[:predicate_mapping]
end

.predicate_namespacesObject



79
80
81
82
# File 'lib/active_fedora/predicates.rb', line 79

def self.predicate_namespaces
  Deprecation.warn("predicate_namespaces has been deprecated and will be removed in ActiveFedora 10.0")
  predicate_config[:predicate_namespaces] ||= {}
end

.set_predicates(new_predicates) ⇒ Object

Add/Modify predicates without destroying the other predicate configs

Examples:

ActiveFedora::Predicates.set_predicates({
                                            "http://projecthydra.org/ns/relations#"=>{has_profile:"hasProfile"},
                                            "info:fedora/fedora-system:def/relations-external#"=>{
                                                references:"references",
                                                has_derivation: "cameFrom"
                                            },
                                        })


111
112
113
114
115
116
117
118
119
120
# File 'lib/active_fedora/predicates.rb', line 111

def self.set_predicates(new_predicates)
  predicate_config = ActiveFedora::Predicates.predicate_config
  new_predicates.each_pair do |ns, predicate_confs|
    predicate_config[:predicate_mapping][ns] ||= {}
    predicate_confs.each_pair do |property, value|
      predicate_config[:predicate_mapping][ns][property] = value
    end
  end
  predicate_config
end

.short_predicate(predicate) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/active_fedora/predicates.rb', line 3

def self.short_predicate(predicate)
  # for this regex to short-circuit correctly, namespaces must be sorted into descending order by length
  if match = /^(#{Predicates.predicate_mappings.keys.sort.reverse.join('|')})(.+)$/.match(predicate.to_str)
    namespace = match[1]
    predicate = match[2]
    Predicates.predicate_mappings[namespace].invert[predicate]
  elsif predicate.is_a? ::RDF::URI
    predicate.to_s.split('/', 4).last.gsub(/(\/|#)/, '_').underscore
  else
    raise "Unable to parse predicate: #{predicate}"
  end
end

.vocabularies(vocabs = {}) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/active_fedora/predicates.rb', line 45

def self.vocabularies(vocabs = {})
  Deprecation.warn("vocabularies has been deprecated and will be removed in ActiveFedora 10.0")
  @vocabularies ||= vocabs
  predicate_mappings.keys.each do |ns|
    @vocabularies[ns] = ::RDF::Vocabulary.new(ns) unless @vocabularies.key? ns
  end
  @vocabularies
end