active_import 0.0.2 → 0.0.3
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/README.rdoc
CHANGED
@@ -30,10 +30,12 @@ There are three Rake tasks that allow you to use a converter on a file:
|
|
30
30
|
|
31
31
|
=== Examples
|
32
32
|
|
33
|
-
rake active_import:csv user.csv CONVERTER=User CONVERTER_OPTIONS="give_admin_access=true"
|
33
|
+
rake active_import:csv FILE=user.csv CONVERTER=User CONVERTER_OPTIONS="give_admin_access=true"
|
34
34
|
|
35
35
|
In this case the file in db/active_import/user.csv would be run through the converter UserConverter. The options specified are available within the converter. In this case @options["give_admin_access"] will evaluate to true.
|
36
36
|
|
37
|
+
The FILE parameter is not strictly necessary in this case either as the default file name will be an underscored value of the name of the converter.
|
38
|
+
|
37
39
|
== Seeding
|
38
40
|
|
39
41
|
Seeding allows you to import several files through different model converters in a single command. It involves the creation of a .seed file. Each file goes on a single line and the options are separated by pipe symbols.
|
@@ -44,8 +46,15 @@ Seeding allows you to import several files through different model converters in
|
|
44
46
|
user_info/roles.xls | Role
|
45
47
|
user_info/permissions.xls | UserPermission
|
46
48
|
|
49
|
+
You could save this file as db/active_import/user_info.seed and run the command like this:
|
50
|
+
|
51
|
+
rake active_import:seed SET=user_info
|
52
|
+
|
47
53
|
Note this will get the data files from a subdirectory: db/active_import/user_info. Also the top conversion uses options but as they are optional, the following two do not.
|
48
54
|
|
55
|
+
If you do not specify a set, the rake task will look for a set based on the current development environment: ie development.seed.
|
56
|
+
|
57
|
+
|
49
58
|
== The Converter Class
|
50
59
|
|
51
60
|
=== Column Setup
|
data/lib/active_import.rb
CHANGED
@@ -0,0 +1,50 @@
|
|
1
|
+
module ActiveImport
|
2
|
+
class Seed
|
3
|
+
|
4
|
+
def self.seed_from_file seed_file_name
|
5
|
+
seed_file = File.open(seed_file_name, "r")
|
6
|
+
seed_file.each_line do |seed_line|
|
7
|
+
unless (/^#/.match(seed_line))
|
8
|
+
seed_info = seed_line.split("|")
|
9
|
+
data_file_name = seed_info[0]
|
10
|
+
converter_name = seed_info[1]
|
11
|
+
|
12
|
+
unless data_file_name.nil? || converter_name.nil?
|
13
|
+
data_file_name = import_file_with_path data_file_name
|
14
|
+
|
15
|
+
if (File.exists? data_file_name)
|
16
|
+
converter_name.strip!
|
17
|
+
converter_options = process_converter_options seed_info[2]
|
18
|
+
|
19
|
+
puts "Importing from: #{data_file_name.cyan}"
|
20
|
+
puts "Using converter: #{converter_name.cyan}"
|
21
|
+
puts "Options: #{converter_options.to_s.cyan}"
|
22
|
+
|
23
|
+
model_converter = eval("#{converter_name.camelize}Converter.new")
|
24
|
+
model_converter.options = converter_options
|
25
|
+
|
26
|
+
import = nil
|
27
|
+
extension = File.extname(data_file_name).downcase.strip
|
28
|
+
case extension
|
29
|
+
when ".xls"
|
30
|
+
puts "Excel file detected".yellow
|
31
|
+
import = ActiveImport::ImportExcel.new(model_converter, data_file_name)
|
32
|
+
when ".xlsx"
|
33
|
+
puts "Excel(X) file detected".yellow
|
34
|
+
import = ActiveImport::ImportExcel.new(model_converter, data_file_name)
|
35
|
+
when ".csv"
|
36
|
+
puts "CSV file detected".yellow
|
37
|
+
import = ActiveImport::ImportCsv.new(model_converter, data_file_name)
|
38
|
+
else
|
39
|
+
puts "File type cannot be processed".red
|
40
|
+
end
|
41
|
+
parse(import, model_converter) unless import.nil?
|
42
|
+
else
|
43
|
+
puts "Skipping non existing file: #{data_file_name}".red
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -4,7 +4,7 @@ class <%= @converter_name.camelize %>Converter < ActiveImport::ModelConverter
|
|
4
4
|
def setup
|
5
5
|
<% @attributes.each do |attribute| -%>
|
6
6
|
<% unless ["id", "created_at", "updated_at"].include? attribute -%>
|
7
|
-
add_column :<%= attribute %>, :match => "
|
7
|
+
add_column :<%= attribute %>, :match => "^<%= attribute %>$", :type => :<%= @model.columns_hash[attribute].type %>, :mandatory => true
|
8
8
|
<% end -%>
|
9
9
|
<% end -%>
|
10
10
|
end
|
@@ -63,49 +63,6 @@ def parse(import, model_converter)
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def seed_from_file(seed_file_name)
|
66
|
-
seed_file = File.open(seed_file_name, "r")
|
67
|
-
seed_file.each_line do |seed_line|
|
68
|
-
unless (/^#/.match(seed_line))
|
69
|
-
seed_info = seed_line.split("|")
|
70
|
-
data_file_name = seed_info[0]
|
71
|
-
converter_name = seed_info[1]
|
72
|
-
|
73
|
-
unless data_file_name.nil? || converter_name.nil?
|
74
|
-
data_file_name = import_file_with_path data_file_name
|
75
|
-
|
76
|
-
if (File.exists? data_file_name)
|
77
|
-
converter_name.strip!
|
78
|
-
converter_options = process_converter_options seed_info[2]
|
79
|
-
|
80
|
-
puts "Importing from: #{data_file_name.cyan}"
|
81
|
-
puts "Using converter: #{converter_name.cyan}"
|
82
|
-
puts "Options: #{converter_options.to_s.cyan}"
|
83
|
-
|
84
|
-
model_converter = eval("#{converter_name.camelize}Converter.new")
|
85
|
-
model_converter.options = converter_options
|
86
|
-
|
87
|
-
import = nil
|
88
|
-
extension = File.extname(data_file_name).downcase.strip
|
89
|
-
case extension
|
90
|
-
when ".xls"
|
91
|
-
puts "Excel file detected".yellow
|
92
|
-
import = ActiveImport::ImportExcel.new(model_converter, data_file_name)
|
93
|
-
when ".xlsx"
|
94
|
-
puts "Excel(X) file detected".yellow
|
95
|
-
import = ActiveImport::ImportExcel.new(model_converter, data_file_name)
|
96
|
-
when ".csv"
|
97
|
-
puts "CSV file detected".yellow
|
98
|
-
import = ActiveImport::ImportCsv.new(model_converter, data_file_name)
|
99
|
-
else
|
100
|
-
puts "File type cannot be processed".red
|
101
|
-
end
|
102
|
-
parse(import, model_converter) unless import.nil?
|
103
|
-
else
|
104
|
-
puts "Skipping non existing file: #{data_file_name}".red
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
66
|
end
|
110
67
|
|
111
68
|
namespace :active_import do
|
@@ -131,7 +88,7 @@ namespace :active_import do
|
|
131
88
|
seed_file_name = File.join(Rails.root, "db", "active_import", "#{set}.seed")
|
132
89
|
if File.exists?(seed_file_name)
|
133
90
|
puts "Seeding from file: '#{seed_file_name.yellow}'"
|
134
|
-
seed_from_file seed_file_name
|
91
|
+
ActiveImport::Seed.seed_from_file seed_file_name
|
135
92
|
else
|
136
93
|
puts "Cannot find seed file: '#{seed_file_name}'".red
|
137
94
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_import
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-04-16 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70129661763280 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70129661763280
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: colorize
|
27
|
-
requirement: &
|
27
|
+
requirement: &70129661762220 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70129661762220
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: roo
|
38
|
-
requirement: &
|
38
|
+
requirement: &70129661759880 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70129661759880
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: sqlite3
|
49
|
-
requirement: &
|
49
|
+
requirement: &70129661758520 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70129661758520
|
58
58
|
description: ''
|
59
59
|
email:
|
60
60
|
- david.monagle@intrica.com.au
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- lib/active_import/import_csv.rb
|
66
66
|
- lib/active_import/import_excel.rb
|
67
67
|
- lib/active_import/model_converter.rb
|
68
|
+
- lib/active_import/seed.rb
|
68
69
|
- lib/active_import/version.rb
|
69
70
|
- lib/active_import.rb
|
70
71
|
- lib/generators/active_import/model_converter_generator.rb
|
@@ -119,7 +120,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
119
120
|
version: '0'
|
120
121
|
segments:
|
121
122
|
- 0
|
122
|
-
hash:
|
123
|
+
hash: 3899470936762174354
|
123
124
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
125
|
none: false
|
125
126
|
requirements:
|
@@ -128,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
129
|
version: '0'
|
129
130
|
segments:
|
130
131
|
- 0
|
131
|
-
hash:
|
132
|
+
hash: 3899470936762174354
|
132
133
|
requirements: []
|
133
134
|
rubyforge_project:
|
134
135
|
rubygems_version: 1.8.6
|