ar_csv_importer 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ require 'ar_csv_importer/importer.rb'
2
+
3
+ module ARCSVImporter
4
+ def has_csv_importer(*row_order)
5
+ class_eval do
6
+ cattr_accessor :csv_importer_row_order
7
+ self.csv_importer_row_order = row_order
8
+
9
+ def self.csv_import(file_path)
10
+ importer.import
11
+ end
12
+
13
+ def self.csv_import!(file_path)
14
+ importer.import(true)
15
+ end
16
+
17
+ def self.importer
18
+ @importer ||= ARCSVImporter::Importer.new(file_path, self, self.csv_importer_row_order)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ ActiveRecord::Base.send(:extend, ARCSVImporter)
@@ -0,0 +1,23 @@
1
+ module ARCSVImporter
2
+ class Importer
3
+ attr_accessor :file_path, :import_class, :row_order
4
+
5
+ def initialize(file_path, import_class, *row_order)
6
+ @file_path = file_path
7
+ @import_class = import_class
8
+ @row_order = row_order.flatten
9
+ end
10
+
11
+ def import(raise_validation_errors = false)
12
+ meth = raise_validation_errors ? :create! : :create
13
+ CSV.foreach(file_path) do |row|
14
+ import_class.send(meth, attrs_from_row(row))
15
+ end
16
+ end
17
+
18
+ private
19
+ def attrs_from_row(row)
20
+ Hash[row.each_with_index.map { |r, i| [row_order[i], r] }]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module ARCSVImporter
2
+ VERSION = 0.1
3
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_csv_importer
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sean Roberts
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.4.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.4.5
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.9.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.9.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 3.2.13
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.13
62
+ description: Easily add CSV import tools to any ActiveRecord model.
63
+ email: roberts.sean@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - lib/ar_csv_importer/importer.rb
69
+ - lib/ar_csv_importer.rb
70
+ - lib/version.rb
71
+ homepage: http://github.com/SeanRoberts/ar_csv_importer
72
+ licenses:
73
+ - MIT
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: 1.9.3
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: 1.5.2
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.23
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Easily add CSV import tools to any ActiveRecord model.
96
+ test_files: []