Skip to content

Commit

Permalink
Improved the RDF::Enumerable#dump and RDF::Writer.dump documentation …
Browse files Browse the repository at this point in the history
…a tad (closes #70).
  • Loading branch information
artob committed Dec 28, 2010
1 parent 6f61b83 commit 4741d37
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lib/rdf/mixin/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -607,14 +607,14 @@ def to_hash
# ntriples = enumerable.dump(:ntriples)
#
# @param [Array<Object>] args
# The first element is passed to Writer.for.
# If the last argument is a hash, it is passed to Writer.dump as options
# if the last argument is a hash, it is passed as keyword options to
# {RDF::Writer.dump}.
# @return [String]
# @see RDF::Writer.dump
# @since 0.2.0
def dump(*args)
opts = args.last.is_a?(Hash) ? args.last : {}
RDF::Writer.for(args.first).dump(self, nil, opts)
options = args.last.is_a?(Hash) ? args.pop : {}
RDF::Writer.for(*args).dump(self, nil, options)
end
end # Enumerable
end # RDF
9 changes: 6 additions & 3 deletions lib/rdf/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ class << self
end

##
# @param [Object] data
# @param [IO, File] io
# @param [Hash{Symbol => Object}] options Passed to Writer.new or Writer.buffer
# @param [RDF::Enumerable, #each] data
# the graph or repository to dump
# @param [IO, File] io
# the output stream or file to write to
# @param [Hash{Symbol => Object}] options
# passed to {RDF::Writer.new} or {RDF::Writer.buffer}
# @return [void]
def self.dump(data, io = nil, options = {})
io = File.open(io, 'w') if io.is_a?(String)
Expand Down

4 comments on commit 4741d37

@gkellogg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The potential problem with this is that Writer.for takes just one arg. If you were to call as graph,dump(:rdfxml, :standard_prefixes => true), you'll get an error. I used args.first to prevent this. You can get around this by passing just a hash, used by both Writer.for and Writer.dump, but this is the same with either implementation.

@artob
Copy link
Member Author

@artob artob commented on 4741d37 Dec 28, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I also changed args.last to args.pop, so it's fine for dump(:rdfxml, :standard_prefixes => true).

The reason I prefer this approach is that in the original patch, the first argument and the last argument were getting used, but any in-between were just silently ignored. So it would have been possible to call dump(:rdfxml, :ignored, 1, 2, 3, :ignored, :standard_prefixes => true) without getting an error.

In the reformulated version above, giving too many non-Hash arguments will result in an ArgumentError from Writer.for, as it ought to; i.e., the user in fact is at fault, and hence ought to get an error so as to know to adjust their code. But perhaps I missed something?

@gkellogg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I see that this does, indeed, work just fine. Thanks.

@artob
Copy link
Member Author

@artob artob commented on 4741d37 Dec 28, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested it with various arguments, and thought I had a bug in there for a moment (ignore that if you got the notification for the previous comment, now deleted), but I think it's actually all working as intended.

Please sign in to comment.