summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/csv.rb25
1 files changed, 24 insertions, 1 deletions
diff --git a/lib/csv.rb b/lib/csv.rb
index 13f86ec318..45da2c9bd6 100644
--- a/lib/csv.rb
+++ b/lib/csv.rb
@@ -973,6 +973,7 @@ class CSV
# <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 @@ class CSV
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 @@ class CSV
# 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 @@ class CSV
init_parsers(options)
init_converters(options)
init_headers(options)
+ init_comments(options)
options.delete(:encoding)
options.delete(:internal_encoding)
@@ -1620,6 +1631,10 @@ class CSV
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 @@ class CSV
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 @@ class CSV
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().