spira 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -5,12 +5,14 @@ It's time to breathe life into your linked data.
5
5
  ---
6
6
 
7
7
  ## Synopsis
8
- Spira is a framework for using the information in RDF.rb repositories as model
8
+ Spira is a framework for using the information in [RDF.rb][] repositories as model
9
9
  objects. It gives you the ability to work in a resource-oriented way without
10
10
  losing access to statement-oriented nature of linked data, if you so choose.
11
11
  It can be used either to access existing RDF data in a resource-oriented way,
12
12
  or to create a new store of RDF data based on simple defaults.
13
13
 
14
+ An introductory blog post is at <http://blog.datagraph.org/2010/05/spira>
15
+
14
16
  ### Example
15
17
 
16
18
  class Person
@@ -332,12 +334,12 @@ built in `assert` and assert helpers such as `assert_set` and
332
334
  end
333
335
  end
334
336
 
335
- dancing-queen.artist = nickelback
336
- dancing-queen.save! #=> ValidationError
337
- dancing-queen.errors.each => ["artist cannot be Nickelback"]
337
+ dancing_queen.artist = nickelback
338
+ dancing_queen.save! #=> ValidationError
339
+ dancing_queen.errors.each #=> ["artist cannot be Nickelback"]
338
340
 
339
- dancing-queen.artist = abba
340
- dancing-queen.save! #=> true
341
+ dancing_queen.artist = abba
342
+ dancing_queen.save! #=> true
341
343
 
342
344
 
343
345
  ## Inheritance
@@ -391,3 +393,4 @@ Fork it on Github and go. Please make sure you're kosher with the UNLICENSE
391
393
  file before contributing.
392
394
 
393
395
  [public-rdf-ruby w3c mailing list]: http://lists.w3.org/Archives/Public/public-rdf-ruby/
396
+ [RDF.rb]: http://rdf.rubyforge.org
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/lib/spira.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rdf'
2
+ require 'spira/exceptions'
2
3
 
3
4
  ##
4
5
  # Spira is a framework for building projections of RDF data into Ruby classes.
@@ -103,7 +104,6 @@ module Spira
103
104
  autoload :Errors, 'spira/errors'
104
105
  autoload :VERSION, 'spira/version'
105
106
 
106
- class ValidationError < StandardError; end
107
107
  end
108
108
 
109
109
  module RDF
@@ -0,0 +1,11 @@
1
+ module Spira
2
+
3
+ ##
4
+ # For cases when a method is called which requires a `type` method be
5
+ # declared on a Spira class.
6
+ class NoTypeError < StandardError ; end
7
+
8
+ ##
9
+ # For cases when a projection fails a validation check
10
+ class ValidationError < StandardError; end
11
+ end
@@ -64,8 +64,8 @@ module Spira
64
64
  end
65
65
 
66
66
  # This lets including classes reference vocabularies without the RDF:: prefix
67
- include RDF
68
67
  include Spira::Types
68
+ include RDF
69
69
  include InstanceMethods
70
70
 
71
71
  end
@@ -72,14 +72,12 @@ module Spira
72
72
  case identifier
73
73
  when RDF::URI
74
74
  identifier
75
- when String
76
- uri = RDF::URI.new(identifier)
75
+ else
76
+ uri = RDF::URI.new(identifier.to_s)
77
77
  return uri if uri.absolute?
78
78
  raise ArgumentError, "Cannot create identifier for #{self} by String without base_uri; RDF::URI required" if self.base_uri.nil?
79
79
  separator = self.base_uri.to_s[-1,1] == "/" ? '' : '/'
80
- RDF::URI.new(self.base_uri.to_s + separator + identifier)
81
- else
82
- raise ArgumentError, "Cannot create an identifier for #{self} from #{identifier}, expected RDF::URI or String"
80
+ RDF::URI.new(self.base_uri.to_s + separator + identifier.to_s)
83
81
  end
84
82
  end
85
83
 
@@ -89,15 +87,42 @@ module Spira
89
87
  # This method is only valid for classes which declare a `type` with the
90
88
  # `type` method in the DSL.
91
89
  #
92
- # @raise [TypeError] if the resource class does not have an RDF type declared
90
+ # @raise [Spira::NoTypeError] if the resource class does not have an RDF type declared
93
91
  # @return [Integer] the count
94
92
  # @see Spira::Resource::DSL
95
93
  def count
