Class: CSV::Parser
- Inherits:
-
Object
- Object
- CSV::Parser
- Defined in:
- lib/csv/parser.rb
Defined Under Namespace
Classes: InputsScanner, InvalidEncoding, Scanner, UnoptimizedStringIO
Instance Method Summary collapse
- #column_separator ⇒ Object
- #field_size_limit ⇒ Object
- #header_row? ⇒ Boolean
- #headers ⇒ Object
-
#initialize(input, options) ⇒ Parser
constructor
A new instance of Parser.
- #liberal_parsing? ⇒ Boolean
- #line ⇒ Object
- #lineno ⇒ Object
- #parse(&block) ⇒ Object
- #quote_character ⇒ Object
- #return_headers? ⇒ Boolean
- #row_separator ⇒ Object
- #skip_blanks? ⇒ Boolean
- #skip_lines ⇒ Object
- #unconverted_fields? ⇒ Boolean
- #use_headers? ⇒ Boolean
Constructor Details
#initialize(input, options) ⇒ Parser
Returns a new instance of Parser.
169 170 171 172 173 174 175 176 |
# File 'lib/csv/parser.rb', line 169 def initialize(input, ) @input = input = @samples = [] @parsed = false prepare end |
Instance Method Details
#column_separator ⇒ Object
178 179 180 |
# File 'lib/csv/parser.rb', line 178 def column_separator @column_separator end |
#field_size_limit ⇒ Object
190 191 192 |
# File 'lib/csv/parser.rb', line 190 def field_size_limit @field_size_limit end |
#header_row? ⇒ Boolean
206 207 208 |
# File 'lib/csv/parser.rb', line 206 def header_row? @use_headers and @headers.nil? end |
#headers ⇒ Object
202 203 204 |
# File 'lib/csv/parser.rb', line 202 def headers @headers end |
#liberal_parsing? ⇒ Boolean
218 219 220 |
# File 'lib/csv/parser.rb', line 218 def liberal_parsing? @liberal_parsing end |
#line ⇒ Object
226 227 228 |
# File 'lib/csv/parser.rb', line 226 def line last_line end |
#lineno ⇒ Object
222 223 224 |
# File 'lib/csv/parser.rb', line 222 def lineno @lineno end |
#parse(&block) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/csv/parser.rb', line 230 def parse(&block) return to_enum(__method__) unless block_given? return if @parsed if @return_headers and @headers headers = Row.new(@headers, @raw_headers, true) if @unconverted_fields headers = add_unconverted_fields(headers, []) end yield headers end row = [] begin @scanner = build_scanner skip_needless_lines start_row while true @quoted_column_value = false @unquoted_column_value = false value = parse_column_value if value and @field_size_limit and value.size >= @field_size_limit raise MalformedCSVError.new("Field size exceeded", @lineno + 1) end if parse_column_end row << value elsif parse_row_end if row.empty? and value.nil? emit_row([], &block) unless @skip_blanks else row << value emit_row(row, &block) row = [] end skip_needless_lines start_row elsif @scanner.eos? break if row.empty? and value.nil? row << value emit_row(row, &block) break else if @quoted_column_value = "Do not allow except col_sep_split_separator " + "after quoted fields" raise MalformedCSVError.new(, @lineno + 1) elsif @unquoted_column_value and @scanner.scan(@cr_or_lf) = "Unquoted fields do not allow \\r or \\n" raise MalformedCSVError.new(, @lineno + 1) elsif @scanner.rest.start_with?(@quote_character) = "Illegal quoting" raise MalformedCSVError.new(, @lineno + 1) else raise MalformedCSVError.new("TODO: Meaningful message", @lineno + 1) end end end rescue InvalidEncoding = "Invalid byte sequence in #{@encoding}" raise MalformedCSVError.new(, @lineno + 1) end @parsed = true end |
#quote_character ⇒ Object
186 187 188 |
# File 'lib/csv/parser.rb', line 186 def quote_character @quote_character end |
#return_headers? ⇒ Boolean
210 211 212 |
# File 'lib/csv/parser.rb', line 210 def return_headers? @return_headers end |
#row_separator ⇒ Object
182 183 184 |
# File 'lib/csv/parser.rb', line 182 def row_separator @row_separator end |
#skip_blanks? ⇒ Boolean
214 215 216 |
# File 'lib/csv/parser.rb', line 214 def skip_blanks? @skip_blanks end |
#skip_lines ⇒ Object
194 195 196 |
# File 'lib/csv/parser.rb', line 194 def skip_lines @skip_lines end |
#unconverted_fields? ⇒ Boolean
198 199 200 |
# File 'lib/csv/parser.rb', line 198 def unconverted_fields? @unconverted_fields end |
#use_headers? ⇒ Boolean
297 298 299 |
# File 'lib/csv/parser.rb', line 297 def use_headers? @use_headers end |