lib/dbx.rb
# Read source as CSV and detect types based on `sample_rows`
# Types returns match with ActiveRecord column types.
# Types are memory cached by `src`.
#
# @return [Hash<String, Symbol>] column name to type symbols
Read source as CSV and detect types based on `sample_rows`
Types returns match with ActiveRecord column types.
Types are memory cached by `src`.
Read source as CSV and detect types based on ` sample_rows ` Types returns match with ActiveRecord column types . Types are memory cached by ` src ` .
def column_types(src, sample_rows: config_sample_rows, csv_options: {})
    headers = nil
    count = 0
    csv_options[:headers] = false
    @types ||= {}
    types = @types[src]
    return types if types
    types = []
    CSV.foreach(src, **csv_options) do |row|
      unless headers
        headers = row.map(&:downcase)
        next
      end
      headers.each_with_index do |header, i|
        next if types[i] == :string
        pattern_type = config['column_patterns'].detect{ |pat, _| header =~ /#{pat}/ }
        if pattern_type
          types[i] = pattern_type.last
          next
        end
        type = detect_type(row[i])
        next if type.nil?
        if types[i] == :decimal && type == :integer
        elsif types[i] == :datetime && type == :date
        else
          types[i] = type
        end
      end
      break if (count += 1) > sample_rows
    end
    types.size.times{|i| types[i] ||= :string }
    @types[src] = Hash[headers.zip(types)]
  end
def column_types ( src , sample_rows : config_sample_rows , csv_options : { } ) headers = nil count = 0 csv_options [ :headers ] = false @types ||= { } types = @types [ src ] return types if types types = [ ] CSV . foreach ( src , ** csv_options ) do | row | unless headers headers = row . map ( & :downcase ) next end headers . each_with_index do | header , i | next if types [ i ] == :string pattern_type = config [ 'column_patterns' ] . detect { | pat , _ | header =~ / #{ pat } / } if pattern_type types [ i ] = pattern_type . last next end type = detect_type ( row [ i ] ) next if type . nil? if types [ i ] == :decimal && type == :integer elsif types [ i ] == :datetime && type == :date else types [ i ] = type end end break if ( count += 1 ) > sample_rows end types . size . times { | i | types [ i ] ||= :string } @types [ src ] = Hash [ headers . zip ( types ) ] end
Read source as CSV and detect types based on `sample_rows`
Types returns match with ActiveRecord column types.
Read source as CSV and detect types based on ` sample_rows ` Types returns match with ActiveRecord column types .
# keep decimal # keep datetime # assign the new type # stop after max rows reached # any remaining nil types are assigned as :string
column name to type symbols
column name to type symbols
Hash<String, Symbol>
src
<not_specific>
sample_rows
<not_specific>
config_sample_rows
<not_specific>
csv_options
<not_specific>