This repository has been archived by the owner on Sep 12, 2018. It is now read-only.
forked from ruby-rdf/spira
/
instance_methods.rb
574 lines (528 loc) · 18.6 KB
/
instance_methods.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
require 'rdf/isomorphic'
require 'set'
module Spira
module Resource
##
# This module contains instance methods for Spira resources. See
# {Spira::Resource} for more information.
#
# @see Spira::Resource
# @see Spira::Resource::ClassMethods
# @see Spira::Resource::DSL
# @see Spira::Resource::Validations
module InstanceMethods
# Marker for whether or not a field has been set or not; distinguishes
# nil and unset.
# @private
NOT_SET = ::Object.new.freeze
##
# This instance's URI.
#
# @return [RDF::URI]
attr_reader :subject
##
# Initialize a new Spira::Resource instance of this resource class using
# a new blank node subject. Accepts a hash of arguments for initial
# attributes. To use a URI or existing blank node as a subject, use
# {Spira::Resource::ClassMethods#for} instead.
#
# @param [Hash{Symbol => Any}] opts Default attributes for this instance
# @yield [self] Executes a given block and calls `#save!`
# @yieldparam [self] self The newly created instance
# @see Spira::Resource::ClassMethods#for
# @see RDF::URI#as
# @see RDF::Node#as
def initialize(opts = {})
@subject = opts[:_subject] || RDF::Node.new
reload(opts)
if block_given?
yield(self)
save!
end
self
end
##
# Reload all attributes for this instance, overwriting or setting
# defaults with the given opts. This resource will block if the
# underlying repository blocks the next time it accesses attributes.
#
# @param [Hash{Symbol => Any}] opts
# @option opts [Symbol] :any A property name. Sets the given property to the given value.
def reload(opts = {})
@cache = opts[:_cache] || RDF::Util::Cache.new
@cache[subject] = self
@dirty = {}
@attributes = {}
@attributes[:current] = {}
@attributes[:copied] = {}
self.class.properties.each do |name, predicate|
case opts[name].nil?
when false
attribute_set(name, opts[name])
when true
@attributes[:copied][name] = NOT_SET
end
end
@attributes[:original] = promise { reload_attributes }
end
##
# Load this instance's attributes. Overwrite loaded values with attributes in the given options.
#
# @return [Hash{Symbol => Any}] attributes
# @private
def reload_attributes()
statements = self.class.repository_or_fail.query(:subject => @subject).to_a
attributes = {}
# Set attributes for each statement corresponding to a predicate
self.class.properties.each do |name, property|
if self.class.is_list?(name)
values = Set.new
collection = statements.select{|s| s.subject == @subject && s.predicate == property[:predicate]} unless statements.empty?
unless collection.nil?
collection.each do |statement|
values << self.class.build_value(statement,property[:type], @cache)
end
end
attributes[name] = values
else
statement = statements.select{|s| s.subject == @subject && s.predicate == property[:predicate]}.first unless statements.empty?
attributes[name] = self.class.build_value(statement, property[:type], @cache)
end
end
attributes
end
##
# Returns a hash of name => value for this instance's attributes
#
# @return [Hash{Symbol => Any}] attributes
def attributes
attributes = {}
self.class.properties.keys.each do |property|
attributes[property] = attribute_get(property)
end
attributes
end
##
# Remove the given attributes from the repository
#
# @param [Hash] attributes The hash of attributes to delete
# @param [Hash{Symbol => Any}] opts Options for deletion
# @option opts [true] :destroy_type Destroys the `RDF.type` statement associated with this class as well
# @private
def _destroy_attributes(attributes, opts = {})
repository = repository_for_attributes(attributes)
repository.insert([@subject, RDF.type, self.class.type]) if (self.class.type && opts[:destroy_type])
self.class.repository_or_fail.delete(*repository)
end
##
# Delete this instance from the repository.
#
# @param [Symbol] what
# @example Delete all fields defined in the model
# @object.destroy!
# @example Delete all instances of this object as the subject of a triple, including non-model data @object.destroy!
# @object.destroy!(:subject)
# @example Delete all instances of this object as the object of a triple
# @object.destroy!(:object)
# @example Delete all triples with this object as the subject or object
# @object.destroy!(:completely)
# @return [true, false] Whether or not the destroy was successful
def destroy!(what = nil)
before_destroy if self.respond_to?(:before_destroy, true)
result = case what
when nil
_destroy_attributes(attributes, :destroy_type => true) != nil
when :subject
self.class.repository_or_fail.delete([subject, nil, nil]) != nil
when :object
self.class.repository_or_fail.delete([nil, nil, subject]) != nil
when :completely
destroy!(:subject) && destroy!(:object)
end
after_destroy if self.respond_to?(:after_destroy, true) if result
result
end
##
# Save changes in this instance to the repository.
#
# @return [self] self
def save!
existed = (self.respond_to?(:before_create, true) || self.respond_to?(:after_create, true)) && !self.type.nil? && exists?
before_create if self.respond_to?(:before_create, true) && !self.type.nil? && !existed
before_save if self.respond_to?(:before_save, true)
# we use the non-raising validate and check it to make a slightly different error message. worth it?...
case validate
when true
_update!
when false
raise(ValidationError, "Could not save #{self.inspect} due to validation errors: " + errors.each.join(';'))
end
after_create if self.respond_to?(:after_create, true) && !self.type.nil? && !existed
after_save if self.respond_to?(:after_save, true)
self
end
##
# Update multiple attributes of this repository.
#
# @example Update multiple attributes
# person.update(:name => 'test', :age => 10)
# #=> person
# person.name
# #=> 'test'
# person.age
# #=> 10
# person.dirty?
# #=> true
# @param [Hash{Symbol => Any}] properties
# @return [self]
def update(properties)
properties.each do |property, value|
attribute_set(property, value)
end
after_update if self.respond_to?(:after_update, true)
self
end
##
# Equivalent to #update followed by #save!
#
# @example Update multiple attributes and save the changes
# person.update!(:name => 'test', :age => 10)
# #=> person
# person.name
# #=> 'test'
# person.age
# #=> 10
# person.dirty?
# #=> false
# @param [Hash{Symbol => Any}] properties
# @return [self]
def update!(properties)
update(properties)
save!
end
##
# Save changes to the repository
#
# @private
def _update!
repo = self.class.repository_or_fail
self.class.properties.each do |property, predicate|
value = attribute_get(property)
if dirty?(property)
repo.delete([subject, predicate[:predicate], nil])
if self.class.is_list?(property)
value.each do |val|
store_attribute(property, val, predicate[:predicate], repo)
end
else
store_attribute(property, value, predicate[:predicate], repo)
end
end
@attributes[:original][property] = value
@dirty[property] = nil
@attributes[:copied][property] = NOT_SET
end
repo.insert(RDF::Statement.new(@subject, RDF.type, type)) if type
end
##
# The `RDF.type` associated with this class.
#
# @return [nil,RDF::URI] The RDF type associated with this instance's class.
def type
self.class.type
end
##
# `type` is a special property which is associated with the class and not
# the instance. Always raises a TypeError to try and assign it.
#
# @raise [TypeError] always
def type=(type)
raise TypeError, "Cannot reassign RDF.type for #{self}; consider appending to a has_many :types"
end
##
# Returns the RDF representation of this resource.
#
# @return [RDF::Enumerable]
def to_rdf
self
end
##
# A developer-friendly view of this projection
#
# @private
def inspect
"<#{self.class}:#{self.object_id} @subject: #{@subject}>"
end
##
# Enumerate each RDF statement that makes up this projection. This makes
# each instance an `RDF::Enumerable`, with all of the nifty benefits
# thereof. See <http://rdf.rubyforge.org/RDF/Enumerable.html> for
# information on arguments.
#
# @see http://rdf.rubyforge.org/RDF/Enumerable.html
def each(*args, &block)
return enum_for(:each) unless block_given?
repository = repository_for_attributes(attributes)
repository.insert(RDF::Statement.new(@subject, RDF.type, type)) unless type.nil?
repository.each(*args, &block)
end
##
# The number of RDF::Statements this projection has.
#
# @see http://rdf.rubyforge.org/RDF/Enumerable.html#count
def count
each.size
end
##
# Sets the given attribute to the given value.
#
# @private
def attribute_set(name, value)
@dirty[name] = true
@attributes[:current][name] = value
end
##
# Returns true if the given attribute has been changed from the backing store
#
def dirty?(name = nil)
case name
when nil
self.class.properties.keys.any? { |key| dirty?(key) }
else
case
when @dirty[name] == true
true
else
case @attributes[:copied][name]
when NOT_SET
false
else
@attributes[:copied][name] != @attributes[:original][name]
end
end
end
end
##
# Get the current value for the given attribute
#
# @private
def attribute_get(name)
case @dirty[name]
when true
@attributes[:current][name]
else
case @attributes[:copied][name].equal?(NOT_SET)
when true
dup = if @attributes[:original][name].is_a?(Spira::Resource)
@attributes[:original][name]
else
begin
@attributes[:original][name].dup
rescue TypeError
@attributes[:original][name]
end
end
@attributes[:copied][name] = dup
when false
@attributes[:copied][name]
end
end
end
##
# Create an RDF::Repository for the given attributes hash. This could
# just as well be a class method but is only used here in #save! and
# #destroy!, so it is defined here for simplicity.
#
# @param [Hash] attributes The attributes to create a repository for
# @private
def repository_for_attributes(attributes)
RDF::Repository.new.tap do |repo|
attributes.each do | name, attribute |
predicate = self.class.properties[name][:predicate]
if self.class.is_list?(name)
attribute.each do |value|
store_attribute(name, value, predicate, repo)
end
else
store_attribute(name, attribute, predicate, repo)
end
end
end
end
##
# Compare this instance with another instance. The comparison is done on
# an RDF level, and will work across subclasses as long as the attributes
# are the same.
#
# @see http://rdf.rubyforge.org/isomorphic/
def ==(other)
case other
# TODO: define behavior for equality on subclasses.
# TODO: should we compare attributes here?
when self.class
@subject == other.uri
when RDF::Enumerable
self.isomorphic_with?(other)
else
false
end
end
##
# Returns true for :to_uri if this instance's subject is a URI, and false if it is not.
# Returns true for :to_node if this instance's subject is a Node, and false if it is not.
# Calls super otherwise.
#
# @private
def respond_to?(*args)
case args[0]
when :to_uri
@subject.respond_to?(:to_uri)
when :to_node
@subject.node?
else
super(*args)
end
end
##
# Returns the RDF::URI associated with this instance if this instance's
# subject is an RDF::URI, and nil otherwise.
#
# @return [RDF::URI,nil]
def uri
@subject.respond_to?(:to_uri) ? @subject : nil
end
##
# Returns the URI representation of this resource, if available. If this
# resource's subject is a BNode, raises a NoMethodError.
#
# @return [RDF::URI]
# @raise [NoMethodError]
def to_uri
uri || (raise NoMethodError, "No such method: :to_uri (this instance's subject is not a URI)")
end
##
# Returns true if the subject associated with this instance is a blank node.
#
# @return [true, false]
def node?
@subject.node?
end
##
# Returns the Node subject of this resource, if available. If this
# resource's subject is a URI, raises a NoMethodError.
#
# @return [RDF::Node]
# @raise [NoMethodError]
def to_node
@subject.node? ? @subject : (raise NoMethodError, "No such method: :to_uri (this instance's subject is not a URI)")
end
##
# The validation errors collection associated with this instance.
#
# @return [Spira::Errors]
# @see Spira::Errors
def errors
@errors ||= Spira::Errors.new
end
##
# Run any model validations and populate the errors object accordingly.
# Returns true if the model is valid, false otherwise
#
# @return [True, False]
def validate
unless self.class.validators.empty?
errors.clear
self.class.validators.each do | validator | self.send(validator) end
end
errors.empty?
end
##
# Run validations on this model and raise a Spira::ValidationError if the validations fail.
#
# @see #validate
# @return true
def validate!
validate || raise(ValidationError, "Failed to validate #{self.inspect}: " + errors.each.join(';'))
end
##
# Returns true if any data exists for this subject in the backing RDF store
#
# @return [Boolean]
def exists?
!data.empty?
end
alias_method :exist?, :exists?
##
# Returns an Enumerator of all RDF data for this subject, not just model data.
#
# @see #each
# @see http://rdf.rubyforge.org/RDF/Enumerable.html
# @return [Enumerator]
def data
self.class.repository.query(:subject => subject)
end
##
# Returns a new instance of this class with the new subject instead of self.subject
#
# @param [RDF::Resource] new_subject
# @return [Spira::Resource] copy
def copy(new_subject)
copy = self.class.for(new_subject)
self.class.properties.each_key { |property| copy.attribute_set(property, self.attribute_get(property)) }
copy
end
##
# Returns a new instance of this class with the new subject instead of
# self.subject after saving the new copy to the repository.
#
# @param [RDF::Resource] new_subject
# @return [Spira::Resource, String] copy
def copy!(new_subject)
copy(new_subject).save!
end
##
# Copies all data, including non-model data, about this resource to
# another URI. The copy is immediately saved to the repository.
#
# @param [RDF::Resource] new_subject
# @return [Spira::Resource, String] copy
def copy_resource!(new_subject)
new_subject = self.class.id_for(new_subject)
update_repository = RDF::Repository.new
data.each do |statement|
update_repository << RDF::Statement.new(new_subject, statement.predicate, statement.object)
end
self.class.repository.insert(update_repository)
new_subject.as(self.class)
end
##
# Rename this resource in the repository to the new given subject.
# Changes are immediately saved to the repository.
#
# @param [RDF::Resource] new_subject
# @return [Spira::Resource, String] new_resource
def rename!(new_subject)
new = copy_resource!(new_subject)
object_statements = self.class.repository.query(:object => subject)
update_repository = RDF::Repository.new
object_statements.each do |statement|
update_repository << RDF::Statement.new(statement.subject, statement.predicate, new.subject)
end
self.class.repository.insert(update_repository)
destroy!(:completely)
new
end
## We have defined #each and can do this fun RDF stuff by default
include ::RDF::Enumerable, ::RDF::Queryable
## Include the base validation functions
include Spira::Resource::Validations
private
def store_attribute(property, value, predicate, repository)
if value
val = self.class.build_rdf_value(value, self.class.properties[property][:type])
repository.insert(RDF::Statement.new(subject, predicate, val))
end
end
end
end
end