spira 0.0.4 → 0.0.5

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/AUTHORS CHANGED
@@ -1 +1,2 @@
1
1
  * Ben Lavender <blavender@gmail.com>
2
+ * Nicholas J Humfrey <njh@aelius.com>
data/CHANGES.md CHANGED
@@ -1,6 +1,15 @@
1
1
  # Changelog for Spira <http://github.com/datagraph/spira>
2
2
 
3
- ## untagged
3
+ ## 0.0.5
4
+ * Relations can now find related classes in modules, either by absolute
5
+ reference, or by class name if they are in the same namespace.
6
+ * Fix a bug with default_vocabulary in which a '/' was appended to
7
+ vocabularies ending in '#'
8
+ * Fix a bug with the Decimal type where round-tripping was incorrect
9
+ * Fix some error messages that were missing closing parentheses
10
+
11
+ ## 0.0.4
12
+ * Added a Decimal type
4
13
  * Small updates for RDF.rb 0.2.0 compatibility
5
14
  * Add a Spira::Base class that can be inherited from for users who prefer to
6
15
  inherit rather than include.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
@@ -139,13 +139,7 @@ module Spira
139
139
  when type.is_a?(Class) && type.ancestors.include?(Spira::Type)
140
140
  type.unserialize(statement.object)
141
141
  when type.is_a?(Symbol) || type.is_a?(String)
142
- klass = begin
143
- Kernel.const_get(type.to_s)
144
- rescue NameError
145
- unless klass.is_a?(Class) && klass.ancestors.include?(Spira::Resource)
146
- raise TypeError, "#{type} is not a Spira Resource (referenced as #{type} by #{self}"
147
- end
148
- end
142
+ klass = classize_resource(type)
149
143
  promise { klass.for(statement.object) }
150
144
  else
151
145
  raise TypeError, "Unable to unserialize #{statement.object} as #{type}"
@@ -159,7 +153,13 @@ module Spira
159
153
  when type.is_a?(Class) && type.ancestors.include?(Spira::Type)
160
154
  type.serialize(value)
161
155
  when value && value.class.ancestors.include?(Spira::Resource)
156
+ klass = classize_resource(type)
157
+ unless klass.ancestors.include?(value.class)
158
+ raise TypeError, "#{value} is an instance of #{value.class}, expected #{klass}"
159
+ end
162
160
  value.subject
161
+ when type.is_a?(Symbol) || type.is_a?(String)
162
+ klass = classize_resource(type)
163
163
  else
164
164
  raise TypeError, "Unable to serialize #{value} as #{type}"
165
165
  end
@@ -167,6 +167,52 @@ module Spira
167
167
 
168
168
  private
169
169
 
170
+ # Return the appropriate class object for a string or symbol
171
+ # representation. Throws errors correctly if the given class cannot be
172
+ # located, or if it is not a Spira::Resource
173
+ #
174
+ def classize_resource(type)
175
+ klass = nil
176
+ begin
177
+ klass = qualified_const_get(type.to_s)
178
+ rescue NameError
179
+ raise NameError, "Could not find relation class #{type} (referenced as #{type} by #{self})"
180
+ klass.is_a?(Class) && klass.ancestors.include?(Spira::Resource)
181
+ end
182
+ unless klass.is_a?(Class) && klass.ancestors.include?(Spira::Resource)
183
+ raise TypeError, "#{type} is not a Spira Resource (referenced as #{type} by #{self})"
184
+ end
185
+ klass
186
+ end
187
+
188
+ # Resolve a constant from a string, relative to this class' namespace, if
189
+ # available, and from root, otherwise.
190
+ #
191
+ # FIXME: this is not really 'qualified', but it's one of those
192
+ # impossible-to-name functions. Open to suggestions.
193
+ #
194
+ # @author njh
195
+ # @private
196
+ def qualified_const_get(str)
197
+ path = str.to_s.split('::')
198
+ from_root = path[0].empty?
199
+ if from_root
200
+ from_root = []
201
+ path = path[1..-1]
202
+ else
203
+ start_ns = ((Class === self)||(Module === self)) ? self : self.class
204
+ from_root = start_ns.to_s.split('::')
205
+ end
206
+ until from_root.empty?
207
+ begin
208
+ return (from_root+path).inject(Object) { |ns,name| ns.const_get(name) }
209
+ rescue NameError
210
+ from_root.delete_at(-1)
211
+ end
212
+ end
213
+ path.inject(Object) { |ns,name| ns.const_get(name) }
214
+ end
215
+
170
216
  ##
