Class: Stockboy::Readers::CSV
- Inherits:
-
Stockboy::Reader
- Object
- Stockboy::Reader
- Stockboy::Readers::CSV
- 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
- #col_sep ⇒ String
-
#encoding ⇒ String
Override source file encoding.
- #headers ⇒ Array, String
- #quote_char ⇒ String
- #row_sep ⇒ String
-
#skip_footer_rows ⇒ Integer
Skip number of rows at end of file after data ends.
-
#skip_header_rows ⇒ Integer
Skip number of rows at start of file before data starts.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Hash of all CSV-specific options.
Instance Method Summary collapse
-
#initialize(opts = {}, &block) ⇒ CSV
constructor
Initialize a new CSV reader.
- #parse(data) ⇒ Object
Constructor Details
#initialize(opts = {}, &block) ⇒ CSV
Initialize a new CSV reader
All stdlib ::CSV options are respected.
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_sep ⇒ 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 |
#encoding ⇒ String
Override source file encoding
23 |
# File 'lib/stockboy/readers/csv.rb', line 23 dsl_attr :encoding |
#headers ⇒ 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 |
#options ⇒ Object (readonly)
Hash of all CSV-specific options
91 92 93 |
# File 'lib/stockboy/readers/csv.rb', line 91 def @csv_options end |
#quote_char ⇒ 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_sep ⇒ 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_footer_rows ⇒ Integer
Skip number of rows at end of file after data ends
37 |
# File 'lib/stockboy/readers/csv.rb', line 37 dsl_attr :skip_footer_rows |
#skip_header_rows ⇒ Integer
Skip number of rows at start of file before data starts
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 = [:header_converters] || [] chain << proc{ |h| h.freeze } opts = .merge(header_converters: chain) ::CSV.parse(sanitize(data), opts).map(&:to_hash) end |