Class: Jcsv::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/reader.rb

Overview

Direct Known Subclasses

ListReader, MapReader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, col_sep: ",", comment_starts: false, comment_matches: false, ignore_empty_lines: true, surrounding_space_need_quotes: false, quote_char: "\"", default_filter: Jcsv.optional, strings_as_keys: false, format: :list, headers: true, custom_headers: nil, chunk_size: 0, deep_map: false, dimensions: nil, suppress_warnings: false) ⇒ Reader


Accepts the following options: and end of the comment, such as <!.*!>, comments everyting between <! and !> such as the delimiter char, a quote char, or spans multiple lines). order to be preserved. The default value is false (quotes aren’t required). are ignored. The default value is true (empty lines are ignored). first dimension, that has all rows with this dimension. If there is a second dimension, then this is also hashed across all rows, etc.


Parameters:

  • comment_starts: (defaults to: false)

    character at the beginning of the line that marks a comment

  • comment_matches: (defaults to: false)

    delimiters that match a comment, needs to comment at the beginning

  • quote_char (defaults to: "\"")

    The quote character (used when a cell contains special characters,

  • col_sep (defaults to: ",")

    the delimiter character (separates each cell in a row).

  • surrounding_spaces_need_quotes

    Whether spaces surrounding a cell need quotes in

  • ignore_empty_lines (defaults to: true)

    Whether empty lines (i.e. containing only end of line symbols)

  • format (defaults to: :list)

    Format of result, list, map, vector.

  • deep

    When true reads data as a deep map (hash), i.e., there is a hash of the



229
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
# File 'lib/reader.rb', line 229

def initialize(filename,
               col_sep: ",",
               comment_starts: false,
               comment_matches: false,
               ignore_empty_lines: true,
               surrounding_space_need_quotes: false,
               quote_char: "\"",
               default_filter: Jcsv.optional,
               strings_as_keys: false,
               format: :list,
               headers: true,
               custom_headers: nil,
               chunk_size: 0,
               deep_map: false,
               dimensions: nil,
               suppress_warnings: false)
  
  @filename = filename
  @col_sep = col_sep
  @comment_starts = comment_starts
  @comment_matches = comment_matches
  @default_filter = default_filter
  @filters = false
  @strings_as_keys = strings_as_keys
  @headers = headers
  @custom_headers = custom_headers
  @ignore_empty_lines = ignore_empty_lines
  @format = format
  @surrounding_space_need_quotes = surrounding_space_need_quotes
  @quote_char = quote_char
  @chunk_size = (chunk_size == :all)? 1.0/0.0 : chunk_size
  @deep_map = (@format == :list)? false : deep_map
  @dimensions_names = dimensions
  @column_mapping = Mapping.new
  @suppress_warnings = suppress_warnings
  
  prepare_dimensions if dimensions

  # set all preferences.  To create a new reader we need to have the dimensions already
  # prepared as this information will be sent to supercsv for processing.
  new_reader(set_preferences)

  # Dynamic class change without writing subclasses. When headers, extend this class
  # with methods that assume there is a header, when no headers, then extend this class
  # with methods that know there is no header.  Could have being done with subclasses,
  # but this would all subclasses to have two subclasses one inheriting from the header
  # class and one inheriting from the headerless classes.  In this way we reduce the
  # subclasses need.
  @headers? prepare_headers : (@custom_headers? set_headers(@custom_headers) :
                                 headerless)

  # if there are dimensions, then we need to prepare the mappings accordingly.  With
  # dimensions defined, users cannot defined mappings.
  dimensions_mappings if dimensions
        
end

Instance Attribute Details

#chunk_sizeObject

chunk_size can be changed on the fly



181
182
183
# File 'lib/reader.rb', line 181

def chunk_size
  @chunk_size
end

#col_sepObject (readonly)

Returns the value of attribute col_sep.



170
171
172
# File 'lib/reader.rb', line 170

def col_sep
  @col_sep
end

#column_mappingObject (readonly)

Returns the value of attribute column_mapping.



185
186
187
# File 'lib/reader.rb', line 185

def column_mapping
  @column_mapping
end

#comment_matchesObject (readonly)

Returns the value of attribute comment_matches.



172
173
174
# File 'lib/reader.rb', line 172

def comment_matches
  @comment_matches
end

#comment_startsObject (readonly)

Returns the value of attribute comment_starts.



171
172
173
# File 'lib/reader.rb', line 171

def comment_starts
  @comment_starts
end

#data_labelsObject (readonly)

Returns the value of attribute data_labels.



184
185
186
# File 'lib/reader.rb', line 184

def data_labels
  @data_labels
end

#dimensions_namesObject (readonly)

Returns the value of attribute dimensions_names.



186
187
188
# File 'lib/reader.rb', line 186

def dimensions_names
  @dimensions_names
end

#filenameObject (readonly)

Reader configuration parameters



169
170
171
# File 'lib/reader.rb', line 169

def filename
  @filename
end

#formatObject (readonly)

output format: list, map, vector, others…



177
178
179
# File 'lib/reader.rb', line 177

def format
  @format
end

#headersObject (readonly)

Returns the value of attribute headers.



183
184
185
# File 'lib/reader.rb', line 183

def headers
  @headers
end

#ignore_empty_linesObject (readonly)

Returns the value of attribute ignore_empty_lines.



173
174
175
# File 'lib/reader.rb', line 173

def ignore_empty_lines
  @ignore_empty_lines
end

#processed_columnObject (readonly)

last processed column



189
190
191
# File 'lib/reader.rb', line 189

def processed_column
  @processed_column
end

#quote_charObject (readonly)

Returns the value of attribute quote_char.



175
176
177
# File 'lib/reader.rb', line 175

def quote_char
  @quote_char
end

#rowsObject (readonly)

Rows read. Returned when reading a chunk of data



192
193
194
# File 'lib/reader.rb', line 192

def rows
  @rows
end

#strings_as_keysObject (readonly)

Returns the value of attribute strings_as_keys.



176
177
178
# File 'lib/reader.rb', line 176

def strings_as_keys
  @strings_as_keys
end

#suppress_warningsObject (readonly)

true if no warning messages should be shown



178
179
180
# File 'lib/reader.rb', line 178

def suppress_warnings
  @suppress_warnings
end

#surrounding_space_need_quotesObject (readonly)

Returns the value of attribute surrounding_space_need_quotes.



174
175
176
# File 'lib/reader.rb', line 174

def surrounding_space_need_quotes
  @surrounding_space_need_quotes
end

Instance Method Details

#[](dim) ⇒ Object





198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/reader.rb', line 198

def [](dim)

  case true
  when (dim == :_data_)
    @data_labels
  when (@dimensions_names.include? dim)
    @dimensions.dimensions[dim].labels.keys
  else
    raise ArgumentError.new("Unknown dimension #{dim}")
  end
  
end

#dimensionsObject





334
335
336
# File 'lib/reader.rb', line 334

def dimensions
  @reader.dimensions
end

#each(&block) ⇒ Object





310
311
312
313
314
315
316
317
318
# File 'lib/reader.rb', line 310

def each(&block)
  
  if (!block_given?)
    to_enum
  else
    parse_with_block(&block)
  end
  
end

#mapping=(map, dim_set = false) ⇒ Object


Both map_reader and list_reader have a mapping= method. Is this really necessary? FIX!!!!




325
326
327
328
# File 'lib/reader.rb', line 325

def mapping=(map, dim_set = false)
  p "reader.rb mapping =.  FIX!"
  @column_mapping.map = map
end