Module: Sequel::Extensions::FromCsv

Defined in:
lib/sequel/extensions/from_csv.rb

Instance Method Summary collapse

Instance Method Details

#seed_from_csv(directory, **opts) ⇒ Object

Finds all CSV files recursively within a directory and calls #seed_from_csv on their respective models

Calling #seed_from_csv with a directory contaning:

  • artists.csv

  • artist/has_albums.csv

Would be equivalent to calling:

Artist.seed_from_csv "artists.csv"
Artist::HasAlbum.seed_from_csv "artist/has_albums.csv"

Options:

:delete_missing

whether to remove rows from the table that were not found in the CSV file

:reset_sequence

whether to update the primary key’s sequence to reflect the max primary key value

:reset_sequence only works on PostgreSQL tables that auto-increment their primary keys using sequences



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sequel/extensions/from_csv.rb', line 20

def seed_from_csv directory, **opts

  Dir.glob("#{directory}/**/*.csv").each do |filename|

    klass = filename.sub "#{directory}", ""
    klass = klass[1..-1] if klass.starts_with? "/"
    klass = klass.split(".")[0]

    model = klass.classify.constantize
    model.plugin :from_csv
    model.seed_from_csv filename, **opts

  end

  nil

end