Skip to content

Commit

Permalink
Add extra docs for CSV classes (ruby#102)
Browse files Browse the repository at this point in the history
* Add docs for lib/csv/fields_converter.rb

* Add docs lib/csv/parser.rb

* Add docs for lib/csv/writer.rb

* Remove link

* More docs

* typo

* Remove detail

* Add description for CSV::Scanner and CSV::InputsScanner

* Add note

* Add .scan() and .rest() docs

* Update fields_converter.rb

* Update parser.rb

* Update writer.rb
  • Loading branch information
vbrazo authored and kou committed Sep 13, 2019
1 parent ccf47e3 commit 71b072e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/csv/fields_converter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# frozen_string_literal: true

class CSV
# Note: Don't use this class directly. This is an internal class.
class FieldsConverter
include Enumerable
#
# A CSV::FieldsConverter is a data structure for storing the
# fields converter properties to be passed as a parameter
# when parsing a new file (e.g. CSV::Parser.new(@io, parser_options))
#

def initialize(options={})
@converters = []
Expand Down
34 changes: 34 additions & 0 deletions lib/csv/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,31 @@
using CSV::MatchP if CSV.const_defined?(:MatchP)

class CSV
# Note: Don't use this class directly. This is an internal class.
class Parser
#
# A CSV::Parser is m17n aware. The parser works in the Encoding of the IO
# or String object being read from or written to. Your data is never transcoded
# (unless you ask Ruby to transcode it for you) and will literally be parsed in
# the Encoding it is in. Thus CSV will return Arrays or Rows of Strings in the
# Encoding of your data. This is accomplished by transcoding the parser itself
# into your Encoding.
#

# Raised when encoding is invalid.
class InvalidEncoding < StandardError
end

#
# CSV::Scanner receives a CSV output, scans it and return the content.
# It also controls the life cycle of the object with its methods +keep_start+,
# +keep_end+, +keep_back+, +keep_drop+.
#
# Uses StringScanner (the official strscan gem). Strscan provides lexical
# scanning operations on a String. We inherit its object and take advantage
# on the methods. For more information, please visit:
# https://ruby-doc.org/stdlib-2.6.1/libdoc/strscan/rdoc/StringScanner.html
#
class Scanner < StringScanner
alias_method :scan_all, :scan

Expand Down Expand Up @@ -50,6 +71,18 @@ def keep_drop
end
end

#
# CSV::InputsScanner receives IO inputs, encoding and the chunk_size.
# It also controls the life cycle of the object with its methods +keep_start+,
# +keep_end+, +keep_back+, +keep_drop+.
#
# CSV::InputsScanner.scan() tries to match with pattern at the current position.
# If there's a match, the scanner advances the “scan pointer” and returns the matched string.
# Otherwise, the scanner returns nil.
#
# CSV::InputsScanner.rest() returns the “rest” of the string (i.e. everything after the scan pointer).
# If there is no more data (eos? = true), it returns "".
#
class InputsScanner
def initialize(inputs, encoding, chunk_size: 8192)
@inputs = inputs.dup
Expand Down Expand Up @@ -319,6 +352,7 @@ def use_headers?
end

private
# A set of tasks to prepare the file in order to parse it
def prepare
prepare_variable
prepare_quote_character
Expand Down
11 changes: 11 additions & 0 deletions lib/csv/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
using CSV::MatchP if CSV.const_defined?(:MatchP)

class CSV
# Note: Don't use this class directly. This is an internal class.
class Writer
#
# A CSV::Writer receives an output, prepares the header, format and output.
# It allows us to write new rows in the object and rewind it.
#
attr_reader :lineno
attr_reader :headers

Expand All @@ -22,6 +27,9 @@ def initialize(output, options)
@fields_converter = @options[:fields_converter]
end

#
# Adds a new row
#
def <<(row)
case row
when Row
Expand All @@ -47,6 +55,9 @@ def <<(row)
self
end

#
# Winds back to the beginning
#
def rewind
@lineno = 0
@headers = nil if @options[:headers].nil?
Expand Down

0 comments on commit 71b072e

Please sign in to comment.