Module: ActiveFedora::Predicates

Defined in:
lib/active_fedora/predicates.rb

Class Method Summary collapse

Class Method Details

.default_predicate_namespaceObject


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

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
# File 'lib/active_fedora/predicates.rb', line 16

def self.find_graph_predicate(predicate)
  #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


89
90
91
92
93
94
95
96
# File 'lib/active_fedora/predicates.rb', line 89

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


73
74
75
# File 'lib/active_fedora/predicates.rb', line 73

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

.predicate_config=(value) ⇒ Object


66
67
68
69
70
71
# File 'lib/active_fedora/predicates.rb', line 66

def self.predicate_config= value
  unless value.nil? or (value.is_a?(Hash) and [:predicate_mapping,:default_namespace].all? { |key| value.has_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


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

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

.predicate_mappingsObject


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

def self.predicate_mappings
  predicate_config[:predicate_mapping]
end

.predicate_namespacesObject


77
78
79
# File 'lib/active_fedora/predicates.rb', line 77

def self.predicate_namespaces
  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"
                                            },
                                        })

108
109
110
111
112
113
114
115
116
117
# File 'lib/active_fedora/predicates.rb', line 108

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.kind_of? ::RDF::URI
    predicate.to_s.split('/', 4).last.gsub(/(\/|#)/, '_').underscore
  else
    raise "Unable to parse predicate: #{predicate}"
  end
end

.vocabularies(vocabs = {}) ⇒ Object


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

def self.vocabularies(vocabs = {})
  @vocabularies ||= vocabs
  predicate_mappings.keys.each do |ns| 
    @vocabularies[ns] = ::RDF::Vocabulary.new(ns) unless @vocabularies.has_key? ns
  end
  @vocabularies
end