Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9916588

Browse files
committedApr 14, 2011
update comments and specs for new ONIX::Reader new() arguments
* passing product class as the second argument is now deprecated * the second argument to ONIX::Reader is now an optional options Hash * the product class should now be specified using the :product_class option * this change was motivated by the need for an :encoding option
1 parent 1b8dddb commit 9916588

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed
 

‎lib/onix/reader.rb

+12-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module ONIX
2525
# in a shim that provides simple accessor access to common attributes, pass the
2626
# shim class as a second argument
2727
#
28-
# reader = ONIX::Reader.new("somefile.xml", ONIX::APAProduct)
28+
# reader = ONIX::Reader.new("somefile.xml", :product_class => ONIX::APAProduct)
2929
#
3030
# puts reader.header.inspect
3131
#
@@ -39,7 +39,7 @@ module ONIX
3939
# As well as accessing the file header, there are handful of other read only
4040
# attributes that might be useful
4141
#
42-
# reader = ONIX::Reader.new("somefile.xml", ONIX::APAProduct)
42+
# reader = ONIX::Reader.new("somefile.xml")
4343
#
4444
# puts reader.version
4545
# puts reader.xml_lang
@@ -62,7 +62,7 @@ module ONIX
6262
# If the encoding declaration is missing or wrong and the file isn't UTF-8,
6363
# you can manually set or override it like so:
6464
#
65-
# reader = ONIX::Reader.new("somefile.xml", ONIX::APAProduct, :encoding => "iso-8859-1")
65+
# reader = ONIX::Reader.new("somefile.xml", :encoding => "iso-8859-1")
6666
#
6767
# If the file contains invalid bytes for the source encoding an exception will
6868
# be raised. This isn't ideal, but I'm still looking for ways to make this
@@ -73,18 +73,22 @@ class Reader
7373

7474
attr_reader :header, :release
7575

76-
def initialize(input, product_klass = nil, options = {})
76+
def initialize(input, *args)
77+
opts = args.last.kind_of?(Hash) ? args.pop : {}
78+
if args.size > 0
79+
ActiveSupport::Deprecation.warn("Passing a klass as ONIX::Reader's second argument is deprecated, use the :product_class option instead", caller)
80+
end
81+
@product_klass = opts[:product_class] || args.pop || ::ONIX::Product
82+
7783
if input.kind_of?(String)
7884
@file = File.open(input, "r")
79-
@reader = Nokogiri::XML::Reader(@file, nil, options[:encoding]) { |cfg| cfg.dtdload.noent }
85+
@reader = Nokogiri::XML::Reader(@file, nil, opts[:encoding]) { |cfg| cfg.dtdload.noent }
8086
elsif input.kind_of?(IO)
81-
@reader = Nokogiri::XML::Reader(input, nil, options[:encoding]) { |cfg| cfg.dtdload.noent }
87+
@reader = Nokogiri::XML::Reader(input, nil, opts[:encoding]) { |cfg| cfg.dtdload.noent }
8288
else
8389
raise ArgumentError, "Unable to read from file or IO stream"
8490
end
8591

86-
@product_klass = product_klass || ::ONIX::Product
87-
8892
@release = find_release
8993
@header = find_header
9094

‎spec/reader_spec.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
end
113113

114114
it "should transparently convert an iso-8859-1 file to utf-8 when there's no declaration but the user manually specifies iso-8859-1" do
115-
reader = ONIX::Reader.new(@no_encoding_decl_file, nil, :encoding => "iso-8859-1")
115+
reader = ONIX::Reader.new(@no_encoding_decl_file, :encoding => "iso-8859-1")
116116
reader.each do |product|
117117
if RUBY_VERSION >= "1.9"
118118
utf8 = Encoding.find("utf-8")
@@ -138,4 +138,18 @@
138138

139139
product.contributors[0].person_name_inverted.should eql("Küng, Hans")
140140
end
141+
142+
it "should support returning an APAProduct using deprecated API" do
143+
reader = ONIX::Reader.new(@file1, ONIX::APAProduct)
144+
reader.each do |product|
145+
product.should be_a_kind_of(ONIX::APAProduct)
146+
end
147+
end
148+
149+
it "should support returning an APAProduct using new API" do
150+
reader = ONIX::Reader.new(@file1, :product_class => ONIX::APAProduct)
151+
reader.each do |product|
152+
product.should be_a_kind_of(ONIX::APAProduct)
153+
end
154+
end
141155
end

0 commit comments

Comments
 (0)
Please sign in to comment.