Module: Import
- Defined in:
- lib/cheap_imports/import.rb
Constant Summary collapse
- DELIMITERS =
Feel free to add extra delimiters to this array.
["\t", '|', ","]
Class Method Summary collapse
- .import(klasses, params_data, args = {}, debug = false) ⇒ Object
- .import_from_file(klasses, filename, debug = false) ⇒ Object
Class Method Details
.import(klasses, params_data, args = {}, debug = false) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cheap_imports/import.rb', line 5 def Import.import(klasses, params_data, args = {}, debug = false) args.merge!({:imported_at => Time.now.strftime("%Y-%m-%d %H:%M:%S")}) unless args.has_key?(:imported_at) klasses = [klasses] unless klasses.is_a?(Array) delim = auto_detect_delimiter(params_data.to_s) header_row = nil objects = [] FCSV.parse(params_data, {:col_sep => delim, :row_sep => :auto}) do |row| next if row.join("") !~ /\w/ # Skip blank rows. header_row ||= row next if header_row.join("") === row.join("") # Skip over duplicate header rows. # Clean up the data, get rid of leading/trailing spaces. row.collect {|str| str.to_s.strip!} h = Hash[*header_row.zip(row).flatten] klasses.each do |c| c.init_recognizable_hashes if c.recognizable_hashes.nil? style = c.recognize_hash_style(h, debug) objects << c.import(h, args) if style end end # Return an array of everything that was imported in this batch. objects end |
.import_from_file(klasses, filename, debug = false) ⇒ Object
34 35 36 37 38 |
# File 'lib/cheap_imports/import.rb', line 34 def Import.import_from_file(klasses, filename, debug = false) args = {} args[:date] = Date.strptime($1) if filename =~ /([0-9]{4}-[0-9]{2}-[0-9]{2})/ import(klasses, File.read(filename), args, debug) end |