importer 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +10 -14
- data/Rakefile +6 -8
- data/VERSION +1 -1
- data/importer.gemspec +26 -39
- data/lib/importer.rb +11 -70
- data/lib/importer/adapters/active_record_adapter.rb +102 -0
- data/lib/importer/adapters/mongo_mapper_adapter.rb +104 -0
- data/lib/importer/import.rb +39 -1
- data/lib/importer/imported_object.rb +29 -2
- data/lib/importer/parser/base.rb +4 -0
- data/lib/importer/parser/csv.rb +2 -2
- data/lib/importer/parser/xml.rb +2 -2
- data/test/factories.rb +0 -9
- data/test/helper.rb +14 -48
- data/test/importer/adapters/active_record_adapter_test.rb +114 -0
- data/test/importer/adapters/mongo_mapper_adapter_test.rb +115 -0
- data/test/importer/import_test.rb +45 -0
- data/test/importer/imported_object_test.rb +18 -0
- metadata +20 -47
- data/lib/importer/import/active_record.rb +0 -41
- data/lib/importer/import/simple.rb +0 -61
- data/lib/importer/imported_object/active_record.rb +0 -41
- data/lib/importer/imported_object/simple.rb +0 -25
- data/rails_generators/importer/importer_generator.rb +0 -18
- data/rails_generators/importer/templates/imported_objects_migration.rb +0 -19
- data/rails_generators/importer/templates/imports_migration.rb +0 -17
- data/test/importer/import/active_record_test.rb +0 -36
- data/test/importer/import/simple_test.rb +0 -36
- data/test/importer/imported_object/active_record_test.rb +0 -37
- data/test/importer/imported_object/simple_test.rb +0 -35
- data/test/importer_test.rb +0 -50
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class Importer::ImportedObjectTest < Test::Unit::TestCase
|
4
|
+
context "creating a new imported object" do
|
5
|
+
setup do
|
6
|
+
@data = { 'a' => 'b' }
|
7
|
+
@validation_errors = { 'c' => 'd' }
|
8
|
+
@imported_object = Importer::ImportedObject.new(:state => 'existing_object', :object => 'DummyObject', :data => @data, :validation_errors => @validation_errors)
|
9
|
+
end
|
10
|
+
|
11
|
+
should "correctly set it's attributes" do
|
12
|
+
assert_equal 'existing_object', @imported_object.state
|
13
|
+
assert_equal 'DummyObject', @imported_object.object
|
14
|
+
assert_equal @data, @imported_object.data
|
15
|
+
assert_equal @validation_errors, @imported_object.validation_errors
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: importer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Micha\xC5\x82 Szajbe"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-04-07 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
23
|
+
version: 0.1.6
|
24
24
|
version:
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: fastercsv
|
@@ -30,31 +30,11 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 1.5.0
|
34
34
|
version:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: activerecord
|
37
|
-
type: :
|
38
|
-
version_requirement:
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
40
|
-
requirements:
|
41
|
-
- - ">="
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: "0"
|
44
|
-
version:
|
45
|
-
- !ruby/object:Gem::Dependency
|
46
|
-
name: activesupport
|
47
|
-
type: :runtime
|
48
|
-
version_requirement:
|
49
|
-
version_requirements: !ruby/object:Gem::Requirement
|
50
|
-
requirements:
|
51
|
-
- - ">="
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: "0"
|
54
|
-
version:
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: workflow
|
57
|
-
type: :runtime
|
37
|
+
type: :development
|
58
38
|
version_requirement:
|
59
39
|
version_requirements: !ruby/object:Gem::Requirement
|
60
40
|
requirements:
|
@@ -63,17 +43,17 @@ dependencies:
|
|
63
43
|
version: "0"
|
64
44
|
version:
|
65
45
|
- !ruby/object:Gem::Dependency
|
66
|
-
name:
|
46
|
+
name: mongo_mapper
|
67
47
|
type: :development
|
68
48
|
version_requirement:
|
69
49
|
version_requirements: !ruby/object:Gem::Requirement
|
70
50
|
requirements:
|
71
51
|
- - ">="
|
72
52
|
- !ruby/object:Gem::Version
|
73
|
-
version:
|
53
|
+
version: 0.7.0
|
74
54
|
version:
|
75
55
|
- !ruby/object:Gem::Dependency
|
76
|
-
name:
|
56
|
+
name: thoughtbot-shoulda
|
77
57
|
type: :development
|
78
58
|
version_requirement:
|
79
59
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -92,7 +72,7 @@ dependencies:
|
|
92
72
|
- !ruby/object:Gem::Version
|
93
73
|
version: "0"
|
94
74
|
version:
|
95
|
-
description: Define new objects or modifications of existing ones in
|
75
|
+
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.
|
96
76
|
email: michal.szajbe@gmail.com
|
97
77
|
executables: []
|
98
78
|
|
@@ -110,20 +90,15 @@ files:
|
|
110
90
|
- VERSION
|
111
91
|
- importer.gemspec
|
112
92
|
- lib/importer.rb
|
93
|
+
- lib/importer/adapters/active_record_adapter.rb
|
94
|
+
- lib/importer/adapters/mongo_mapper_adapter.rb
|
113
95
|
- lib/importer/import.rb
|
114
|
-
- lib/importer/import/active_record.rb
|
115
|
-
- lib/importer/import/simple.rb
|
116
96
|
- lib/importer/imported_object.rb
|
117
|
-
- lib/importer/imported_object/active_record.rb
|
118
|
-
- lib/importer/imported_object/simple.rb
|
119
97
|
- lib/importer/parser.rb
|
120
98
|
- lib/importer/parser/base.rb
|
121
99
|
- lib/importer/parser/csv.rb
|
122
100
|
- lib/importer/parser/xml.rb
|
123
101
|
- rails/init.rb
|
124
|
-
- rails_generators/importer/importer_generator.rb
|
125
|
-
- rails_generators/importer/templates/imported_objects_migration.rb
|
126
|
-
- rails_generators/importer/templates/imports_migration.rb
|
127
102
|
- test/database.yml
|
128
103
|
- test/factories.rb
|
129
104
|
- test/fixtures/empty.csv
|
@@ -133,14 +108,13 @@ files:
|
|
133
108
|
- test/fixtures/products.csv
|
134
109
|
- test/fixtures/products.xml
|
135
110
|
- test/helper.rb
|
136
|
-
- test/importer/
|
137
|
-
- test/importer/
|
138
|
-
- test/importer/
|
139
|
-
- test/importer/
|
111
|
+
- test/importer/adapters/active_record_adapter_test.rb
|
112
|
+
- test/importer/adapters/mongo_mapper_adapter_test.rb
|
113
|
+
- test/importer/import_test.rb
|
114
|
+
- test/importer/imported_object_test.rb
|
140
115
|
- test/importer/parser/csv_test.rb
|
141
116
|
- test/importer/parser/xml_test.rb
|
142
117
|
- test/importer/parser_test.rb
|
143
|
-
- test/importer_test.rb
|
144
118
|
has_rdoc: true
|
145
119
|
homepage: http://github.com/szajbus/importer
|
146
120
|
licenses: []
|
@@ -168,15 +142,14 @@ rubyforge_project:
|
|
168
142
|
rubygems_version: 1.3.5
|
169
143
|
signing_key:
|
170
144
|
specification_version: 3
|
171
|
-
summary: Import objects from
|
145
|
+
summary: Import objects from external files
|
172
146
|
test_files:
|
173
147
|
- test/factories.rb
|
174
148
|
- test/helper.rb
|
175
|
-
- test/importer/
|
176
|
-
- test/importer/
|
177
|
-
- test/importer/
|
178
|
-
- test/importer/
|
149
|
+
- test/importer/adapters/active_record_adapter_test.rb
|
150
|
+
- test/importer/adapters/mongo_mapper_adapter_test.rb
|
151
|
+
- test/importer/import_test.rb
|
152
|
+
- test/importer/imported_object_test.rb
|
179
153
|
- test/importer/parser/csv_test.rb
|
180
154
|
- test/importer/parser/xml_test.rb
|
181
155
|
- test/importer/parser_test.rb
|
182
|
-
- test/importer_test.rb
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
require 'workflow'
|
3
|
-
|
4
|
-
module Importer
|
5
|
-
module Import
|
6
|
-
# ActiveRecord model that stores import summaries in imports database table.
|
7
|
-
# has_many :+imported_objects+
|
8
|
-
#
|
9
|
-
# Attributes:
|
10
|
-
# * +new_objects_count+ - number of new objects created during the import
|
11
|
-
# * +existing_objects_count+ - number of objects modified during the import
|
12
|
-
# * +invalid_objects_count+ - number of objects that couldn't have been imported
|
13
|
-
# * +workflow_state+ - import may be in one of three states: ready, started or
|
14
|
-
# finished. The state changes during the import process.
|
15
|
-
class ActiveRecord < ::ActiveRecord::Base
|
16
|
-
set_table_name "imports"
|
17
|
-
|
18
|
-
include Workflow
|
19
|
-
|
20
|
-
workflow do
|
21
|
-
state :ready do
|
22
|
-
event :start, :transitions_to => :started
|
23
|
-
end
|
24
|
-
state :started do
|
25
|
-
event :finish, :transitions_to => :finished
|
26
|
-
end
|
27
|
-
state :finished
|
28
|
-
end
|
29
|
-
|
30
|
-
named_scope :ready, :conditions => { :workflow_state => "ready" }
|
31
|
-
named_scope :started, :conditions => { :workflow_state => "started" }
|
32
|
-
named_scope :finished, :conditions => { :workflow_state => "finished" }
|
33
|
-
|
34
|
-
has_many :imported_objects, :class_name => "Importer::ImportedObject::ActiveRecord", :foreign_key => "import_id"
|
35
|
-
|
36
|
-
def build_imported_object
|
37
|
-
imported_objects.build(:import => self)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
module Importer
|
2
|
-
module Import
|
3
|
-
# Simple import summary. It's not stored in database (as with +ActiveRecord+ import).
|
4
|
-
#
|
5
|
-
# Attributes:
|
6
|
-
# * +new_objects_count+ - number of new objects created during the import
|
7
|
-
# * +existing_objects_count+ - number of objects modified during the import
|
8
|
-
# * +invalid_objects_count+ - number of objects that couldn't have been imported
|
9
|
-
# * +workflow_state+ - import may be in one of three states: ready, started or
|
10
|
-
# finished. The state changes during the import process.
|
11
|
-
class Simple
|
12
|
-
attr_reader :state, :new_objects_count, :existing_objects_count, :invalid_objects_count, :imported_objects
|
13
|
-
|
14
|
-
class << self
|
15
|
-
def create
|
16
|
-
import = new
|
17
|
-
import.save
|
18
|
-
import
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def initialize
|
23
|
-
@state = "ready"
|
24
|
-
@new_objects_count = 0
|
25
|
-
@existing_objects_count = 0
|
26
|
-
@invalid_objects_count = 0
|
27
|
-
@imported_objects = []
|
28
|
-
end
|
29
|
-
|
30
|
-
def start!
|
31
|
-
@state = "started"
|
32
|
-
end
|
33
|
-
|
34
|
-
def finish!
|
35
|
-
@state = "finished"
|
36
|
-
end
|
37
|
-
|
38
|
-
def add_object(imported_object)
|
39
|
-
case imported_object.state
|
40
|
-
when "new_object"
|
41
|
-
@new_objects_count += 1
|
42
|
-
when "existing_object"
|
43
|
-
@existing_objects_count += 1
|
44
|
-
when "invalid_object"
|
45
|
-
@invalid_objects_count += 1
|
46
|
-
end
|
47
|
-
@imported_objects << imported_object
|
48
|
-
end
|
49
|
-
|
50
|
-
def save
|
51
|
-
true
|
52
|
-
end
|
53
|
-
|
54
|
-
alias_method :save!, :save
|
55
|
-
|
56
|
-
def build_imported_object
|
57
|
-
Importer::ImportedObject::Simple.new(:import => self)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
|
3
|
-
module Importer
|
4
|
-
module ImportedObject
|
5
|
-
# ActiveRecord model that stores detailed information of imported objects in
|
6
|
-
# imported_objects database table.
|
7
|
-
#
|
8
|
-
# belongs_to :import - reference to import instance
|
9
|
-
# belongs_to :object - reference to imported object
|
10
|
-
#
|
11
|
-
# Attributes:
|
12
|
-
# * +data+ - object's detected attributes hash
|
13
|
-
# * +validation_errors+ - object's validation errors hash.
|
14
|
-
class ActiveRecord < ::ActiveRecord::Base
|
15
|
-
set_table_name "imported_objects"
|
16
|
-
|
17
|
-
named_scope :new_objects, :conditions => { :state => "new_object" }
|
18
|
-
named_scope :existing_objects, :conditions => { :state => "existing_object" }
|
19
|
-
named_scope :invalid_objects, :conditions => { :state => "invalid_object" }
|
20
|
-
|
21
|
-
belongs_to :import, :class_name => "Importer::Import::ActiveRecord"
|
22
|
-
belongs_to :object, :polymorphic => true
|
23
|
-
|
24
|
-
after_save :increment_counter
|
25
|
-
after_destroy :decrement_counter
|
26
|
-
|
27
|
-
serialize :data
|
28
|
-
serialize :validation_errors
|
29
|
-
|
30
|
-
protected
|
31
|
-
|
32
|
-
def increment_counter
|
33
|
-
import.increment!("#{state}s_count") if import
|
34
|
-
end
|
35
|
-
|
36
|
-
def decrement_counter
|
37
|
-
import.decrement!("#{state}s_count") if import
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
module Importer
|
2
|
-
module ImportedObject
|
3
|
-
# Simple imported object details. It's not stored in database (as with
|
4
|
-
# +ActiveRecord+ imported object).
|
5
|
-
#
|
6
|
-
# Attributes:
|
7
|
-
# * +data+ - object's detected attributes hash
|
8
|
-
# * +validation_errors+ - object's validation errors hash
|
9
|
-
class Simple
|
10
|
-
attr_reader :import
|
11
|
-
attr_accessor :state, :object, :data, :validation_errors
|
12
|
-
|
13
|
-
def initialize(attributes = {})
|
14
|
-
raise ArgumentError.new(":import attribute is required.") unless attributes[:import]
|
15
|
-
@import = attributes[:import]
|
16
|
-
end
|
17
|
-
|
18
|
-
def save
|
19
|
-
import.add_object(self)
|
20
|
-
end
|
21
|
-
|
22
|
-
alias_method :save!, :save
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Rails
|
2
|
-
module Generator
|
3
|
-
class ImporterGenerator < NamedBase
|
4
|
-
def banner
|
5
|
-
"Usage: #{$0} importer import"
|
6
|
-
end
|
7
|
-
|
8
|
-
def manifest
|
9
|
-
record do |m|
|
10
|
-
time = Time.now
|
11
|
-
m.template "imports_migration.rb", "db/migrate/#{time.strftime('%Y%m%d%H%M%S')}_create_imports.rb"
|
12
|
-
time += 1
|
13
|
-
m.template "imported_objects_migration.rb", "db/migrate/#{time.strftime('%Y%m%d%H%M%S')}_create_imported_objects.rb"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
class CreateImportedObjects < ActiveRecord::Migration
|
2
|
-
def self.up
|
3
|
-
create_table :imported_objects do |t|
|
4
|
-
t.integer :import_id, :null => false
|
5
|
-
t.string :object_type
|
6
|
-
t.integer :object_id
|
7
|
-
t.text :data
|
8
|
-
t.text :validation_errors
|
9
|
-
t.string :state, :null => false, :limit => 20
|
10
|
-
t.timestamps
|
11
|
-
end
|
12
|
-
add_index :imported_objects, :import_id
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.down
|
16
|
-
remove_index :imported_objects, :import_id
|
17
|
-
drop_table :imported_objects
|
18
|
-
end
|
19
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
class CreateImports < ActiveRecord::Migration
|
2
|
-
def self.up
|
3
|
-
create_table :imports do |t|
|
4
|
-
t.integer :new_objects_count, :null => false, :default => 0
|
5
|
-
t.integer :existing_objects_count, :null => false, :default => 0
|
6
|
-
t.integer :invalid_objects_count, :null => false, :default => 0
|
7
|
-
t.string :workflow_state, :null => false, :default => "ready", :limit => 10
|
8
|
-
t.timestamps
|
9
|
-
end
|
10
|
-
add_index :imports, :workflow_state
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.down
|
14
|
-
remove_index :imports, :workflow_state
|
15
|
-
drop_table :imports
|
16
|
-
end
|
17
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class Importer::Import::ActiveRecordTest < Test::Unit::TestCase
|
4
|
-
context "" do
|
5
|
-
setup do
|
6
|
-
@product = Factory(:product, :customid => "1", :name => "A pink ball", :description => "Round glass ball.", :price => 86)
|
7
|
-
end
|
8
|
-
|
9
|
-
context "importing from an XML file" do
|
10
|
-
setup do
|
11
|
-
@import = Product.import(fixture_file("products.xml"))
|
12
|
-
end
|
13
|
-
|
14
|
-
should "create one new product" do
|
15
|
-
assert_equal 1, @import.new_objects_count
|
16
|
-
end
|
17
|
-
|
18
|
-
should "modify one existing product" do
|
19
|
-
assert_equal 1, @import.existing_objects_count
|
20
|
-
end
|
21
|
-
|
22
|
-
should "not save one product because of errors" do
|
23
|
-
assert_equal 1, @import.invalid_objects_count
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context "an import" do
|
29
|
-
setup { @import = Factory(:active_record_import) }
|
30
|
-
|
31
|
-
should "correctly build imported object" do
|
32
|
-
imported_object = @import.build_imported_object
|
33
|
-
assert_equal @import, imported_object.import
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class Importer::Import::SimpleTest < Test::Unit::TestCase
|
4
|
-
context "" do
|
5
|
-
setup do
|
6
|
-
@product = Factory(:product, :customid => "1", :name => "A pink ball", :description => "Round glass ball.", :price => 86)
|
7
|
-
end
|
8
|
-
|
9
|
-
context "importing from an XML file" do
|
10
|
-
setup do
|
11
|
-
@import = Product.import(fixture_file("products.xml"), :import => Factory(:simple_import))
|
12
|
-
end
|
13
|
-
|
14
|
-
should "create one new product" do
|
15
|
-
assert_equal 1, @import.new_objects_count
|
16
|
-
end
|
17
|
-
|
18
|
-
should "modify one existing product" do
|
19
|
-
assert_equal 1, @import.existing_objects_count
|
20
|
-
end
|
21
|
-
|
22
|
-
should "not save one product because of errors" do
|
23
|
-
assert_equal 1, @import.invalid_objects_count
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context "an import" do
|
29
|
-
setup { @import = Factory(:simple_import) }
|
30
|
-
|
31
|
-
should "correctly build imported object" do
|
32
|
-
imported_object = @import.build_imported_object
|
33
|
-
assert_equal @import, imported_object.import
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|