171
217
  # Add getters and setters for a property or list.
172
218
  # @private
@@ -177,7 +223,7 @@ module Spira
177
223
  when @default_vocabulary.nil?
178
224
  raise TypeError, "A :predicate option is required for types without a default vocabulary"
179
225
  else @default_vocabulary
180
- separator = @default_vocabulary.to_s[-1,1] == "/" ? '' : '/'
226
+ separator = @default_vocabulary.to_s[-1,1] =~ /(\/|#)/ ? '' : '/'
181
227
  RDF::URI.new(@default_vocabulary.to_s + separator + name.to_s)
182
228
  end
183
229
  type = case
@@ -301,7 +301,7 @@ module Spira
301
301
  # @return [RDF::URI]
302
302
  # @raise [NoMethodError]
303
303
  def to_uri
304
- uri || (raise NoMethodError, "No such method: :to_uri (this instance's subject is not a URI")
304
+ uri || (raise NoMethodError, "No such method: :to_uri (this instance's subject is not a URI)")
305
305
  end
306
306
 
307
307
  ##
@@ -319,7 +319,7 @@ module Spira
319
319
  # @return [RDF::Node]
320
320
  # @raise [NoMethodError]
321
321
  def to_node
322
- @subject.node? ? @subject : (raise NoMethodError, "No such method: :to_uri (this instance's subject is not a URI")
322
+ @subject.node? ? @subject : (raise NoMethodError, "No such method: :to_uri (this instance's subject is not a URI)")
323
323
  end
324
324
 
325
325
  ##
@@ -1,3 +1,5 @@
1
+ require 'bigdecimal'
2
+
1
3
  module Spira::Types
2
4
 
3
5
  ##
@@ -13,14 +15,12 @@ module Spira::Types
13
15
  include Spira::Type
14
16
 
15
17
  def self.unserialize(value)
16
- require 'bigdecimal' unless defined?(BigDecimal)
17
18
  object = value.object
18
19
  object.is_a?(BigDecimal) ? object : BigDecimal.new(object.to_s)
19
20
  end
20
21
 
21
22
  def self.serialize(value)
22
- require 'bigdecimal' unless defined?(BigDecimal)
23
- RDF::Literal.new(value, :datatype => XSD.decimal)
23
+ RDF::Literal.new(value.is_a?(BigDecimal) ? value.to_s('F') : value.to_s, :datatype => XSD.decimal)
24
24
  end
25
25
 
26
26
  register_alias XSD.decimal
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 = 4
5
+ TINY = 5
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
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
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-06-21 00:00:00 -05:00
18
+ date: 2010-06-28 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -83,9 +83,9 @@ dependencies:
83
83
  - !ruby/object:Gem::Version
84
84
  segments:
85
85
  - 0
86
- - 1
87
86
  - 2
88
- version: 0.1.2
87
+ - 0
88
+ version: 0.2.0
89
89
  type: :runtime
90
90
  version_requirements: *id005
91
91
  - !ruby/object:Gem::Dependency
@@ -97,9 +97,9 @@ dependencies:
97
97
  - !ruby/object:Gem::Version
98
98
  segments:
99
99
  - 0
100
- - 1
101
- - 1
102
- version: 0.1.1
100
+ - 2
101
+ - 0
102
+ version: 0.2.0
103
103
  type: :runtime
104
104
  version_requirements: *id006
105
105
  description: Spira is a framework for using the information in RDF.rb repositories as model objects.