ruby-changes:24692
From: jeg2 <ko1@a...>
Date: Tue, 21 Aug 2012 05:52:46 +0900 (JST)
Subject: [ruby-changes:24692] jeg2:r36743 (trunk): * lib/csv.rb: Fixes #161 on github
jeg2 2012-08-21 05:52:36 +0900 (Tue, 21 Aug 2012) New Revision: 36743 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=36743 Log: * lib/csv.rb: Fixes #161 on github * lib/csv.rb: You can now specify a pattern for :skip_lines. Matching lines will not be passed to the CSV parser. * lib/csv.rb: Patch by Christian Schwartz. Modified files: trunk/ChangeLog trunk/lib/csv.rb trunk/test/csv/test_features.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 36742) +++ ChangeLog (revision 36743) @@ -1,7 +1,14 @@ +Tue Aug 21 05:43:00 2012 James Edward Gray II <james@g...> + + * lib/csv.rb: Fixes #161 on github + * lib/csv.rb: You can now specify a pattern for :skip_lines. + Matching lines will not be passed to the CSV parser. + * lib/csv.rb: Patch by Christian Schwartz. + Tue Aug 21 05:25:41 2012 Eric Hodel <drbrain@s...> - * re.c (rb_reg_initialize_m): Forgot to update output for or'd-options - example. + * re.c (rb_reg_initialize_m): Forgot to update output for or'd-options + example. Tue Aug 21 05:18:03 2012 Eric Hodel <drbrain@s...> Index: lib/csv.rb =================================================================== --- lib/csv.rb (revision 36742) +++ lib/csv.rb (revision 36743) @@ -973,6 +973,7 @@ # <b><tt>:header_converters</tt></b>:: +nil+ # <b><tt>:skip_blanks</tt></b>:: +false+ # <b><tt>:force_quotes</tt></b>:: +false+ + # <b><tt>:skip_lines</tt></b>:: +nil+ # DEFAULT_OPTIONS = { col_sep: ",", row_sep: :auto, @@ -984,7 +985,8 @@ return_headers: false, header_converters: nil, skip_blanks: false, - force_quotes: false }.freeze + force_quotes: false, + skip_lines: nil }.freeze # # This method will return a CSV instance, just like CSV::new(), but the @@ -1554,6 +1556,14 @@ # skip over any rows with no content. # <b><tt>:force_quotes</tt></b>:: When set to a +true+ value, CSV will # quote all CSV fields it creates. + # <b><tt>:skip_lines</tt></b>:: When set to an object responding to + # <tt>match</tt>, every line matching + # it is considered a comment and ignored + # during parsing. When set to +nil+ + # no line is considered a comment. + # If the passed object does not respond + # to <tt>match</tt>, <tt>ArgumentError</tt> + # is thrown. # # See CSV::DEFAULT_OPTIONS for the default settings. # @@ -1591,6 +1601,7 @@ init_parsers(options) init_converters(options) init_headers(options) + init_comments(options) options.delete(:encoding) options.delete(:internal_encoding) @@ -1620,6 +1631,10 @@ attr_reader :quote_char # The limit for field size, if any. See CSV::new for details. attr_reader :field_size_limit + + # The regex marking a line as a comment. See CSV::new for details + attr_reader :skip_lines + # # Returns the current list of converters in effect. See CSV::new for details. # Built-in converters will be returned by name, while others will be returned @@ -1873,6 +1888,8 @@ end end + next if @skip_lines and @skip_lines.match parse + parts = parse.split(@col_sep, -1) if parts.empty? if in_extended_col @@ -2189,6 +2206,12 @@ init_converters(options, :header_converters) end + def init_comments(options) + @skip_lines = options.delete(:skip_lines) + if @skip_lines and not @skip_lines.respond_to?(:match) + raise ArgumentError, ":skip_lines has to respond to matches" + end + end # # The actual work method for adding converters, used by both CSV.convert() and # CSV.header_convert(). Index: test/csv/test_features.rb =================================================================== --- test/csv/test_features.rb (revision 36742) +++ test/csv/test_features.rb (revision 36743) @@ -274,4 +274,37 @@ assert(CSV::VERSION.frozen?) assert_match(/\A\d\.\d\.\d\Z/, CSV::VERSION) end + + def test_accepts_comment_skip_lines_option + assert_nothing_raised(ArgumentError) do + CSV.new nil, :skip_lines => /\A\s*#/ + end + end + + def test_accepts_comment_defaults_to_nil + c = CSV.new nil + assert_equal c.skip_lines, nil + end + + class RegexStub + end + + def test_requires_skip_lines_to_call_match + regex_stub = RegexStub.new + assert_raise(ArgumentError) do + CSV.new nil, :skip_lines => regex_stub + end + end + + def test_comment_rows_are_ignored + sample_data = "line,1,a\n#not,a,line\nline,2,b\n #also,no,line" + c = CSV.new sample_data, :skip_lines => /\A\s*#/ + assert_equal c.each.to_a, [["line", "1", "a"], ["line", "2", "b"]] + end + + def test_quoted_skip_line_markers_are_ignored + sample_data = "line,1,a\n\"#not\",a,line\nline,2,b" + c = CSV.new sample_data, :skip_lines => /\A\s*#/ + assert_equal c.each.to_a, [["line", "1", "a"], ["#not", "a", "line"], ["line", "2", "b"]] + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/