Skip to content

Commit

Permalink
Add Enumerable#canonicaliza method, which can be overridden by rdf-no…
Browse files Browse the repository at this point in the history
…rmalize, which normalizes each statement in an enumerable.
  • Loading branch information
gkellogg committed Aug 19, 2023
1 parent f868294 commit 8898065
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
22 changes: 21 additions & 1 deletion lib/rdf/mixin/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -721,13 +721,33 @@ def enum_graph
end
alias_method :enum_graphs, :enum_graph

##
# Enumerates each statement using its canonical representation.
#
# @note This is updated by `RDF::Normalize` to also canonicalize blank nodes.
#
# @return [RDF::Enumerable]
def canonicalize
this = self
Enumerable::Enumerator.new do |yielder|
this.send(:each_statement) {|y| yielder << y.canonicalize}
end
end

##
# Mutating canonicalization not supported
#
# @raise NotImplementedError
def canonicalize!
raise NotImplementedError, "Canonicalizing enumerables not supported"
end

##
# Returns all RDF statements in `self` as an array.
#
# Mixes in `RDF::Enumerable` into the returned object.
#
# @return [Array]
# @since 0.2.0
def to_a
super.extend(RDF::Enumerable)
end
Expand Down
3 changes: 2 additions & 1 deletion spec/model_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@
end

it "#canonicalize!" do
[statement, uri, node, literal, graph, statement, variable, list].each do |v|
[statement, uri, node, literal, statement, variable, list].each do |v|
expect(v.canonicalize!).to equal v
end
expect {graph.canonicalize!}.to raise_error(NotImplementedError)
end

it "#to_rdf" do
Expand Down

0 comments on commit 8898065

Please sign in to comment.