Skip to content

Commit 70dfeaa

Browse files
committed
Refactor #insert_statement & #delete_statement
Use private methods to create mongo query for re-use in block syntax
1 parent 73e2e6d commit 70dfeaa

1 file changed

Lines changed: 26 additions & 19 deletions

File tree

lib/rdf/mongo.rb

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ class Statement
88
# Creates a BSON representation of the statement.
99
# @return [Hash]
1010
def to_mongo
11-
self.to_hash.inject({}) do |hash, (place_in_statement, entity)|
12-
hash.merge(RDF::Mongo::Conversion.to_mongo(entity, place_in_statement))
11+
self.to_hash.inject({}) do |hash, (place_in_statement, entity)|
12+
hash.merge(RDF::Mongo::Conversion.to_mongo(entity, place_in_statement))
1313
end
1414
end
15-
15+
1616
##
1717
# Create BSON for a statement representation. Note that if the statement has no graph name,
1818
# a value of `false` will be used to indicate the default context
@@ -27,7 +27,7 @@ def self.from_mongo(statement)
2727
graph_name: RDF::Mongo::Conversion.from_mongo(statement['c'], statement['ct'], statement['cl']))
2828
end
2929
end
30-
30+
3131
module Mongo
3232
autoload :VERSION, "rdf/mongo/version"
3333

@@ -68,7 +68,7 @@ def self.to_mongo(value, place_in_statement)
6868
v, k = value.to_s, :u
6969
end
7070
v = nil if v == ''
71-
71+
7272
case place_in_statement
7373
when :subject
7474
t, k1, lt = :st, :s, :sl
@@ -114,7 +114,7 @@ class Repository < ::RDF::Repository
114114
# The collection used for storing quads
115115
# @return [Mongo::Collection]
116116
attr_reader :collection
117-
117+
118118
##
119119
# Initializes this repository instance.
120120
#
@@ -171,28 +171,21 @@ def initialize(options = {}, &block)
171171
# @see RDF::Mutable#insert_statement
172172
def supports?(feature)
173173
case feature.to_sym
174-
when :graph_name then true
174+
when :graph_name then true
175175
when :validity then @options.fetch(:with_validity, true)
176176
else false
177177
end
178178
end
179-
179+
180180
def insert_statement(statement)
181-
raise ArgumentError, "Statement #{statement.inspect} is incomplete" if statement.incomplete?
182-
st_mongo = statement.to_mongo
183-
st_mongo[:ct] ||= :default # Indicate statement is in the default graph
184-
#puts "insert statement: #{st_mongo.inspect}"
181+
st_mongo = statement_to_insert(statement)
185182
@collection.update_one(st_mongo, st_mongo, upsert: true)
186183
end
187184

188185
# @see RDF::Mutable#delete_statement
189186
def delete_statement(statement)
190-
case statement.graph_name
191-
when nil
192-
@collection.delete_one(statement.to_mongo.merge('ct'=>:default))
193-
else
194-
@collection.delete_one(statement.to_mongo)
195-
end
187+
st_mongo = statement_to_delete(statement)
188+
@collection.delete_one(st_mongo)
196189
end
197190

198191
##
@@ -261,13 +254,27 @@ def query_pattern(pattern, options = {}, &block)
261254
block.call(RDF::Statement.from_mongo(document))
262255
end
263256
end
264-
257+
265258
private
266259

267260
def enumerator! # @private
268261
require 'enumerator' unless defined?(::Enumerable)
269262
@@enumerator_klass = defined?(::Enumerable::Enumerator) ? ::Enumerable::Enumerator : ::Enumerator
270263
end
264+
265+
def statement_to_insert(statement)
266+
raise ArgumentError, "Statement #{statement.inspect} is incomplete" if statement.incomplete?
267+
st_mongo = statement.to_mongo
268+
st_mongo[:ct] ||= :default # Indicate statement is in the default graph
269+
#puts "insert statement: #{st_mongo.inspect}"
270+
st_mongo
271+
end
272+
273+
def statement_to_delete(statement)
274+
st_mongo = statement.to_mongo
275+
st_mongo[:ct] = :default if statement.graph_name.nil?
276+
st_mongo
277+
end
271278
end
272279
end
273280
end

0 commit comments

Comments
 (0)