-
Notifications
You must be signed in to change notification settings - Fork 98
/
rdf.rb
233 lines (209 loc) · 6.37 KB
/
rdf.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
require 'stringio'
require 'bigdecimal'
require 'date'
require 'time'
require 'rdf/version'
module RDF
# RDF mixins
autoload :Countable, 'rdf/mixin/countable'
autoload :Durable, 'rdf/mixin/durable'
autoload :Enumerable, 'rdf/mixin/enumerable'
autoload :Indexable, 'rdf/mixin/indexable'
autoload :Mutable, 'rdf/mixin/mutable'
autoload :Queryable, 'rdf/mixin/queryable'
autoload :Readable, 'rdf/mixin/readable'
autoload :TypeCheck, 'rdf/mixin/type_check'
autoload :Transactable, 'rdf/mixin/transactable'
autoload :Writable, 'rdf/mixin/writable'
# RDF objects
autoload :Graph, 'rdf/model/graph'
autoload :IRI, 'rdf/model/uri'
autoload :Literal, 'rdf/model/literal'
autoload :Node, 'rdf/model/node'
autoload :Resource, 'rdf/model/resource'
autoload :Statement, 'rdf/model/statement'
autoload :URI, 'rdf/model/uri'
autoload :Value, 'rdf/model/value'
autoload :Term, 'rdf/model/term'
# RDF collections
autoload :List, 'rdf/model/list'
# RDF serialization
autoload :Format, 'rdf/format'
autoload :Reader, 'rdf/reader'
autoload :ReaderError, 'rdf/reader'
autoload :Writer, 'rdf/writer'
autoload :WriterError, 'rdf/writer'
# RDF serialization formats
autoload :NTriples, 'rdf/ntriples'
autoload :NQuads, 'rdf/nquads'
# RDF storage
autoload :Changeset, 'rdf/changeset'
autoload :Dataset, 'rdf/model/dataset'
autoload :Repository, 'rdf/repository'
autoload :Transaction, 'rdf/transaction'
# RDF querying
autoload :Query, 'rdf/query'
# RDF vocabularies
autoload :Vocabulary, 'rdf/vocabulary'
autoload :StrictVocabulary, 'rdf/vocabulary'
VOCABS = Dir.glob(File.join(File.dirname(__FILE__), 'rdf', 'vocab', '*.rb')).map { |f| File.basename(f)[0...-(File.extname(f).size)].to_sym } rescue []
# Use const_missing instead of autoload to load most vocabularies so we can provide deprecation messages
def self.const_missing(constant)
if VOCABS.include?(constant.to_s.downcase.to_sym)
require "rdf/vocab/#{constant.to_s.downcase}"
const_get(constant)
else
super
end
end
# Utilities
autoload :Util, 'rdf/util'
##
# Alias for `RDF::Resource.new`.
#
# @param (see RDF::Resource#initialize)
# @return [RDF::Resource]
def self.Resource(*args, &block)
Resource.new(*args, &block)
end
##
# Alias for `RDF::Node.new`.
#
# @param (see RDF::Node#initialize)
# @return [RDF::Node]
def self.Node(*args, &block)
Node.new(*args, &block)
end
##
# Cast to a URI. If already a URI, return the passed argument.
#
# @param (see RDF::URI#initialize)
# @return [RDF::URI]
def self.URI(*args, &block)
(uri = args.first).respond_to?(:to_uri) ? uri.to_uri : URI.new(*args, &block)
end
##
# Alias for `RDF::Literal.new`.
#
# @param (see RDF::Literal#initialize)
# @return [RDF::Literal]
def self.Literal(*args, &block)
case literal = args.first
when RDF::Literal then literal
else Literal.new(*args, &block)
end
end
##
# Alias for `RDF::Graph.new`.
#
# @param (see RDF::Graph#initialize)
# @return [RDF::Graph]
def self.Graph(**options, &block)
Graph.new(options, &block)
end
##
# @overload List()
# @return [RDF::URI] returns the IRI for `rdf:List`
#
# @overload List(*args)
# @param (see RDF::List#[])
# @return [RDF::List]
#
# @overload List(array)
# @param [Array] array
# @return [RDF::List]
#
# @overload List(list)
# @param [RDF::List] list
# @return [RDF::List] returns itself
def self.List(*args)
case
when args.empty?
RDF[:List]
when args.length == 1 && args.first.is_a?(RDF::List)
args.first
when args.length == 1 && args.first.is_a?(Array)
List[*args.first]
else
List[*args]
end
end
##
# @overload Statement()
# @return [RDF::URI] returns the IRI for `rdf:Statement`
#
# @overload Statement(**options)
# @param [Hash{Symbol => Object}] options
# @option options [RDF::Resource] :subject (nil)
# @option options [RDF::URI] :predicate (nil)
# @option options [RDF::Term] :object (nil)
# @option options [RDF::Resource] :graph_name (nil)
# Note, a graph_name MUST be an IRI or BNode.
# @return [RDF::Statement]
#
# @overload Statement(subject, predicate, object, **options)
# @param [RDF::Resource] subject
# @param [RDF::URI] predicate
# @param [RDF::Term] object
# @param [Hash{Symbol => Object}] options
# @option options [RDF::Resource] :graph_name (nil)
# @return [RDF::Statement]
#
def self.Statement(*args)
if args.empty?
RDF[:Statement]
else
Statement.new(*args)
end
end
##
# Alias for `RDF::Vocabulary.create`.
#
# @param (see RDF::Vocabulary#initialize)
# @return [Class]
def self.Vocabulary(uri)
Vocabulary.create(uri)
end
##
# Alias for `RDF::StrictVocabulary.create`.
#
# @param (see RDF::Vocabulary#initialize)
# @return [Class]
def self.StrictVocabulary(prefix)
StrictVocabulary.create(prefix)
end
##
# @return [#to_s] property
# @return [URI]
def self.[](property)
property.to_s =~ %r{_\d+} ? RDF::URI("#{to_uri}#{property}") : RDF::RDFV[property]
end
##
# Return an enumerator over {RDF::Statement} defined for this vocabulary.
# @return [RDF::Enumerable::Enumerator]
# @see Object#enum_for
def self.enum_for(method = :each_statement, *args)
# Ensure that enumerators are, themselves, queryable
Enumerable::Enumerator.new do |yielder|
RDF::RDFV.send(method, *args) {|*y| yielder << (y.length > 1 ? y : y.first)}
end
end
class << self
alias_method :to_enum, :enum_for
end
##
# respond to module or RDFV
def self.respond_to?(method, include_all = false)
super || RDF::RDFV.respond_to?(method, include_all)
end
##
# Delegate other methods to RDF::RDFV
def self.method_missing(property, *args, &block)
if args.empty?
# Special-case rdf:_n for all integers
property.to_s =~ %r{_\d+} ? RDF::URI("#{to_uri}#{property}") : RDF::RDFV.send(property)
else
super
end
end
end