fruit_to_lime 2.5.3 → 2.5.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/lib/fruit_to_lime.rb +17 -17
  2. data/lib/fruit_to_lime/csv_helper.rb +47 -47
  3. data/lib/fruit_to_lime/email_helper.rb +10 -10
  4. data/lib/fruit_to_lime/errors.rb +16 -16
  5. data/lib/fruit_to_lime/excel_helper.rb +10 -10
  6. data/lib/fruit_to_lime/global_phone.json +6571 -6571
  7. data/lib/fruit_to_lime/model/address.rb +60 -60
  8. data/lib/fruit_to_lime/model/class_settings.rb +50 -50
  9. data/lib/fruit_to_lime/model/coworker.rb +76 -76
  10. data/lib/fruit_to_lime/model/coworker_reference.rb +33 -33
  11. data/lib/fruit_to_lime/model/customfield.rb +87 -87
  12. data/lib/fruit_to_lime/model/deal.rb +141 -145
  13. data/lib/fruit_to_lime/model/deal_status.rb +12 -12
  14. data/lib/fruit_to_lime/model/note.rb +79 -79
  15. data/lib/fruit_to_lime/model/organization.rb +203 -197
  16. data/lib/fruit_to_lime/model/person.rb +151 -151
  17. data/lib/fruit_to_lime/model/referencetosource.rb +45 -45
  18. data/lib/fruit_to_lime/model/relation.rb +23 -23
  19. data/lib/fruit_to_lime/model/rootmodel.rb +338 -330
  20. data/lib/fruit_to_lime/model/settings.rb +60 -60
  21. data/lib/fruit_to_lime/model/tag.rb +35 -35
  22. data/lib/fruit_to_lime/model_helpers.rb +54 -54
  23. data/lib/fruit_to_lime/phone_helper.rb +74 -74
  24. data/lib/fruit_to_lime/roo_helper.rb +72 -72
  25. data/lib/fruit_to_lime/serialize_helper.rb +186 -186
  26. data/lib/fruit_to_lime/templating.rb +52 -52
  27. data/spec/address_spec.rb +48 -48
  28. data/spec/class_settings_spec.rb +37 -37
  29. data/spec/coworker_spec.rb +94 -94
  30. data/spec/custom_field_spec.rb +22 -22
  31. data/spec/deal_spec.rb +101 -102
  32. data/spec/helpers/csv_helper_spec.rb +29 -29
  33. data/spec/helpers/email_helper_spec.rb +32 -32
  34. data/spec/helpers/phone_helper_spec.rb +97 -97
  35. data/spec/helpers/serialize_helper_spec.rb +249 -249
  36. data/spec/helpers/xsd_validate_spec.rb +58 -58
  37. data/spec/note_spec.rb +98 -67
  38. data/spec/organization_spec.rb +103 -89
  39. data/spec/person_spec.rb +134 -134
  40. data/spec/rootmodel_spec.rb +277 -230
  41. data/spec/templating_spec.rb +11 -11
  42. data/templates/csv/lib/tomodel.rb +230 -230
  43. data/templates/csv/spec/exporter_spec.rb +17 -17
  44. data/templates/csv/spec/sample_data/coworkers.csv +2 -2
  45. data/templates/csv/spec/sample_data/deals.csv +2 -2
  46. data/templates/csv/spec/sample_data/organizations.csv +2 -2
  47. data/templates/csv/spec/sample_data/persons.csv +2 -2
  48. data/templates/easy/Gemfile +5 -0
  49. data/templates/easy/Rakefile.rb +7 -0
  50. data/templates/easy/convert.rb +3 -0
  51. data/templates/easy/lib/tomodel.rb +330 -0
  52. data/templates/easy/spec/exporter_spec.rb +10 -0
  53. data/templates/easy/spec/sample_data/Company.txt +649 -0
  54. data/templates/easy/spec/spec_helper.rb +24 -0
  55. data/templates/excel/lib/tomodel.rb +207 -207
  56. data/templates/sqlserver/lib/tomodel.rb +79 -79
  57. metadata +10 -3
