Skip to content

Commit a24805a

Browse files
committed
Finish 3.1.1
2 parents f06f671 + 484303a commit a24805a

12 files changed

Lines changed: 175 additions & 57 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ Gemfile.lock
1212
benchmark/
1313
/.byebug_history
1414
/coverage/
15+
\#*\#
16+
.\#*

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ group :develop do
1010
gem "rdf-vocab", git: "https://github.com/ruby-rdf/rdf-vocab", branch: "develop"
1111
gem "rdf-xsd", git: "https://github.com/ruby-rdf/rdf-xsd", branch: "develop"
1212

13-
gem "ebnf", git: "https://github.com/gkellogg/ebnf", branch: "develop"
13+
gem "ebnf", git: "https://github.com/dryruby/ebnf", branch: "develop"
1414
gem "sxp", git: "https://github.com/dryruby/sxp", branch: "develop"
1515

1616
gem 'rest-client-components'

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.0
1+
3.1.1

lib/rdf/changeset.rb

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ module RDF
3131
#
3232
# @since 2.0.0
3333
class Changeset
34-
include RDF::Mutable
34+
# include RDF::Mutable
35+
include RDF::Util::Coercions
3536

3637
##
37-
# Applies a changeset to the given mutable RDF::Enumerable .
38+
# Applies a changeset to the given {RDF::Mutable} object.
3839
#
3940
# @param [RDF::Mutable] mutable
4041
# @param [Hash{Symbol => Object}] options
@@ -98,6 +99,24 @@ def readable?
9899
false
99100
end
100101

102+
##
103+
# Returns `false` as changesets are not {RDF::Writable}.
104+
#
105+
# @return [Boolean]
106+
# @see RDF::Writable#writable?
107+
def writable?
108+
false
109+
end
110+
111+
##
112+
# Returns `false` as changesets are not {RDF::Mutable}.
113+
#
114+
# @return [Boolean]
115+
# @see RDF::Mutable#mutable?
116+
def mutable?
117+
false
118+
end
119+
101120
##
102121
# Applies this changeset to the given mutable RDF::Enumerable.
103122
#
@@ -127,35 +146,77 @@ def inspect
127146

128147
##
129148
# Outputs a developer-friendly representation of this changeset to
130-
# `stderr`.
149+
# `$stderr`.
131150
#
132151
# @return [void]
133152
def inspect!
134153
$stderr.puts(self.inspect)
135154
end
136155

137-
protected
138-
139156
##
140-
# Appends an RDF statement to the sequence to insert when applied.
157+
# Returns the sum of both the `inserts` and `deletes` counts.
141158
#
142-
# @param [RDF::Statement] statement
143-
# @return [void]
144-
# @see RDF::Writable#insert_statement
145-
def insert_statement(statement)
146-
self.inserts << statement
159+
# @return [Integer]
160+
def count
161+
inserts.count + deletes.count
147162
end
148163

164+
# Append statements to `inserts`. Statements _should_ be constant
165+
# as variable statements will at best be ignored or at worst raise
166+
# an error when applied.
167+
#
168+
# @param statements [Enumerable, RDF::Statement] Some statements
169+
# @return [self]
170+
def insert(*statements)
171+
coerce_statements(statements) do |stmts|
172+
append_statements :inserts, stmts
173+
end
174+
175+
self
176+
end
177+
alias_method :insert!, :insert
178+
alias_method :<<, :insert
179+
180+
# Append statements to `deletes`. Statements _may_ contain
181+
# variables, although support will depend on the {RDF::Mutable}
182+
# target.
183+
#
184+
# @param statements [Enumerable, RDF::Statement] Some statements
185+
# @return [self]
186+
def delete(*statements)
187+
coerce_statements(statements) do |stmts|
188+
append_statements :deletes, stmts
189+
end
190+
191+
self
192+
end
193+
alias_method :delete!, :delete
194+
alias_method :>>, :delete
195+
196+
private
197+
149198
##
150-
# Appends an RDF statement to the sequence to delete when applied.
199+
# Append statements to the appropriate target. This is a little
200+
# shim to go in between the other shim and the target.
151201
#
152-
# @param [RDF::Statement] statement
153-
# @return [void]
154-
# @see RDF::Mutable#delete_statement
155-
def delete_statement(statement)
156-
self.deletes << statement
202+
# @param target [Symbol] the method to send
203+
# @param arg [Enumerable, RDF::Statement]
204+
#
205+
def append_statements(target, arg)
206+
# coerce to an enumerator
207+
stmts = case
208+
when arg.is_a?(RDF::Statement)
209+
[arg]
210+
when arg.respond_to?(:each_statement)
211+
arg.each_statement
212+
when arg.respond_to?(:each)
213+
arg
214+
else
215+
raise ArgumentError, "Invalid statement: #{arg.class}"
216+
end
217+
218+
stmts.each { |s| send(target) << s }
157219
end
158220

159-
undef_method :load, :update, :clear
160221
end # Changeset
161222
end # RDF

lib/rdf/mixin/mutable.rb

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Mutable
99
extend RDF::Util::Aliasing::LateBound
1010
include RDF::Readable
1111
include RDF::Writable
12+
include RDF::Util::Coercions
1213

1314
##
1415
# Returns `true` if `self` is mutable.
@@ -154,20 +155,9 @@ def update(*statements)
154155
def delete(*statements)
155156
raise TypeError.new("#{self} is immutable") if immutable?
156157

