load_data_infile 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -3,6 +3,7 @@ require 'ostruct'
3
3
 
4
4
  module LoadDataInfile
5
5
  module MySql
6
+ mattr_accessor :load_data_infile_defaults
6
7
 
7
8
  # Deletes all rows in table very fast, but without calling +destroy+ method
8
9
  # nor any hooks.
@@ -29,6 +30,14 @@ module LoadDataInfile
29
30
  end
30
31
 
31
32
  # Load csv from a file using MySql's LOAD DATA INFILE
33
+ # You can set defaults for all these options using the accesor load_data_infile_defaults:
34
+ #
35
+ # class ActiveRecord::Base
36
+ # load_data_infile_defaults = {
37
+ # :ignore => 1
38
+ # }
39
+ # end
40
+ #
32
41
  # For details see: http://dev.mysql.com/doc/refman/5.1/en/load-data.html
33
42
  #
34
43
  # Options:
@@ -51,7 +60,9 @@ module LoadDataInfile
51
60
  # table :: [OPTIONAL] Table name. Defaults to quoted_table_name (won't work if used from an abstract class, e.g. ActiveRecord::Base')
52
61
  # terminated_by :: [OPTIONAL] Character
53
62
  # disable_keys :: [OPTIONAL] true or false. Defaults to true. Disables foreign keys while running the import.
54
- def load_data_infile(options = {})
63
+ def load_data_infile(opt = {})
64
+ options = (load_data_infile_defaults || Hash.new).merge(opt)
65
+
55
66
  disable_keys_option = !options.member?(:disable_keys) || options[:disable_keys]
56
67
 
57
68
  c = Context.new
@@ -52,4 +52,25 @@ describe LoadDataInfile do
52
52
  "field_c" => 24000
53
53
  }]
54
54
  end
55
+
56
+ it "can use defaults" do
57
+ ActiveRecord::Base.load_data_infile_defaults = {
58
+ :terminated_by => ",",
59
+ :columns => %w|id @field_a @field_b @field_c|,
60
+ :mappings => {
61
+ :field_a => "CONCAT('So ', @field_a)",
62
+ :field_b => "CONCAT('Much ', @field_b)",
63
+ :field_c => "@field_c * 10",
64
+ }
65
+ }
66
+
67
+ Thing.load_data_infile(:path => FIXTURE_WITHOUT_HEADERS)
68
+
69
+ Thing.all.map(&:attributes).should == [{
70
+ "id" => 61,
71
+ "field_a" => "So live",
72
+ "field_b" => "Much from",
73
+ "field_c" => 24000
74
+ }]
75
+ end
55
76
  end
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  SPEC_PATH = File.dirname(__FILE__)
2
2
  $LOAD_PATH.unshift(SPEC_PATH)
3
3
  $LOAD_PATH.unshift(File.join(SPEC_PATH, '..', 'lib'))
4
- require 'load_data_infile'
5
4
  require 'spec'
6
5
  require 'spec/autorun'
7
6
  require 'rubygems'
8
7
  require 'active_record'
8
+ require 'load_data_infile'
9
9
  require 'active_record_helper'
10
10
  require File.join(SPEC_PATH, "..", "rails", "init.rb")
11
11
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
7
+ - 4
8
8
  - 0
9
- version: 0.3.0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Emmanuel Oga
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-02-24 00:00:00 -03:00
17
+ date: 2010-02-25 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency