@@ -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
@@ -86,9 +87,9 @@ def initialize(insert: [], delete: [], &block)
8687 end
8788
8889 ##
89- # Returns ` false` to indicate that this changeset is append-only.
90+ # Returns + false+ to indicate that this changeset is append-only.
9091 #
91- # Changesets do not support the ` RDF::Enumerable` protocol directly.
92+ # Changesets do not support the + RDF::Enumerable+ protocol directly.
9293 # To enumerate the RDF statements to be inserted or deleted, use the
9394 # {RDF::Changeset#inserts} and {RDF::Changeset#deletes} accessors.
9495 #
@@ -98,6 +99,14 @@ def readable?
9899 false
99100 end
100101
102+ def writable?
103+ false
104+ end
105+
106+ def mutable?
107+ false
108+ end
109+
101110 ##
102111 # Applies this changeset to the given mutable RDF::Enumerable.
103112 #
@@ -111,7 +120,7 @@ def apply(mutable, **options)
111120 end
112121
113122 ##
114- # @return [Boolean] ` true` iff inserts and deletes are both empty
123+ # @return [Boolean] + true+ iff inserts and deletes are both empty
115124 def empty?
116125 deletes . empty? && inserts . empty?
117126 end
@@ -127,41 +136,77 @@ def inspect
127136
128137 ##
129138 # Outputs a developer-friendly representation of this changeset to
130- # ` stderr` .
139+ # +$ stderr+ .
131140 #
132141 # @return [void]
133142 def inspect!
134143 $stderr. puts ( self . inspect )
135144 end
136145
137- protected
138-
139146 ##
140- # Appends an RDF statement to the sequence to insert when applied .
147+ # Returns the sum of both the +inserts+ and +deletes+ counts .
141148 #
142- # @param [RDF::Statement] statement
143- # @return [void]
144- # @see RDF::Writable#insert_statement
145- def insert_statement ( statement )
146- self . inserts << statement
149+ # @return [Integer]
150+ def count
151+ inserts . count + deletes . count
147152 end
148153
149- ##
150- # Appends an RDF statement to the sequence to delete when applied.
154+ # Append statements to +inserts+. Statements _should_ be constant
155+ # as variable statements will at best be ignored or at worst raise
156+ # an error when applied.
151157 #
152- # @param [RDF::Statement] statement
153- # @return [void]
154- # @see RDF::Mutable#delete_statement
155- def delete_statement ( statement )
156- self . deletes << statement
158+ # @param statements [Enumerable, # RDF::Statement] Some statements
159+ # @return [self]
160+ def insert ( *statements )
161+ process_statements ( statements ) do |stmts |
162+ append_statements :inserts , stmts
163+ end
164+
165+ self
166+ end
167+ alias_method :insert! , :insert
168+ alias_method :<< , :insert
169+
170+ # Append statements to +deletes+. Statements _may_ contain
171+ # variables, although support will depend on the {RDF::Mutable}
172+ # target.
173+ #
174+ # @param statements [Enumerable, RDF::Statement] Some statements
175+ # @return [self]
176+ def delete ( *statements )
177+ process_statements ( statements ) do |stmts |
178+ append_statements :deletes , stmts
179+ end
180+
181+ self
157182 end
183+ alias_method :delete! , :delete
184+ alias_method :>> , :delete
185+
186+ private
158187
159- # This simply returns its argument as a query in order to trick
160- # {RDF::Mutable#delete} into working.
161- def query ( stmt )
162- RDF ::Query . new RDF ::Query ::Pattern . from ( stmt )
188+ ##
189+ # Append statements to the appropriate target. This is a crappy
190+ # little shim to go in between the other shim and the target.
191+ #
192+ # @param target [Symbol] the method to send
193+ # @param arg [Enumerable, RDF::Statement]
194+ #
195+ def append_statements ( target , arg )
196+ # coerce to an enumerator
197+ stmts = case
198+ when arg . is_a? ( RDF ::Statement )
199+ [ arg ]
200+ when arg . respond_to? ( :each_statement )
201+ arg . each_statement
202+ when arg . respond_to? ( :each )
203+ arg
204+ else
205+ raise ArgumentError , "Invalid statement: #{ arg . class } "
206+ end
207+
208+ stmts . each { |s | send ( target ) << s }
163209 end
164210
165- undef_method :load , :update , :clear
166211 end # Changeset
167212end # RDF
0 commit comments