@@ -12,15 +12,18 @@ With these Table Importer you are able to import any legacy Data and map it easi
12
12
13
13
=== Usage
14
14
15
- * get your Data as nested Array in rows ans columns
15
+ * get your Data as nested Array in rows and columns
16
16
* build a dictionary to map your data to your Classes
17
- * {"ClassName"=>attributes}
17
+ {"ClassName"=>attributes}
18
18
where attributes is a hash of of objects attributes and its corresponding columns
19
- * attributes = {:attribute=>:col_1}
19
+ attributes = {:attribute=>:col_1}
20
20
* you can use number or letters eg. :col1, :col_1, :col_A, :colB, :col_AB, :col_aa etc.
21
- * for pre processing data use array with a lambda as last element (:attribute => [:co_l,col_2,lambda{|col1,col2| col1 +" " +col2 }]
22
- * use constant values :attribute=> "My Value"
23
- * for associations user Hashes :association => {:attribute=>:col1}
21
+ * for pre processing data use array with a lambda as last element
22
+ :attribute => [:co_l,col_2,lambda{|col1,col2| col1 +" " +col2 }]
23
+ * use constant values
24
+ :attribute=> "My Value"
25
+ * for associations user Hashes
26
+ :association => {:attribute=>:col1}
24
27
* Build the Mapper by passing the dictonary
25
28
mapper = TableImporter::Mapper.new(dictonary)
26
29
* Build the Importer by passing the data and the mapper
@@ -31,12 +34,12 @@ where attributes is a hash of of objects attributes and its corresponding column
31
34
32
35
=== Note
33
36
34
- The the Objects are only build not saved so you can post process them before saving.
37
+ The Objects are only build not saved so you can post process them before saving.
35
38
36
39
37
40
=== Example
38
41
39
- Assume you hab the follwoing Tabluar Data (e.g. from a CVS File with CSV.read("your_file.csv"))
42
+ Assume you have the follwoing Tabluar Data (e.g. from a CVS File with CSV.read("your_file.csv"))
40
43
41
44
data=
42
45
[
@@ -47,25 +50,20 @@ Assume you hab the follwoing Tabluar Data (e.g. from a CVS File with CSV.read("y
47
50
And you have the follwoing simple Data Structure
48
51
49
52
Class Contact < ActiveRecord::Base
50
-
51
53
# first_name String (column1)
52
54
# last_name String (column2)
53
55
# full_name String (column1 column2)
54
56
has_many :tags
55
57
belongs_to :user
56
58
has_one :address
57
-
58
59
end
59
60
60
61
Class Tag < ActiveRecord::Base
61
-
62
62
#tagging String (column 8, 9 and 10)
63
63
belongs_to :contact
64
-
65
64
end
66
65
67
66
Class User < ActiveRecord::Base
68
-
69
67
# name String (column 6)
70
68
has_many :contacts
71
69
has_one :group
@@ -76,14 +74,12 @@ And you have the follwoing simple Data Structure
76
74
end
77
75
78
76
Class Address
79
-
80
77
# street String (column3)
81
78
# number Integer (RegEx Number Part of column3)
82
79
# zip String (column4)
83
80
# city String (column5)
84
81
# country String ('USA' constant for the import)
85
82
belongs_to :contact
86
-
87
83
end
88
84
89
85
You can build your Data as follows
@@ -93,7 +89,7 @@ You can build your Data as follows
93
89
dictonary = {"Contact" =>{:first_name=>:col_1,:last_name=>:col_2,:full_name=>[:col_1,:col_2,lambda{|f,l| "#{v} #{l}"}],
94
90
# has_many associations you have three tags here
95
91
:tags=>{:tagging => [:col_8,:col_9,:col_10]},
96
- # belongs_to
92
+ # nested belongs_to
97
93
:user => {:name=>:col_6,:group=>{:name=>:col_7}},
98
94
# has_one
99
95
:address => {:street=>[:col_3,lambda{|s| s[/(.*?) ([\d]+)/,1]}],
0 commit comments