Class: Stockboy::Readers::CSV

Inherits:
Stockboy::Reader show all
Defined in:
lib/stockboy/readers/csv.rb

Overview

Parse data from CSV into hashes

All standard ::CSV options are respected and passed through

Options collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ CSV

Initialize a new CSV reader

All stdlib ::CSV options are respected.

Parameters:

  • opts (Hash) (defaults to: {})

See Also:



70
71
72
73
74
75
76
77
# File 'lib/stockboy/readers/csv.rb', line 70

def initialize(opts={}, &block)
  super
  @csv_options = opts.reject {|k,v| !::CSV::DEFAULT_OPTIONS.keys.include?(k) }
  @csv_options[:headers] = @csv_options.fetch(:headers, true)
  @skip_header_rows = opts.fetch(:skip_header_rows, 0)
  @skip_footer_rows = opts.fetch(:skip_footer_rows, 0)
  DSL.new(self).instance_eval(&block) if block_given?
end

Instance Attribute Details

#col_sepString

Returns:

  • (String)


55
56
57
58
59
# File 'lib/stockboy/readers/csv.rb', line 55

::CSV::DEFAULT_OPTIONS.keys.each do |opt|
  dsl_attr opt, attr_accessor: false
  define_method(opt)        { @csv_options[opt] }
  define_method(:"#{opt}=") { |value| @csv_options[opt] = value }
end

#encodingString

Override source file encoding

Returns:

  • (String)


23
# File 'lib/stockboy/readers/csv.rb', line 23

dsl_attr :encoding

#headersArray, String

Returns:

  • (Array, String)


55
56
57
58
59
# File 'lib/stockboy/readers/csv.rb', line 55

::CSV::DEFAULT_OPTIONS.keys.each do |opt|
  dsl_attr opt, attr_accessor: false
  define_method(opt)        { @csv_options[opt] }
  define_method(:"#{opt}=") { |value| @csv_options[opt] = value }
end

#optionsObject (readonly)

Hash of all CSV-specific options



91
92
93
# File 'lib/stockboy/readers/csv.rb', line 91

def options
  @csv_options
end

#quote_charString

Returns:

  • (String)


55
56
57
58
59
# File 'lib/stockboy/readers/csv.rb', line 55

::CSV::DEFAULT_OPTIONS.keys.each do |opt|
  dsl_attr opt, attr_accessor: false
  define_method(opt)        { @csv_options[opt] }
  define_method(:"#{opt}=") { |value| @csv_options[opt] = value }
end

#row_sepString

Returns:

  • (String)


55
56
57
58
59
# File 'lib/stockboy/readers/csv.rb', line 55

::CSV::DEFAULT_OPTIONS.keys.each do |opt|
  dsl_attr opt, attr_accessor: false
  define_method(opt)        { @csv_options[opt] }
  define_method(:"#{opt}=") { |value| @csv_options[opt] = value }
end

Skip number of rows at end of file after data ends

Returns:

  • (Integer)


37
# File 'lib/stockboy/readers/csv.rb', line 37

dsl_attr :skip_footer_rows

#skip_header_rowsInteger

Skip number of rows at start of file before data starts

Returns:

  • (Integer)


30
# File 'lib/stockboy/readers/csv.rb', line 30

dsl_attr :skip_header_rows

Instance Method Details

#parse(data) ⇒ Object



79
80
81
82
83
84
# File 'lib/stockboy/readers/csv.rb', line 79

def parse(data)
  chain = options[:header_converters] || []
  chain << proc{ |h| h.freeze }
  opts = options.merge(header_converters: chain)
  ::CSV.parse(sanitize(data), opts).map(&:to_hash)
end