Skip to content

Commit 8898065

Browse files
committed
Add Enumerable#canonicaliza method, which can be overridden by rdf-normalize, which normalizes each statement in an enumerable.
1 parent f868294 commit 8898065

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

lib/rdf/mixin/enumerable.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,13 +721,33 @@ def enum_graph
721721
end
722722
alias_method :enum_graphs, :enum_graph
723723

724+
##
725+
# Enumerates each statement using its canonical representation.
726+
#
727+
# @note This is updated by `RDF::Normalize` to also canonicalize blank nodes.
728+
#
729+
# @return [RDF::Enumerable]
730+
def canonicalize
731+
this = self
732+
Enumerable::Enumerator.new do |yielder|
733+
this.send(:each_statement) {|y| yielder << y.canonicalize}
734+
end
735+
end
736+
737+
##
738+
# Mutating canonicalization not supported
739+
#
740+
# @raise NotImplementedError
741+
def canonicalize!
742+
raise NotImplementedError, "Canonicalizing enumerables not supported"
743+
end
744+
724745
##
725746
# Returns all RDF statements in `self` as an array.
726747
#
727748
# Mixes in `RDF::Enumerable` into the returned object.
728749
#
729750
# @return [Array]
730-
# @since 0.2.0
731751
def to_a
732752
super.extend(RDF::Enumerable)
733753
end

spec/model_value_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@
135135
end
136136

137137
it "#canonicalize!" do
138-
[statement, uri, node, literal, graph, statement, variable, list].each do |v|
138+
[statement, uri, node, literal, statement, variable, list].each do |v|
139139
expect(v.canonicalize!).to equal v
140140
end
141+
expect {graph.canonicalize!}.to raise_error(NotImplementedError)
141142
end
142143

143144
it "#to_rdf" do

0 commit comments

Comments
 (0)