fruit_to_lime 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,207 @@
1
+ module FruitToLime
2
+ module SerializeHelper
3
+
4
+ def fill_using(entity_columns, row)
5
+ entity_columns.select do |ecol|
6
+ ecol.selected!=nil && ecol.selected.length >0 && ecol.column!=nil && ecol.column.length > 0
7
+ end.each do |ecol|
8
+ selected = ecol.selected
9
+ value = row[ecol.column]
10
+ set_attribute(selected, value)
11
+ end
12
+ return self
13
+ end
14
+
15
+ def serialize()
16
+ SerializeHelper::serialize(self)
17
+ end
18
+
19
+ def serialize_to_file(file)
20
+ SerializeHelper::serialize_to_file(file, self)
21
+ end
22
+
23
+ def self.serialize_variables(obj)
24
+ if (obj.respond_to?(:serialize_variables))
25
+ return obj.serialize_variables.map do |ivar|
26
+ varn = ivar[:id].to_s.gsub(/^\@/,'').split('_').map do |m|
27
+ m.capitalize
28
+ end.join('')
29
+ varv = obj.instance_variable_get("@#{ivar[:id].to_s}")
30
+ if (varv.respond_to?(:serialize_variables))
31
+ varv = serialize_variables(varv)
32
+ elsif (varv.is_a?(Array))
33
+ varv = varv.map { |elem| SerializeHelper::serialize(elem) }.join("\n")
34
+ else
35
+ varv = varv.to_s.encode(:xml => :text)
36
+ end
37
+ "<#{varn}>#{ varv }</#{varn}>"
38
+ end.join("\n")
39
+ end
40
+ raise "!!#{obj.class}"
41
+ end
42
+
43
+ def self.serialize(obj)
44
+ if (obj.respond_to?(:serialize_variables))
45
+ varn = obj.serialize_name
46
+ "<#{varn}>#{ SerializeHelper::serialize_variables(obj) }</#{varn}>"
47
+ elsif obj.respond_to?(:to_xml)
48
+ obj.to_xml
49
+ else
50
+ obj.to_s.encode(:xml => :text)
51
+ end
52
+ end
53
+
54
+ def self.serialize_to_file(file, obj)
55
+ File.open(file, 'w') do |f|
56
+ f.write(SerializeHelper::serialize(obj))
57
+ end
58
+ end
59
+
60
+ def set_attribute(selected, value)
61
+ if selected.is_a? String
62
+ selected = [selected]
63
+ end
64
+ case selected.length
65
+ when 1
66
+ found = get_import_rows.find do |v|
67
+ v[:id].to_s == selected.first
68
+ end
69
+ if !found
70
+ return false
71
+ end
72
+ send(found[:id].to_s+"=",value)
73
+ return true
74
+ when 2
75
+ found = get_import_rows.find do |v|
76
+ v[:id].to_s == selected.first
77
+ end
78
+ if !found
79
+ return false
80
+ end
81
+ subval = send(found[:id].to_s)
82
+ if !subval
83
+ subval = get_default(found[:type])
84
+ send(found[:id].to_s+"=",subval)
85
+ end
86
+ subval.set_attribute(selected.slice(1), value)
87
+ else
88
+ raise "! #{selected.length}"
89
+ end
90
+ end
91
+
92
+ def symbol_to_name(symbol)
93
+ symbol.to_s.split('_').join(' ').capitalize
94
+ end
95
+
96
+ def get_default(type)
97
+ case type
98
+ when :string then
99
+ ""
100
+ when :bool then
101
+ false
102
+ when :source_ref then
103
+ SourceRef.new
104
+ when :address then
105
+ Address.new
106
+ when :notes then
107
+ []
108
+ when :tags then
109
+ []
110
+ when :persons then
111
+ []
112
+ when :custom_fields then
113
+ []
114
+ when :organization_reference then
115
+ OrganizationReference.new
116
+ when :date then
117
+ nil
118
+ when :coworker_reference then
119
+ CoworkerReference.new
120
+ else
121
+ raise "Dont know how to handle '#{type}'"
122
+ end
123
+ end
124
+
125
+ def map_symbol_to_row(symbol,type)
126
+ {
127
+ :id=>symbol.to_s,
128
+ :name=> symbol==:id ? 'Go id' : symbol_to_name(symbol),
129
+ :type=>type
130
+ }
131
+ end
132
+
133
+ def map_to_row(p)
134
+ case p[:type]
135
+ when :string then
136
+ map_symbol_to_row(p[:id],p[:type])
137
+ when :bool then
138
+ map_symbol_to_row(p[:id],p[:type])
139
+ when :date then
140
+ map_symbol_to_row(p[:id],p[:type])
141
+ when :notes then
142
+ {
143
+ :id => p[:id].to_s,
144
+ :name => symbol_to_name(p[:id]),
145
+ :type => p[:type],
146
+ :models => SerializeHelper.get_import_rows(:note)
147
+ }
148
+ when :tags then
149
+ {
150
+ :id => p[:id].to_s,
151
+ :type => p[:type],
152
+ :name => symbol_to_name(p[:id]),
153
+ }
154
+ when :persons then
155
+ {
156
+ :id => p[:id].to_s,
157
+ :type => p[:type],
158
+ :name => symbol_to_name(p[:id]),
159
+ :models => SerializeHelper.get_import_rows(:person)
160
+ }
161
+ when :custom_fields then
162
+ {
163
+ :id => p[:id].to_s,
164
+ :type => p[:type],
165
+ :name => symbol_to_name(p[:id]),
166
+ :models => SerializeHelper.get_import_rows(:custom_field)
167
+ }
168
+ else
169
+ {
170
+ :id => p[:id].to_s,
171
+ :name => symbol_to_name(p[:id]),
172
+ :type => p[:type],
173
+ :model => SerializeHelper.get_import_rows(p[:type])
174
+ }
175
+ end
176
+ end
177
+
178
+ def get_import_rows
179
+ serialize_variables.map do |p|
180
+ map_to_row p
181
+ end
182
+ end
183
+
184
+ def self.get_import_rows(type)
185
+ case type
186
+ when :person then
187
+ Person.new
188
+ when :source_ref then
189
+ SourceRef.new
190
+ when :note then
191
+ Note.new
192
+ when :address then
193
+ Address.new
194
+ when :organization then
195
+ Organization.new
196
+ when :coworker_reference then
197
+ CoworkerReference.new
198
+ when :organization_reference then
199
+ OrganizationReference.new
200
+ when :custom_field then
201
+ CustomField.new
202
+ else
203
+ raise "Unknown type: #{type}"
204
+ end.get_import_rows
205
+ end
206
+ end
207
+ end
metadata CHANGED
@@ -1,11 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fruit_to_lime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Oskar Gewalli
9
+ - Peter Wilhelmsson
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
@@ -80,6 +81,7 @@ files:
80
81
  - lib/fruit_to_lime/model/rootmodel.rb
81
82
  - lib/fruit_to_lime/model/sourceref.rb
82
83
  - lib/fruit_to_lime/model/tag.rb
84
+ - lib/fruit_to_lime/serialize_helper.rb
83
85
  - lib/fruit_to_lime.rb
84
86
  - spec/gosourceref_spec.rb
85
87
  - spec/helpers/address_helper_spec.rb