@@ -1,72 +1,72 @@
1
- require "csv"
2
- module FruitToLime
3
- # @example transform xlsx file into rows
4
- # organizations_path = File.join(File.dirname(__FILE__), 'organizations.xlsx') # same path as this file
5
- # rows = FruitToLime::RooHelper.new(Roo::Excelx.new(organizations_path)).rows
6
- class RooHelper
7
-
8
- def initialize(data)
9
- @data = data
10
- @default_sheet = data.sheets.first
11
- end
12
-
13
- # Get rows for the first sheet.
14
- # The rows are hashes of the first row of cells as header cells and the rest as content.
15
- # @example If the header 'Name' and the second column contains 'Johan'.
16
- # FruitToLime::RooHelper.new(Roo::Excelx.new(file_path)).rows
17
- # # returns:
18
- # [{'Name'=>'Johan'}]
19
- def rows
20
- return rows_for_sheet(@default_sheet)
21
- end
22
-
23
- # @example transform xlsx file into rows for the second sheet
24
- # data = Roo::Excelx.new(organizations_path)
25
- # rows = FruitToLime::RooHelper.new(data).rows_for_sheet(data.sheets[1])
26
- def rows_for_sheet(sheet)
27
- column_headers = {}
28
- 1.upto(@data.last_column(sheet)) do |col|
29
- column_headers[col] = @data.cell(1, col, sheet).encode('UTF-8')
30
- end
31
-
32
- rs = []
33
- 2.upto(@data.last_row(sheet)) do |row|
34
- r = {}
35
- 1.upto(@data.last_column(sheet)) do |col|
36
- val = cell_to_csv(row, col, sheet)
37
- r[column_headers[col]] = val
38
- end
39
- rs.push(r)
40
- end
41
- return rs
42
- end
43
-
44
- def cell_to_csv(row, col, sheet)
45
- if @data.empty?(row,col,sheet)
46
- ''
47
- else
48
- onecell = @data.cell(row,col,sheet)
49
- case @data.celltype(row,col,sheet)
50
- when :string
51
- unless onecell.empty?
52
- onecell.encode('UTF-8').strip
53
- end
54
- when :float, :percentage
55
- if onecell == onecell.to_i
56
- onecell.to_i.to_s
57
- else
58
- onecell.to_s
59
- end
60
- when :date, :datetime
61
- onecell.to_s
62
- when :time
63
- Roo::Base.integer_to_timestring(onecell)
64
- when :formula
65
- onecell.to_s
66
- else
67
- raise "unhandled celltype #{@data.celltype(row,col,sheet)} for cell at row: #{row}, col: #{col} in sheet #{sheet}"
68
- end || ""
69
- end
70
- end
71
- end
72
- end
1
+ require "csv"
2
+ module FruitToLime
3
+ # @example transform xlsx file into rows
4
+ # organizations_path = File.join(File.dirname(__FILE__), 'organizations.xlsx') # same path as this file
5
+ # rows = FruitToLime::RooHelper.new(Roo::Excelx.new(organizations_path)).rows
6
+ class RooHelper
7
+
8
+ def initialize(data)
9
+ @data = data
10
+ @default_sheet = data.sheets.first
11
+ end
12
+
13
+ # Get rows for the first sheet.
14
+ # The rows are hashes of the first row of cells as header cells and the rest as content.
15
+ # @example If the header 'Name' and the second column contains 'Johan'.
16
+ # FruitToLime::RooHelper.new(Roo::Excelx.new(file_path)).rows
17
+ # # returns:
18
+ # [{'Name'=>'Johan'}]
19
+ def rows
20
+ return rows_for_sheet(@default_sheet)
21
+ end
22
+
23
+ # @example transform xlsx file into rows for the second sheet
24
+ # data = Roo::Excelx.new(organizations_path)
25
+ # rows = FruitToLime::RooHelper.new(data).rows_for_sheet(data.sheets[1])
26
+ def rows_for_sheet(sheet)
27
+ column_headers = {}
28
+ 1.upto(@data.last_column(sheet)) do |col|
29
+ column_headers[col] = @data.cell(1, col, sheet).encode('UTF-8')
30
+ end
31
+
32
+ rs = []
33
+ 2.upto(@data.last_row(sheet)) do |row|
34
+ r = {}
35
+ 1.upto(@data.last_column(sheet)) do |col|
36
+ val = cell_to_csv(row, col, sheet)
37
+ r[column_headers[col]] = val
38
+ end
39
+ rs.push(r)
40
+ end
41
+ return rs
42
+ end
43
+
44
+ def cell_to_csv(row, col, sheet)
45
+ if @data.empty?(row,col,sheet)
46
+ ''
47
+ else
48
+ onecell = @data.cell(row,col,sheet)
49
+ case @data.celltype(row,col,sheet)
50
+ when :string
51
+ unless onecell.empty?
52
+ onecell.encode('UTF-8').strip
53
+ end
54
+ when :float, :percentage
55
+ if onecell == onecell.to_i
56
+ onecell.to_i.to_s
57
+ else
58
+ onecell.to_s
59
+ end
60
+ when :date, :datetime
61
+ onecell.to_s
62
+ when :time
63
+ Roo::Base.integer_to_timestring(onecell)
64
+ when :formula
65
+ onecell.to_s
66
+ else
67
+ raise "unhandled celltype #{@data.celltype(row,col,sheet)} for cell at row: #{row}, col: #{col} in sheet #{sheet}"
68
+ end || ""
69
+ end
70
+ end
71
+ end
72
+ end
@@ -1,186 +1,186 @@
1
- # encoding: utf-8
2
- require "rexml/document"
3
-
4
- module FruitToLime
5
- module SerializeHelper
6
- def serialize()
7
- SerializeHelper::serialize(self)
8
- end
9
-
10
- def serialize_to_file(file)
11
- SerializeHelper::serialize_to_file(file, self)
12
- end
13
-
14
- # @!visibility private
15
- def self.serialize_variables_rexml(elem, obj)
16
- if (obj.respond_to?(:serialize_variables))
17
- obj.serialize_variables.each do |serialize_variable|
18
- element_name = serialize_variable[:id].to_s.gsub(/^\@/,'').split('_').map do |m|
19
- m.capitalize
20
- end.join('')
21
-
22
- raw_var = obj.instance_variable_get("@#{serialize_variable[:id].to_s}")
23
- if raw_var != nil
24
- element = elem.add_element(element_name)
25
- if (raw_var.respond_to?(:serialize_variables))
26
- SerializeHelper::serialize_variables_rexml(element, raw_var)
27
- elsif (raw_var.is_a?(Array))
28
- raw_var.each do |raw_var_elem|
29
- SerializeHelper::serialize_rexml(element, raw_var_elem)
30
- end
31
- else
32
- element.text = raw_var.to_s.encode('UTF-8')
33
- end
34
- end
35
- end
36
- return
37
- end
38
- raise "Do not know how to handle #{obj.class} !!"
39
- end
40
-
41
- # @!visibility private
42
- def self.serialize_rexml(elem, obj)
43
- if obj.respond_to?(:to_rexml)
44
- obj.to_rexml(elem)
45
- elsif (obj.respond_to?(:serialize_variables))
46
- element_name = obj.serialize_name
47
- SerializeHelper::serialize_variables_rexml(elem.add_element(element_name), obj)
48
- else
49
- elem.text = obj.to_s
50
- end
51
- end
52
-
53
- # @!visibility private
54
- def self.serialize(obj, indent= 2)
55
- # indent -1 to avoid indent
56
- if obj.respond_to?(:to_rexml)
57
- doc = REXML::Document.new()
58
- SerializeHelper::serialize_rexml(doc, obj)
59
- doc.write( xml_str = "" , indent, true)
60
- xml_str
61
- elsif (obj.respond_to?(:serialize_variables))
62
- element_name = obj.serialize_name
63
- doc = REXML::Document.new()
64
- SerializeHelper::serialize_variables_rexml(doc.add_element(element_name), obj)
65
- doc.write( xml_str = "", indent, true)
66
- xml_str
67
- else
68
- obj.to_s.encode(:xml => :text)
69
- end
70
- end
71
-
72
- # @!visibility private
73
- def self.serialize_to_file(file, obj)
74
- File.open(file, 'w') do |f|
75
- f.write(SerializeHelper::serialize(obj))
76
- end
77
- end
78
-
79
- # @!visibility private
80
- def symbol_to_name(symbol)
81
- symbol.to_s.split('_').join(' ').capitalize
82
- end
83
-
84
- # @!visibility private
85
- def map_symbol_to_row(symbol,type)
86
- {
87
- :id => symbol.to_s,
88
- :name => symbol == :id ? 'Go id' : symbol_to_name(symbol),
89
- :type =>type
90
- }
91
- end
92
-
93
- # @!visibility private
94
- def map_to_row(p)
95
- case p[:type]
96
- when :string then
97
- map_symbol_to_row(p[:id],p[:type])
98
- when :bool then
99
- map_symbol_to_row(p[:id],p[:type])
100
- when :date then
101
- map_symbol_to_row(p[:id],p[:type])
102
- when :notes then
103
- {
104
- :id => p[:id].to_s,
105
- :name => symbol_to_name(p[:id]),
106
- :type => p[:type],
107
- :models => SerializeHelper.get_import_rows(:note)
108
- }
109
- when :tags then
110
- {
111
- :id => p[:id].to_s,
112
- :type => p[:type],
113
- :name => symbol_to_name(p[:id]),
114
- }
115
- when :persons then
116
- {
117
- :id => p[:id].to_s,
118
- :type => p[:type],
119
- :name => symbol_to_name(p[:id]),
120
- :models => SerializeHelper.get_import_rows(:person)
121
- }
122
- when :custom_fields then
123
- {
124
- :id => p[:id].to_s,
125
- :type => p[:type],
126
- :name => symbol_to_name(p[:id]),
127
- :models => SerializeHelper.get_import_rows(:custom_field)
128
- }
129
- when :custom_values then
130
- {
131
- :id => p[:id].to_s,
132
- :type => p[:type],
133
- :name => symbol_to_name(p[:id]),
134
- :models => SerializeHelper.get_import_rows(:custom_value)
135
- }
136
- else
137
- {
138
- :id => p[:id].to_s,
139
- :name => symbol_to_name(p[:id]),
140
- :type => p[:type],
141
- :model => SerializeHelper.get_import_rows(p[:type])
142
- }
143
- end
144
- end
145
-
146
- # What fields/rows on the class is supposed to be used by the Gem to generate the xml
147
- # This method uses #serialize_variables.
148
- def get_import_rows
149
- serialize_variables.map do |p|
150
- map_to_row p
151
- end
152
- end
153
-
154
- # @!visibility private
155
- def self.get_import_rows(type)
156
- case type
157
- when :person then
158
- Person.new
159
- when :source_ref then
160
- ReferenceToSource.new
161
- when :note then
162
- Note.new
163
- when :address then
164
- Address.new
165
- when :organization then
166
- Organization.new
167
- when :coworker_reference then
168
- CoworkerReference.new
169
- when :organization_reference then
170
- OrganizationReference.new
171
- when :custom_field then
172
- CustomField.new
173
- when :custom_value then
174
- CustomValue.new
175
- when :custom_field_reference then
176
- CustomFieldReference.new
177
- when :settings then
178
- Settings.new
179
- when :class_settings then
180
- ClassSettings.new
181
- else
182
- raise "Unknown type: #{type}"
183
- end.get_import_rows
184
- end
185
- end
186
- end
1
+ # encoding: utf-8
2
+ require "rexml/document"
3
+
4
+ module FruitToLime
5
+ module SerializeHelper
6
+ def serialize()
7
+ SerializeHelper::serialize(self)
8
+ end
9
+
10
+ def serialize_to_file(file)
11
+ SerializeHelper::serialize_to_file(file, self)
12
+ end
13
+
14
+ # @!visibility private
15
+ def self.serialize_variables_rexml(elem, obj)
16
+ if (obj.respond_to?(:serialize_variables))
17
+ obj.serialize_variables.each do |serialize_variable|
18
+ element_name = serialize_variable[:id].to_s.gsub(/^\@/,'').split('_').map do |m|
19
+ m.capitalize
20
+ end.join('')
21
+
22
+ raw_var = obj.instance_variable_get("@#{serialize_variable[:id].to_s}")
23
+ if raw_var != nil
24
+ element = elem.add_element(element_name)
25
+ if (raw_var.respond_to?(:serialize_variables))
26
+ SerializeHelper::serialize_variables_rexml(element, raw_var)
27
+ elsif (raw_var.is_a?(Array))
28
+ raw_var.each do |raw_var_elem|
29
+ SerializeHelper::serialize_rexml(element, raw_var_elem)
30
+ end
31
+ else
32
+ element.text = raw_var.to_s.encode('UTF-8')
33
+ end
34
+ end
35
+ end
36
+ return
37
+ end
38
+ raise "Do not know how to handle #{obj.class} !!"
39
+ end
40
+
41
+ # @!visibility private
42
+ def self.serialize_rexml(elem, obj)
43
+ if obj.respond_to?(:to_rexml)
44
+ obj.to_rexml(elem)
45
+ elsif (obj.respond_to?(:serialize_variables))
46
+ element_name = obj.serialize_name
47
+ SerializeHelper::serialize_variables_rexml(elem.add_element(element_name), obj)
48
+ else
49
+ elem.text = obj.to_s
50
+ end
51
+ end
52
+
53
+ # @!visibility private
54
+ def self.serialize(obj, indent= 2)
55
+ # indent -1 to avoid indent
56
+ if obj.respond_to?(:to_rexml)
57
+ doc = REXML::Document.new()
58
+ SerializeHelper::serialize_rexml(doc, obj)
59
+ doc.write( xml_str = "" , indent, true)
60
+ xml_str
61
+ elsif (obj.respond_to?(:serialize_variables))
62
+ element_name = obj.serialize_name
63
+ doc = REXML::Document.new()
64
+ SerializeHelper::serialize_variables_rexml(doc.add_element(element_name), obj)
65
+ doc.write( xml_str = "", indent, true)
66
+ xml_str
67
+ else
68
+ obj.to_s.encode(:xml => :text)
69
+ end
70
+ end
71
+
72
+ # @!visibility private
73
+ def self.serialize_to_file(file, obj)
74
+ File.open(file, 'w') do |f|
75
+ f.write(SerializeHelper::serialize(obj))
76
+ end
77
+ end
78
+
79
+ # @!visibility private
80
+ def symbol_to_name(symbol)
81
+ symbol.to_s.split('_').join(' ').capitalize
82
+ end
83
+
84
+ # @!visibility private
85
+ def map_symbol_to_row(symbol,type)
86
+ {
87
+ :id => symbol.to_s,
88
+ :name => symbol == :id ? 'Go id' : symbol_to_name(symbol),
89
+ :type =>type
90
+ }
91
+ end
92
+
93
+ # @!visibility private
94
+ def map_to_row(p)
95
+ case p[:type]
96
+ when :string then
97
+ map_symbol_to_row(p[:id],p[:type])
98
+ when :bool then
99
+ map_symbol_to_row(p[:id],p[:type])
100
+ when :date then
101
+ map_symbol_to_row(p[:id],p[:type])
102
+ when :notes then
103
+ {
104
+ :id => p[:id].to_s,
105
+ :name => symbol_to_name(p[:id]),
106
+ :type => p[:type],
107
+ :models => SerializeHelper.get_import_rows(:note)
108
+ }
109
+ when :tags then
110
+ {
111
+ :id => p[:id].to_s,
112
+ :type => p[:type],
113
+ :name => symbol_to_name(p[:id]),
114
+ }
115
+ when :persons then
116
+ {
117
+ :id => p[:id].to_s,
118
+ :type => p[:type],
119
+ :name => symbol_to_name(p[:id]),
120
+ :models => SerializeHelper.get_import_rows(:person)
121
+ }
122
+ when :custom_fields then
123
+ {
124
+ :id => p[:id].to_s,
125
+ :type => p[:type],
126
+ :name => symbol_to_name(p[:id]),
127
+ :models => SerializeHelper.get_import_rows(:custom_field)
128
+ }
129
+ when :custom_values then
130
+ {
131
+ :id => p[:id].to_s,
132
+ :type => p[:type],
133
+ :name => symbol_to_name(p[:id]),
134
+ :models => SerializeHelper.get_import_rows(:custom_value)
135
+ }
136
+ else
137
+ {
138
+ :id => p[:id].to_s,
139
+ :name => symbol_to_name(p[:id]),
140
+ :type => p[:type],
141
+ :model => SerializeHelper.get_import_rows(p[:type])
142
+ }
143
+ end
144
+ end
145
+
146
+ # What fields/rows on the class is supposed to be used by the Gem to generate the xml
147
+ # This method uses #serialize_variables.
148
+ def get_import_rows
149
+ serialize_variables.map do |p|
150
+ map_to_row p
151
+ end
152
+ end
153
+
154
+ # @!visibility private
155
+ def self.get_import_rows(type)
156
+ case type
157
+ when :person then
158
+ Person.new
159
+ when :source_ref then
160
+ ReferenceToSource.new
161
+ when :note then
162
+ Note.new
163
+ when :address then
164
+ Address.new
165
+ when :organization then
166
+ Organization.new
167
+ when :coworker_reference then
168
+ CoworkerReference.new
169
+ when :organization_reference then
170
+ OrganizationReference.new
171
+ when :custom_field then
172
+ CustomField.new
173
+ when :custom_value then
174
+ CustomValue.new
175
+ when :custom_field_reference then
176
+ CustomFieldReference.new
177
+ when :settings then
178
+ Settings.new
179
+ when :class_settings then
180
+ ClassSettings.new
181
+ else
182
+ raise "Unknown type: #{type}"
183
+ end.get_import_rows
184
+ end
185
+ end
186
+ end