spira 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES.md +5 -0
- data/lib/spira/resource/dsl.rb +47 -27
- data/lib/spira/resource/instance_methods.rb +29 -11
- data/lib/spira/version.rb +1 -1
- metadata +12 -5
- data/VERSION +0 -1
data/CHANGES.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# Changelog for Spira <http://github.com/datagraph/spira>
|
2
2
|
|
3
|
+
## 0.0.12
|
4
|
+
* Implemented #validate, #validate! (refactored from #save!)
|
5
|
+
* Force to_a on query results when constructing to force the promise-like
|
6
|
+
semantics of SPARQL::Client
|
7
|
+
|
3
8
|
## 0.0.11
|
4
9
|
* Bumped the version dependency on rdf-isomorphic to 0.3.0
|
5
10
|
* Added support for before_create, after_create, before_save, after_save,
|
data/lib/spira/resource/dsl.rb
CHANGED
@@ -70,19 +70,25 @@ module Spira
|
|
70
70
|
# @see Spira::Type
|
71
71
|
# @return [Void]
|
72
72
|
def property(name, opts = {} )
|
73
|
-
|
73
|
+
predicate = predicate_for(opts[:predicate], name)
|
74
|
+
type = type_for(opts[:type])
|
75
|
+
@properties[name] = { :predicate => predicate, :type => type }
|
76
|
+
add_accessors(name,opts)
|
74
77
|
end
|
75
78
|
|
76
79
|
##
|
77
80
|
# The plural form of `property`. `Has_many` has the same options as
|
78
81
|
# `property`, but instead of a single value, a Ruby Array of objects will
|
79
|
-
# be created instead.
|
80
|
-
#
|
81
|
-
# an
|
82
|
+
# be created instead.
|
83
|
+
#
|
84
|
+
# has_many corresponds to an RDF subject with several triples of the same
|
85
|
+
# predicate. This corresponds to a Ruby Set, which will be returned when
|
86
|
+
# the property is accessed. Arrays will be accepted for new values, but
|
87
|
+
# ordering and duplicate values will be lost on save.
|
82
88
|
#
|
83
89
|
# @see Spira::Resource::DSL#property
|
84
90
|
def has_many(name, opts = {})
|
85
|
-
|
91
|
+
property(name, opts)
|
86
92
|
@lists[name] = true
|
87
93
|
end
|
88
94
|
|
@@ -215,34 +221,48 @@ module Spira
|
|
215
221
|
end
|
216
222
|
|
217
223
|
##
|
218
|
-
#
|
224
|
+
# Determine the type for a property based on the given type option
|
225
|
+
#
|
226
|
+
# @param [nil, Spira::Type, Constant] type
|
227
|
+
# @return Spira::Type
|
228
|
+
# @private
|
229
|
+
def type_for(type)
|
230
|
+
case
|
231
|
+
when type.nil?
|
232
|
+
Spira::Types::Any
|
233
|
+
when type.is_a?(Symbol) || type.is_a?(String)
|
234
|
+
type
|
235
|
+
when !(Spira.types[type].nil?)
|
236
|
+
Spira.types[type]
|
237
|
+
else
|
238
|
+
raise TypeError, "Unrecognized type: #{type}"
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
##
|
243
|
+
# Determine the predicate for a property based on the given predicate, name, and default vocabulary
|
244
|
+
#
|
245
|
+
# @param [#to_s, #to_uri] predicate
|
246
|
+
# @param [Symbol] name
|
247
|
+
# @return [RDF::URI]
|
219
248
|
# @private
|
220
|
-
def
|
221
|
-
|
222
|
-
when
|
223
|
-
|
249
|
+
def predicate_for(predicate, name)
|
250
|
+
case
|
251
|
+
when predicate.respond_to?(:to_uri) && predicate.to_uri.absolute?
|
252
|
+
predicate
|
224
253
|
when @default_vocabulary.nil?
|
225
254
|
raise ResourceDeclarationError, "A :predicate option is required for types without a default vocabulary"
|
226
|
-
else
|
255
|
+
else
|
256
|
+
# FIXME: use rdf.rb smart separator after 0.3.0 release
|
227
257
|
separator = @default_vocabulary.to_s[-1,1] =~ /(\/|#)/ ? '' : '/'
|
228
258
|
RDF::URI.intern(@default_vocabulary.to_s + separator + name.to_s)
|
229
259
|
end
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
when opts[:type].is_a?(Symbol) || opts[:type].is_a?(String)
|
237
|
-
opts[:type]
|
238
|
-
when !(Spira.types[opts[:type]].nil?)
|
239
|
-
Spira.types[opts[:type]]
|
240
|
-
else
|
241
|
-
raise TypeError, "Unrecognized type: #{opts[:type]}"
|
242
|
-
end
|
243
|
-
@properties[name] = {}
|
244
|
-
@properties[name][:predicate] = predicate
|
245
|
-
@properties[name][:type] = type
|
260
|
+
end
|
261
|
+
|
262
|
+
##
|
263
|
+
# Add getters and setters for a property or list.
|
264
|
+
# @private
|
265
|
+
def add_accessors(name, opts)
|
246
266
|
name_equals = (name.to_s + "=").to_sym
|
247
267
|
|
248
268
|
self.send(:define_method,name_equals) do |arg|
|
@@ -78,14 +78,14 @@ module Spira
|
|
78
78
|
# @return [Hash{Symbol => Any}] attributes
|
79
79
|
# @private
|
80
80
|
def reload_attributes()
|
81
|
-
statements = self.class.repository_or_fail.query(:subject => @subject)
|
81
|
+
statements = self.class.repository_or_fail.query(:subject => @subject).to_a
|
82
82
|
attributes = {}
|
83
83
|
|
84
84
|
# Set attributes for each statement corresponding to a predicate
|
85
85
|
self.class.properties.each do |name, property|
|
86
86
|
if self.class.is_list?(name)
|
87
87
|
values = Set.new
|
88
|
-
collection = statements.
|
88
|
+
collection = statements.select{|s| s.subject == @subject && s.predicate == property[:predicate]} unless statements.empty?
|
89
89
|
unless collection.nil?
|
90
90
|
collection.each do |statement|
|
91
91
|
values << self.class.build_value(statement,property[:type], @cache)
|
@@ -93,7 +93,7 @@ module Spira
|
|
93
93
|
end
|
94
94
|
attributes[name] = values
|
95
95
|
else
|
96
|
-
statement = statements.
|
96
|
+
statement = statements.select{|s| s.subject == @subject && s.predicate == property[:predicate]}.first unless statements.empty?
|
97
97
|
attributes[name] = self.class.build_value(statement, property[:type], @cache)
|
98
98
|
end
|
99
99
|
end
|
@@ -162,16 +162,12 @@ module Spira
|
|
162
162
|
existed = (self.respond_to?(:before_create) || self.respond_to?(:after_create)) && !self.type.nil? && exists?
|
163
163
|
before_create if self.respond_to?(:before_create) && !self.type.nil? && !existed
|
164
164
|
before_save if self.respond_to?(:before_save)
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
if errors.empty?
|
165
|
+
# we use the non-raising validate and check it to make a slightly different error message. worth it?...
|
166
|
+
case validate
|
167
|
+
when true
|
169
168
|
_update!
|
170
|
-
|
169
|
+
when false
|
171
170
|
raise(ValidationError, "Could not save #{self.inspect} due to validation errors: " + errors.each.join(';'))
|
172
|
-
end
|
173
|
-
else
|
174
|
-
_update!
|
175
171
|
end
|
176
172
|
after_create if self.respond_to?(:after_create) && !self.type.nil? && !existed
|
177
173
|
after_save if self.respond_to?(:after_save)
|
@@ -464,6 +460,28 @@ module Spira
|
|
464
460
|
@errors ||= Spira::Errors.new
|
465
461
|
end
|
466
462
|
|
463
|
+
##
|
464
|
+
# Run any model validations and populate the errors object accordingly.
|
465
|
+
# Returns true if the model is valid, false otherwise
|
466
|
+
#
|
467
|
+
# @return [True, False]
|
468
|
+
def validate
|
469
|
+
unless self.class.validators.empty?
|
470
|
+
errors.clear
|
471
|
+
self.class.validators.each do | validator | self.send(validator) end
|
472
|
+
end
|
473
|
+
errors.empty?
|
474
|
+
end
|
475
|
+
|
476
|
+
##
|
477
|
+
# Run validations on this model and raise a Spira::ValidationError if the validations fail.
|
478
|
+
#
|
479
|
+
# @see #validate
|
480
|
+
# @return true
|
481
|
+
def validate!
|
482
|
+
validate || raise(ValidationError, "Failed to validate #{self.inspect}: " + errors.each.join(';'))
|
483
|
+
end
|
484
|
+
|
467
485
|
##
|
468
486
|
# Returns true if any data exists for this subject in the backing RDF store
|
469
487
|
#
|
data/lib/spira/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 12
|
9
|
+
version: 0.0.12
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ben Lavender
|
@@ -15,13 +15,14 @@ bindir:
|
|
15
15
|
- bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-02-10 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: rdf-spec
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
25
26
|
requirements:
|
26
27
|
- - ">="
|
27
28
|
- !ruby/object:Gem::Version
|
@@ -36,6 +37,7 @@ dependencies:
|
|
36
37
|
name: rspec
|
37
38
|
prerelease: false
|
38
39
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
39
41
|
requirements:
|
40
42
|
- - ">="
|
41
43
|
- !ruby/object:Gem::Version
|
@@ -50,6 +52,7 @@ dependencies:
|
|
50
52
|
name: yard
|
51
53
|
prerelease: false
|
52
54
|
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
53
56
|
requirements:
|
54
57
|
- - ">="
|
55
58
|
- !ruby/object:Gem::Version
|
@@ -64,6 +67,7 @@ dependencies:
|
|
64
67
|
name: rdf
|
65
68
|
prerelease: false
|
66
69
|
requirement: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
67
71
|
requirements:
|
68
72
|
- - ">="
|
69
73
|
- !ruby/object:Gem::Version
|
@@ -78,6 +82,7 @@ dependencies:
|
|
78
82
|
name: rdf-isomorphic
|
79
83
|
prerelease: false
|
80
84
|
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
81
86
|
requirements:
|
82
87
|
- - ">="
|
83
88
|
- !ruby/object:Gem::Version
|
@@ -92,6 +97,7 @@ dependencies:
|
|
92
97
|
name: promise
|
93
98
|
prerelease: false
|
94
99
|
requirement: &id006 !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
95
101
|
requirements:
|
96
102
|
- - ">="
|
97
103
|
- !ruby/object:Gem::Version
|
@@ -115,7 +121,6 @@ files:
|
|
115
121
|
- AUTHORS
|
116
122
|
- README
|
117
123
|
- UNLICENSE
|
118
|
-
- VERSION
|
119
124
|
- lib/spira/base.rb
|
120
125
|
- lib/spira/errors.rb
|
121
126
|
- lib/spira/exceptions.rb
|
@@ -148,6 +153,7 @@ rdoc_options: []
|
|
148
153
|
require_paths:
|
149
154
|
- lib
|
150
155
|
required_ruby_version: !ruby/object:Gem::Requirement
|
156
|
+
none: false
|
151
157
|
requirements:
|
152
158
|
- - ">="
|
153
159
|
- !ruby/object:Gem::Version
|
@@ -157,6 +163,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
157
163
|
- 2
|
158
164
|
version: 1.8.2
|
159
165
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
|
+
none: false
|
160
167
|
requirements:
|
161
168
|
- - ">="
|
162
169
|
- !ruby/object:Gem::Version
|
@@ -166,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
166
173
|
requirements: []
|
167
174
|
|
168
175
|
rubyforge_project: spira
|
169
|
-
rubygems_version: 1.3.
|
176
|
+
rubygems_version: 1.3.7
|
170
177
|
signing_key:
|
171
178
|
specification_version: 3
|
172
179
|
summary: A framework for using the information in RDF.rb repositories as model objects.
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.0.11
|