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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/active_fedora/rdf_node.rb', line 113

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

Instance Method Details

#append(subject, predicate, args) ⇒ Object

append a value

Parameters:

  • predicate (Symbol, RDF::URI)

    the predicate to insert into the graph



82
83
84
85
86
# File 'lib/active_fedora/rdf_node.rb', line 82

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



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

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/active_fedora/rdf_node.rb', line 61

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:

  • predicate (Symbol, RDF::URI)

    the predicate to insert into the graph



98
99
100
101
# File 'lib/active_fedora/rdf_node.rb', line 98

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

#get_values(subject, predicate) ⇒ Object

Parameters:

  • predicate (Symbol, RDF::URI)

    the predicate to insert into the graph



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

def get_values(subject, predicate)
  options = config_for_term_or_uri(predicate)
  TermProxy.new(self, subject, predicate, options)
end

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



103
104
105
106
107
108
109
110
111
# File 'lib/active_fedora/rdf_node.rb', line 103

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



10
11
12
13
14
15
16
# File 'lib/active_fedora/rdf_node.rb', line 10

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



18
19
20
# File 'lib/active_fedora/rdf_node.rb', line 18

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_fedora/rdf_node.rb', line 40

def set_value(subject, predicate, values)

  options = config_for_term_or_uri(predicate)
  predicate = options[:predicate]

  delete_predicate(subject, predicate)
  Array(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
      next if arg.kind_of?(String) && arg.empty?

      graph.insert([subject, predicate, arg])
    end
  end

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

#target_class(predicate) ⇒ Object



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

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