96
- raise TypeError, "Cannot count a #{self} without a reference type URI." if @type.nil?
97
- result = repository.query(:predicate => RDF.type, :object => @type)
98
- result.count
94
+ raise Spira::NoTypeError, "Cannot count a #{self} without a reference type URI." if @type.nil?
95
+ repository.query(:predicate => RDF.type, :object => @type).subjects.count
96
+ end
97
+
98
+ ##
99
+ # Enumerate over all resources projectable as this class. This method is
100
+ # only valid for classes which declare a `type` with the `type` method in
101
+ # the DSL.
102
+ #
103
+ # @raise [Spira::NoTypeError] if the resource class does not have an RDF type declared
104
+ # @overload each
105
+ # @yield [instance] A block to perform for each available projection of this class
106
+ # @yieldparam [self] instance
107
+ # @yieldreturn [Void]
108
+ # @return [Void]
109
+ #
110
+ # @overload each
111
+ # @return [Enumerator]
112
+ # @see Spira::Resource::DSL
113
+ def each(&block)
114
+ raise Spira::NoTypeError, "Cannot count a #{self} without a reference type URI." if @type.nil?
115
+ case block_given?
116
+ when false
117
+ enum_for(:each)
118
+ else
119
+ repository.query(:predicate => RDF.type, :object => @type).each_subject do |subject|
120
+ block.call(self.for(subject))
121
+ end
122
+ end
99
123
  end
100
124
 
125
+
101
126
  ##
102
127
  # Returns true if the given property is a has_many property, false otherwise
103
128
  #
@@ -176,7 +176,23 @@ module Spira
176
176
  def type=(type)
177
177
  raise TypeError, "Cannot reassign RDF.type for #{self}; consider appending to a has_many :types"
178
178
  end
179
-
179
+
180
+ ##
181
+ # Returns the RDF representation of this resource.
182
+ #
183
+ # @return [RDF::Enumerable]
184
+ def to_rdf
185
+ self
186
+ end
187
+
188
+ ##
189
+ # Returns the URI representation of this resource.
190
+ #
191
+ # @return [RDF::URI]
192
+ def to_uri
193
+ uri
194
+ end
195
+
180
196
  ##
181
197
  # A developer-friendly view of this projection
182
198
  #
@@ -255,6 +271,7 @@ module Spira
255
271
  def ==(other)
256
272
  case other
257
273
  # TODO: define behavior for equality on subclasses.
274
+ # TODO: should we compare attributes here?
258
275
  when self.class
259
276
  @uri == other.uri
260
277
  when RDF::Enumerable
data/lib/spira/types.rb CHANGED
@@ -19,6 +19,7 @@ module Spira
19
19
  require 'spira/types/any'
20
20
  require 'spira/types/string'
21
21
  require 'spira/types/float'
22
+ require 'spira/types/uri'
22
23
 
23
24
 
24
25
  end
@@ -0,0 +1,25 @@
1
+ module Spira::Types
2
+
3
+ ##
4
+ # This type takes RDF Resource objects and provides RDF::URI objects for the
5
+ # ruby representation.
6
+ #
7
+ # @see Spira::Type
8
+ # @see http://rdf.rubyforge.org/RDF/URI.html
9
+ class URI
10
+
11
+ include Spira::Type
12
+
13
+ def self.unserialize(value)
14
+ RDF::URI(value)
15
+ end
16
+
17
+ def self.serialize(value)
18
+ RDF::URI(value)
19
+ end
20
+
21
+ register_alias :uri
22
+ register_alias RDF::URI
23
+
24
+ end
25
+ end
data/lib/spira/version.rb CHANGED
@@ -2,7 +2,7 @@ module Spira
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 2
6
6
  EXTRA = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Lavender
@@ -15,7 +15,7 @@ bindir:
15
15
  - bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-05-21 00:00:00 +02:00
18
+ date: 2010-06-04 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -116,6 +116,7 @@ files:
116
116
  - UNLICENSE
117
117
  - VERSION
118
118
  - lib/spira/errors.rb
119
+ - lib/spira/exceptions.rb
119
120
  - lib/spira/resource/class_methods.rb
120
121
  - lib/spira/resource/dsl.rb
121
122
  - lib/spira/resource/instance_methods.rb
@@ -127,10 +128,11 @@ files:
127
128
  - lib/spira/types/float.rb
128
129
  - lib/spira/types/integer.rb
129
130
  - lib/spira/types/string.rb
131
+ - lib/spira/types/uri.rb
130
132
  - lib/spira/types.rb
131
133
  - lib/spira/version.rb
132
134
  - lib/spira.rb
133
- has_rdoc: false
135
+ has_rdoc: yard
134
136
  homepage: http://spira.rubyforge.org
135
137
  licenses:
136
138
  - Public Domain