importer 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +28 -0
- data/VERSION +1 -1
- data/importer.gemspec +3 -3
- data/lib/importer/adapters/active_record_adapter.rb +2 -1
- data/lib/importer/adapters/mongo_mapper_adapter.rb +2 -1
- data/lib/importer/import.rb +4 -3
- data/test/importer/adapters/active_record_adapter_test.rb +13 -0
- data/test/importer/adapters/mongo_mapper_adapter_test.rb +13 -0
- metadata +54 -29
data/README.rdoc
CHANGED
@@ -25,10 +25,17 @@ Or the plugin
|
|
25
25
|
|
26
26
|
Add to your model
|
27
27
|
|
28
|
+
# ActiveRecord
|
28
29
|
class Product < ActiveRecord::Base
|
29
30
|
include Importer
|
30
31
|
end
|
31
32
|
|
33
|
+
# MongoMapper
|
34
|
+
class Product
|
35
|
+
include MongoMapper::Document
|
36
|
+
include Importer
|
37
|
+
end
|
38
|
+
|
32
39
|
And start importing
|
33
40
|
|
34
41
|
Product.import(path_to_xml_or_csv_file)
|
@@ -45,6 +52,27 @@ You can also create your custom versions of Import and ImportedObject classes. A
|
|
45
52
|
|
46
53
|
Product.import(path_to_file, :import => CustomImportClass)
|
47
54
|
|
55
|
+
== Updating objects
|
56
|
+
|
57
|
+
Importer is smart enough to figure out whether it has to create a new object or just update existing one during import. By default it's tries to find existing object by detected id attribute. If the object is found Importer updates it, otherwise a new object is created. You can change the way how existing objects are searched for by overriding your model's +find_on_import+ class method.
|
58
|
+
|
59
|
+
class Product < ActiveRecord::Base
|
60
|
+
include Importer
|
61
|
+
|
62
|
+
def find_on_import(import, attributes)
|
63
|
+
find_by_name(attributes["custom_attribute"])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
== Building objects
|
68
|
+
|
69
|
+
The default way to build new objects or update existing ones is to merge their attributes with detected ones. To provide custom building logic override your model's +merge_attributes_on_import+ instance method.
|
70
|
+
|
71
|
+
def merge_attributes_on_import(import, attributes)
|
72
|
+
self.attributes = attributes
|
73
|
+
self.imported_at = Time.now
|
74
|
+
end
|
75
|
+
|
48
76
|
== Note on Patches/Pull Requests
|
49
77
|
|
50
78
|
* Fork the project.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
data/importer.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{importer}
|
8
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Micha\305\202 Szajbe"]
|
12
|
-
s.date = %q{2010-04-
|
12
|
+
s.date = %q{2010-04-16}
|
13
13
|
s.description = %q{Define new objects or modifications of existing ones in external file (xml, csv, etc) and import them to your application. Importer will not only import all the objects but also will give you detailed summary of the import process.}
|
14
14
|
s.email = %q{michal.szajbe@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -54,7 +54,7 @@ Gem::Specification.new do |s|
|
|
54
54
|
s.homepage = %q{http://github.com/szajbus/importer}
|
55
55
|
s.rdoc_options = ["--charset=UTF-8"]
|
56
56
|
s.require_paths = ["lib"]
|
57
|
-
s.rubygems_version = %q{1.3.
|
57
|
+
s.rubygems_version = %q{1.3.6}
|
58
58
|
s.summary = %q{Import objects from external files}
|
59
59
|
s.test_files = [
|
60
60
|
"test/factories.rb",
|
@@ -42,8 +42,9 @@ module Importer
|
|
42
42
|
# * +import+ - by default importer returns instance of +Import+ class that contains
|
43
43
|
# detailed report of import process, you can implement your own Import class and force
|
44
44
|
# the importer to use it by passing it's class here
|
45
|
+
# * +import_options+ - options passed to Import instance on it's initialization
|
45
46
|
def import(file, options = {})
|
46
|
-
import = (options[:import] || Importer::Import).new
|
47
|
+
import = (options[:import] || Importer::Import).new(options[:import_options])
|
47
48
|
parser = options[:parser] || Importer::Parser.get_klass(file)
|
48
49
|
data = parser.run(file)
|
49
50
|
|
@@ -46,8 +46,9 @@ module Importer
|
|
46
46
|
# * +import+ - by default importer returns instance of +Import+ class that contains
|
47
47
|
# detailed report of import process, you can implement your own Import class and force
|
48
48
|
# the importer to use it by passing it's class here
|
49
|
+
# * +import_options+ - options passed to Import instance on it's initialization
|
49
50
|
def import(file, options = {})
|
50
|
-
import = (options[:import] || Importer::Import).new
|
51
|
+
import = (options[:import] || Importer::Import).new(options[:import_options])
|
51
52
|
parser = options[:parser] || Importer::Parser.get_klass(file)
|
52
53
|
data = parser.run(file)
|
53
54
|
|
data/lib/importer/import.rb
CHANGED
@@ -14,10 +14,11 @@ module Importer
|
|
14
14
|
# Just be sure to implement +add_object+ and +build_imported_object+
|
15
15
|
# methods in your custom class.
|
16
16
|
class Import
|
17
|
-
attr_reader :imported_objects
|
17
|
+
attr_reader :options, :imported_objects
|
18
18
|
|
19
|
-
def initialize
|
19
|
+
def initialize(options = nil)
|
20
20
|
@imported_objects = []
|
21
|
+
@options = options
|
21
22
|
end
|
22
23
|
|
23
24
|
def add_object(imported_object)
|
@@ -40,4 +41,4 @@ module Importer
|
|
40
41
|
imported_objects.select { |object| object.state == 'invalid_object' }
|
41
42
|
end
|
42
43
|
end
|
43
|
-
end
|
44
|
+
end
|
@@ -111,4 +111,17 @@ class Importer::Adapters::ActiveRecordAdapterTest < Test::Unit::TestCase
|
|
111
111
|
end
|
112
112
|
end
|
113
113
|
end
|
114
|
+
|
115
|
+
context "passing import_options to #import method" do
|
116
|
+
setup do
|
117
|
+
stub(Importer::Parser::Xml).run(fixture_file("empty.xml")) { [] }
|
118
|
+
|
119
|
+
@options = { :key => 'value' }
|
120
|
+
@import = Product.import(fixture_file("empty.xml"), :import_options => @options)
|
121
|
+
end
|
122
|
+
|
123
|
+
should "pass the options to Import instance" do
|
124
|
+
assert_equal @options, @import.options
|
125
|
+
end
|
126
|
+
end
|
114
127
|
end
|
@@ -112,4 +112,17 @@ class Importer::Adapters::MongoMapperAdapterTest < Test::Unit::TestCase
|
|
112
112
|
end
|
113
113
|
end
|
114
114
|
end
|
115
|
+
|
116
|
+
context "passing import_options to #import method" do
|
117
|
+
setup do
|
118
|
+
stub(Importer::Parser::Xml).run("empty.xml") { [] }
|
119
|
+
|
120
|
+
@options = { :key => 'value' }
|
121
|
+
@import = Product.import(fixture_file("empty.xml"), :import_options => @options)
|
122
|
+
end
|
123
|
+
|
124
|
+
should "pass the options to Import instance" do
|
125
|
+
assert_equal @options, @import.options
|
126
|
+
end
|
127
|
+
end
|
115
128
|
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: importer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 4
|
8
|
+
- 1
|
9
|
+
version: 0.4.1
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- "Micha\xC5\x82 Szajbe"
|
@@ -9,69 +14,87 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-04-
|
17
|
+
date: 2010-04-16 00:00:00 +02:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: crack
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 1
|
30
|
+
- 6
|
23
31
|
version: 0.1.6
|
24
|
-
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
25
34
|
- !ruby/object:Gem::Dependency
|
26
35
|
name: fastercsv
|
27
|
-
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
38
|
requirements:
|
31
39
|
- - ">="
|
32
40
|
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 1
|
43
|
+
- 5
|
44
|
+
- 0
|
33
45
|
version: 1.5.0
|
34
|
-
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
35
48
|
- !ruby/object:Gem::Dependency
|
36
49
|
name: activerecord
|
37
|
-
|
38
|
-
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
40
52
|
requirements:
|
41
53
|
- - ">="
|
42
54
|
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
43
57
|
version: "0"
|
44
|
-
|
58
|
+
type: :development
|
59
|
+
version_requirements: *id003
|
45
60
|
- !ruby/object:Gem::Dependency
|
46
61
|
name: mongo_mapper
|
47
|
-
|
48
|
-
|
49
|
-
version_requirements: !ruby/object:Gem::Requirement
|
62
|
+
prerelease: false
|
63
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
50
64
|
requirements:
|
51
65
|
- - ">="
|
52
66
|
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
- 7
|
70
|
+
- 0
|
53
71
|
version: 0.7.0
|
54
|
-
|
72
|
+
type: :development
|
73
|
+
version_requirements: *id004
|
55
74
|
- !ruby/object:Gem::Dependency
|
56
75
|
name: thoughtbot-shoulda
|
57
|
-
|
58
|
-
|
59
|
-
version_requirements: !ruby/object:Gem::Requirement
|
76
|
+
prerelease: false
|
77
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
60
78
|
requirements:
|
61
79
|
- - ">="
|
62
80
|
- !ruby/object:Gem::Version
|
81
|
+
segments:
|
82
|
+
- 0
|
63
83
|
version: "0"
|
64
|
-
|
84
|
+
type: :development
|
85
|
+
version_requirements: *id005
|
65
86
|
- !ruby/object:Gem::Dependency
|
66
87
|
name: sqlite3-ruby
|
67
|
-
|
68
|
-
|
69
|
-
version_requirements: !ruby/object:Gem::Requirement
|
88
|
+
prerelease: false
|
89
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
70
90
|
requirements:
|
71
91
|
- - ">="
|
72
92
|
- !ruby/object:Gem::Version
|
93
|
+
segments:
|
94
|
+
- 0
|
73
95
|
version: "0"
|
74
|
-
|
96
|
+
type: :development
|
97
|
+
version_requirements: *id006
|
75
98
|
description: Define new objects or modifications of existing ones in external file (xml, csv, etc) and import them to your application. Importer will not only import all the objects but also will give you detailed summary of the import process.
|
76
99
|
email: michal.szajbe@gmail.com
|
77
100
|
executables: []
|
@@ -128,18 +151,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
128
151
|
requirements:
|
129
152
|
- - ">="
|
130
153
|
- !ruby/object:Gem::Version
|
154
|
+
segments:
|
155
|
+
- 0
|
131
156
|
version: "0"
|
132
|
-
version:
|
133
157
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
158
|
requirements:
|
135
159
|
- - ">="
|
136
160
|
- !ruby/object:Gem::Version
|
161
|
+
segments:
|
162
|
+
- 0
|
137
163
|
version: "0"
|
138
|
-
version:
|
139
164
|
requirements: []
|
140
165
|
|
141
166
|
rubyforge_project:
|
142
|
-
rubygems_version: 1.3.
|
167
|
+
rubygems_version: 1.3.6
|
143
168
|
signing_key:
|
144
169
|
specification_version: 3
|
145
170
|
summary: Import objects from external files
|