157-
statements.map! do |value|
158-
case
159-
when value.respond_to?(:each_statement)
160-
delete_statements(value)
161-
nil
162-
when (statement = Statement.from(value)).constant?
163-
statement
164-
else
165-
delete_statements(query(value))
166-
nil
167-
end
158+
coerce_statements(statements, query: true, constant: true) do |value|
159+
delete_statements(value)
168160
end
169-
statements.compact!
170-
delete_statements(statements) unless statements.empty?
171161

172162
return self
173163
end

lib/rdf/mixin/writable.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module RDF
77
# @see RDF::Repository
88
module Writable
99
extend RDF::Util::Aliasing::LateBound
10+
include RDF::Util::Coercions
1011

1112
##
1213
# Returns `true` if `self` is writable.
@@ -58,26 +59,13 @@ def <<(data)
5859
# @param [Enumerable<RDF::Statement>] statements
5960
# @return [self]
6061
def insert(*statements)
61-
statements.map! do |value|
62-
case
63-
when value.respond_to?(:each_statement)
64-
insert_statements(value)
65-
nil
66-
when (statement = Statement.from(value))
67-
statement
68-
else
69-
raise ArgumentError.new("not a valid statement: #{value.inspect}")
70-
end
71-
end
72-
statements.compact!
73-
insert_statements(statements) unless statements.empty?
62+
coerce_statements(statements) { |value| insert_statements value }
7463

7564
return self
7665
end
7766
alias_method :insert!, :insert
7867

7968
protected
80-
8169
##
8270
# Inserts statements from the given RDF reader into the underlying
8371
# storage or output stream.

lib/rdf/repository.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,14 @@ def each_statement(&block)
340340
# @see Mutable#apply_changeset
341341
def apply_changeset(changeset)
342342
data = @data
343-
changeset.deletes.each { |del| data = delete_from(data, del) }
343+
changeset.deletes.each do |del|
344+
if del.constant?
345+
data = delete_from(data, del)
346+
else
347+
# we need this condition to handle wildcard statements
348+
query_pattern(del) { |stmt| data = delete_from(data, stmt) }
349+
end
350+
end
344351
changeset.inserts.each { |ins| data = insert_to(data, ins) }
345352
@data = data
346353
end

lib/rdf/util.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module RDF; module Util
2-
autoload :Aliasing, 'rdf/util/aliasing'
3-
autoload :Cache, 'rdf/util/cache'
4-
autoload :File, 'rdf/util/file'
5-
autoload :Logger, 'rdf/util/logger'
6-
autoload :UUID, 'rdf/util/uuid'
2+
autoload :Aliasing, 'rdf/util/aliasing'
3+
autoload :Cache, 'rdf/util/cache'
4+
autoload :File, 'rdf/util/file'
5+
autoload :Logger, 'rdf/util/logger'
6+
autoload :UUID, 'rdf/util/uuid'
7+
autoload :Coercions, 'rdf/util/coercions'
78
end; end

lib/rdf/util/coercions.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
module RDF
3+
module Util
4+
module Coercions
5+
# This is a provisional module intended to house input
6+
# coercions. Currently the only coercion is a statement
7+
# preprocessor that is used in e.g. {RDF::Writable#insert} and
8+
# {RDF::Mutable#delete}.
9+
10+
protected
11+
12+
##
13+
# Coerce an array of arguments into {RDF::Statement}, or
14+
# {RDF::Enumerable} and then yield to a block. Note that this
15+
# code was amalgamated from that which was sandwiched around
16+
# both {RDF::Writable#insert_statements} and
17+
# {RDF::Mutable#delete_statements}. The parameters `query` and
18+
# `constant` are therefore present to handle the conditions
19+
# where the statements contain wildcards and what to do about
20+
# them.
21+
#
22+
# @example
23+
# coerce_statements(statements) { |value| do_something(value) }
24+
#
25+
# @param statements [#map] The arbitrary-ish input to be manipulated
26+
# @param query [false, true] Whether to call `query` before the block
27+
# (as expected by {Mutable#delete_statements})
28+
# @param constant [false, true] Whether to test if the statements
29+
# are constant (as expected by {Mutable#delete_statements})
30+
# @yield [RDF::Statement, RDF::Enumerable]
31+
# @return statements
32+
def coerce_statements(statements, query: false, constant: false, &block)
33+
raise ArgumentError, 'expecting a block' unless block_given?
34+
35+
statements = statements.map do |value|
36+
case
37+
when value.respond_to?(:each_statement)
38+
block.call(value)
39+
nil
40+
when (statement = Statement.from(value)) &&
41+
(!constant || statement.constant?)
42+
statement
43+
when query
44+
# XXX note that this only makes sense when the module is include()d
45+
block.call(self.query(value))
46+
nil
47+
else
48+
raise ArgumentError, "Not a valid statement: #{value.inspect}"
49+
end
50+
end.compact
51+
52+
block.call(statements) unless statements.empty?
53+
54+
# eh might as well return these
55+
statements
56+
end
57+
58+
end
59+
end
60+
end

lib/rdf/vocab/xsd.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class XSD < RDF::Vocabulary("http://www.w3.org/2001/XMLSchema#")
190190
comment: %(
191191
boolean represents the values of two-valued logic.
192192
).freeze,
193-
label: "base64Binary".freeze,
193+
label: "boolean".freeze,
194194
subClassOf: "xsd:anyAtomicType".freeze,
195195
type: "rdfs:Datatype".freeze
196196
term :byte,

0 commit comments

Comments
 (0)