Module: ActiveFedora::RdfNode

Extended by:
ActiveSupport::Autoload, ActiveSupport::Concern
Included in:
RDFDatastream
Defined in:
lib/active_fedora/rdf_node.rb,
lib/active_fedora/rdf_node/term_proxy.rb

Defined Under Namespace

Modules: ClassMethods Classes: Builder, TermProxy

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/active_fedora/rdf_node.rb', line 117

def method_missing(name, *args)
  if (md = /^([^=]+)=$/.match(name.to_s)) && pred = find_predicate(md[1])
      set_value(rdf_subject, pred, *args)  
  elsif find_predicate(name)
      get_values(rdf_subject, name)
  else 
    super
  end
rescue ActiveFedora::UnregisteredPredicateError
  super
end

Class Method Details

.rdf_registryObject

Mapping from URI to ruby class



9
10
11
# File 'lib/active_fedora/rdf_node.rb', line 9

def self.rdf_registry
  @@rdf_registry ||= {}
end

Instance Method Details

#append(subject, predicate, args) ⇒ Object

append a value

Parameters:

  • predicate (Symbol, RDF::URI)

    the predicate to insert into the graph



86
87
88
89
90
# File 'lib/active_fedora/rdf_node.rb', line 86

def append(subject, predicate, args)
  options = config_for_term_or_uri(predicate)
  graph.insert([subject, predicate, args])
  TermProxy.new(self, subject, options[:predicate], options)
end

#config_for_term_or_uri(term) ⇒ Object



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

def config_for_term_or_uri(term)
  case term
  when RDF::URI
    self.class.config.each { |k, v| return v if v[:predicate] == term}
  else
    self.class.config[term.to_sym]
  end
end

#delete_predicate(subject, predicate, values = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/active_fedora/rdf_node.rb', line 66

def delete_predicate(subject, predicate, values = nil)
  predicate = find_predicate(predicate) unless predicate.kind_of? RDF::URI

  if values.nil?
    query = RDF::Query.new do
      pattern [subject, predicate, :value]
    end

    query.execute(graph).each do |solution|
      graph.delete [subject, predicate, solution.value]
    end
  else
    Array(values).each do |v|
      graph.delete [subject, predicate, v]
    end
  end
end

#find_predicate(term) ⇒ Object

Parameters:

  • term (Symbol, RDF::URI)

    predicate the predicate to insert into the graph



102
103
104
105
# File 'lib/active_fedora/rdf_node.rb', line 102

def find_predicate(term)
  conf = config_for_term_or_uri(term)
  conf ? conf[:predicate] : nil
end

#get_values(subject, term) ⇒ Object

Parameters:

  • subject (RDF::URI)

    the base node to start the search from

  • term (Symbol)

    the term to get the values for



30
31
32
33
34
# File 'lib/active_fedora/rdf_node.rb', line 30

def get_values(subject, term)
  options = config_for_term_or_uri(term)
  predicate = options[:predicate]
  TermProxy.new(self, subject, predicate, options)
end

#query(subject, predicate, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/active_fedora/rdf_node.rb', line 107

def query subject, predicate, &block
  predicate = find_predicate(predicate) unless predicate.kind_of? RDF::URI
  
  q = RDF::Query.new do
    pattern [subject, predicate, :value]
  end

  q.execute(graph, &block)
end

#rdf_subjectObject

Get the subject for this rdf object



16
17
18
19
20
21
22
# File 'lib/active_fedora/rdf_node.rb', line 16

def rdf_subject
  @subject ||= begin
    s = self.class.rdf_subject.call(self)
    s &&= RDF::URI.new(s) if s.is_a? String
    s
  end
end

#reset_rdf_subject!Object



24
25
26
# File 'lib/active_fedora/rdf_node.rb', line 24

def reset_rdf_subject!
  @subject = nil
end

#set_value(subject, predicate, values) ⇒ Object

if there are any existing statements with this predicate, replace them

Parameters:

  • subject (RDF::URI)

    the subject to insert into the graph

  • predicate (Symbol, RDF::URI)

    the predicate to insert into the graph

  • values (Array, #to_s)

    the value/values to insert into the graph



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_fedora/rdf_node.rb', line 47

def set_value(subject, predicate, values)
  options = config_for_term_or_uri(predicate)
  predicate = options[:predicate]
  values = Array(values)

  remove_existing_values(subject, predicate, values)

  values.each do |arg|
    if arg.respond_to?(:rdf_subject) # an RdfObject
      graph.insert([subject, predicate, arg.rdf_subject ])
    else
      arg = arg.to_s if arg.kind_of? RDF::Literal
      graph.insert([subject, predicate, arg])
    end
  end

  TermProxy.new(self, subject, predicate, options)
end

#target_class(predicate) ⇒ Object



36
37
38
39
40
41
# File 'lib/active_fedora/rdf_node.rb', line 36

def target_class(predicate)
  _, conf = self.class.config_for_predicate(predicate)
  class_name = conf[:class_name]
  return nil unless class_name
  ActiveFedora.class_from_string(class_name, self.class)
end