Module: ActiveFedora::RdfNode
Defined Under Namespace
Modules: ClassMethods
Classes: Builder, TermProxy
Class Method Summary
collapse
Instance Method Summary
collapse
-
#==(other_object) ⇒ Object
Comparison Operator Checks that * RDF subject id (URI) is same * Class is the same * Both objects reference the same RDF graph in memory.
-
#append(subject, predicate, args) ⇒ Object
-
#attributes=(values) ⇒ Object
-
#config_for_term_or_uri(term) ⇒ Object
-
#delete_predicate(subject, predicate, values = nil) ⇒ Object
-
#destroy ⇒ Object
-
#find_predicate(term) ⇒ Object
-
#get_values(subject, term) ⇒ Object
-
#insert_child(predicate, node) ⇒ Object
-
#mark_for_destruction ⇒ Object
-
#marked_for_destruction? ⇒ Boolean
-
#method_missing(name, *args) ⇒ Object
-
#new_record=(val) ⇒ Object
-
#new_record? ⇒ Boolean
-
#query(subject, predicate, &block) ⇒ Object
-
#rdf_subject ⇒ Object
Get the subject for this rdf object.
-
#reset_child_cache! ⇒ Object
-
#reset_rdf_subject! ⇒ Object
-
#set_value(subject, term, values) ⇒ Object
if there are any existing statements with this predicate, replace them.
-
#target_class(predicate) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/active_fedora/rdf_node.rb', line 193
def method_missing(name, *args)
if md = /^([^=]+)=$/.match(name.to_s)
set_value(rdf_subject, md[1], *args)
elsif find_predicate(name)
get_values(rdf_subject, name)
else
super
end
rescue ActiveFedora::UnregisteredPredicateError
super
end
|
Class Method Details
.rdf_registry ⇒ Object
Mapping from URI to ruby class
10
11
12
|
# File 'lib/active_fedora/rdf_node.rb', line 10
def self.rdf_registry
@@rdf_registry ||= {}
end
|
Instance Method Details
#==(other_object) ⇒ Object
Comparison Operator Checks that
* RDF subject id (URI) is same
* Class is the same
* Both objects reference the same RDF graph in memory
19
20
21
22
23
|
# File 'lib/active_fedora/rdf_node.rb', line 19
def ==(other_object)
self.class == other_object.class &&
self.rdf_subject.id == other_object.rdf_subject.id &&
self.graph.object_id == other_object.graph.object_id
end
|
#append(subject, predicate, args) ⇒ Object
158
159
160
161
|
# File 'lib/active_fedora/rdf_node.rb', line 158
def append(subject, predicate, args)
options = config_for_term_or_uri(predicate)
graph.insert([subject, predicate, args])
end
|
#attributes=(values) ⇒ Object
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/active_fedora/rdf_node.rb', line 126
def attributes=(values)
raise ArgumentError, "values must be a Hash, you provided #{values.class}" unless values.kind_of? Hash
values.with_indifferent_access.each do |key, value|
if self.class.config.keys.include?(key)
set_value(rdf_subject, key, value)
elsif nested_attributes_options.keys.map{ |k| "#{k}_attributes"}.include?(key)
send("#{key}=".to_sym, value)
end
end
end
|
#config_for_term_or_uri(term) ⇒ Object
168
169
170
171
172
173
174
175
|
# File 'lib/active_fedora/rdf_node.rb', line 168
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/active_fedora/rdf_node.rb', line 137
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
reset_child_cache!
end
|
#destroy ⇒ Object
Be careful with destroy. It will still be in the cache untill you call reset()
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/active_fedora/rdf_node.rb', line 104
def destroy
subject = rdf_subject
query = RDF::Query.new do
pattern [subject, :predicate, :value]
end
query.execute(graph).each do |solution|
graph.delete [subject, solution.predicate, solution.value]
end
query = RDF::Query.new do
pattern [:subject, :predicate, subject]
end
query.execute(graph).each do |solution|
graph.delete [solution.subject, solution.predicate, subject]
end
end
|
#find_predicate(term) ⇒ Object
178
179
180
181
|
# File 'lib/active_fedora/rdf_node.rb', line 178
def find_predicate(term)
conf = config_for_term_or_uri(term)
conf ? conf.predicate : nil
end
|
#get_values(subject, term) ⇒ Object
45
46
47
48
49
50
|
# File 'lib/active_fedora/rdf_node.rb', line 45
def get_values(subject, term)
options = config_for_term_or_uri(term)
predicate = options.predicate
@target ||= {}
@target[term.to_s] ||= TermProxy.new(self, subject, predicate, options)
end
|
#insert_child(predicate, node) ⇒ Object
163
164
165
|
# File 'lib/active_fedora/rdf_node.rb', line 163
def insert_child(predicate, node)
graph.insert([rdf_subject, predicate, node.rdf_subject])
end
|
#mark_for_destruction ⇒ Object
59
60
61
|
# File 'lib/active_fedora/rdf_node.rb', line 59
def mark_for_destruction
@marked_for_destruction = true
end
|
#marked_for_destruction? ⇒ Boolean
63
64
65
|
# File 'lib/active_fedora/rdf_node.rb', line 63
def marked_for_destruction?
@marked_for_destruction
end
|
#new_record=(val) ⇒ Object
67
68
69
|
# File 'lib/active_fedora/rdf_node.rb', line 67
def new_record= val
@new_record = val
end
|
#new_record? ⇒ Boolean
71
72
73
|
# File 'lib/active_fedora/rdf_node.rb', line 71
def new_record?
@new_record
end
|
#query(subject, predicate, &block) ⇒ Object
183
184
185
186
187
188
189
190
191
|
# File 'lib/active_fedora/rdf_node.rb', line 183
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_subject ⇒ Object
Get the subject for this rdf object
27
28
29
30
31
32
33
|
# File 'lib/active_fedora/rdf_node.rb', line 27
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_child_cache! ⇒ Object
39
40
41
|
# File 'lib/active_fedora/rdf_node.rb', line 39
def reset_child_cache!
@target = {}
end
|
#reset_rdf_subject! ⇒ Object
35
36
37
|
# File 'lib/active_fedora/rdf_node.rb', line 35
def reset_rdf_subject!
@subject = nil
end
|
#set_value(subject, term, values) ⇒ Object
if there are any existing statements with this predicate, replace them
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/active_fedora/rdf_node.rb', line 79
def set_value(subject, term, values)
options = config_for_term_or_uri(term)
predicate = options.predicate
values = Array(values)
remove_existing_values(subject, predicate, values)
values.each do |arg|
if arg.respond_to?(:rdf_subject) 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
@target ||= {}
proxy = @target[term.to_s]
proxy ||= TermProxy.new(self, subject, predicate, options)
proxy.reset!
proxy
end
|
#target_class(predicate) ⇒ Object
52
53
54
55
56
57
|
# File 'lib/active_fedora/rdf_node.rb', line 52
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
|