fruit_to_lime 0.8.2 → 0.9.0

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.
Files changed (46) hide show
  1. data/bin/fruit_to_lime +24 -24
  2. data/lib/fruit_to_lime/address_helper.rb +17 -17
  3. data/lib/fruit_to_lime/csv_helper.rb +32 -32
  4. data/lib/fruit_to_lime/model/address.rb +37 -37
  5. data/lib/fruit_to_lime/model/coworker.rb +44 -0
  6. data/lib/fruit_to_lime/model/coworker_reference.rb +17 -14
  7. data/lib/fruit_to_lime/model/customfield.rb +30 -29
  8. data/lib/fruit_to_lime/model/deal.rb +49 -0
  9. data/lib/fruit_to_lime/model/note.rb +38 -27
  10. data/lib/fruit_to_lime/model/organization.rb +140 -121
  11. data/lib/fruit_to_lime/model/person.rb +115 -111
  12. data/lib/fruit_to_lime/model/referencetosource.rb +42 -42
  13. data/lib/fruit_to_lime/model/rootmodel.rb +142 -39
  14. data/lib/fruit_to_lime/model/tag.rb +33 -28
  15. data/lib/fruit_to_lime/model_helpers.rb +19 -0
  16. data/lib/fruit_to_lime/roo_helper.rb +59 -54
  17. data/lib/fruit_to_lime/serialize_helper.rb +139 -139
  18. data/lib/fruit_to_lime/templating.rb +51 -51
  19. data/lib/fruit_to_lime.rb +13 -12
  20. data/spec/helpers/address_helper_spec.rb +48 -48
  21. data/spec/helpers/csv_helper_spec.rb +15 -15
  22. data/spec/helpers/roo_helper_spec.rb +10 -10
  23. data/spec/helpers/serialize_helper_spec.rb +211 -211
  24. data/spec/person_spec.rb +44 -44
  25. data/spec/spec_helper.rb +24 -24
  26. data/spec/templating_spec.rb +40 -40
  27. data/templates/csv/Gemfile +5 -5
  28. data/templates/csv/Rakefile.rb +7 -7
  29. data/templates/csv/convert.rb +2 -2
  30. data/templates/csv/lib/tomodel.rb +56 -42
  31. data/templates/csv/spec/sample_data/organizations.csv +2 -2
  32. data/templates/csv/spec/spec_helper.rb +24 -24
  33. data/templates/csv/spec/tomodel_spec.rb +14 -14
  34. data/templates/excel/Gemfile +6 -6
  35. data/templates/excel/Rakefile.rb +7 -7
  36. data/templates/excel/convert.rb +2 -2
  37. data/templates/excel/lib/tomodel.rb +53 -39
  38. data/templates/excel/spec/spec_helper.rb +20 -20
  39. data/templates/excel/spec/tomodel_spec.rb +18 -18
  40. data/templates/sqlserver/Gemfile +6 -6
  41. data/templates/sqlserver/Rakefile.rb +7 -7
  42. data/templates/sqlserver/convert.rb +2 -2
  43. data/templates/sqlserver/lib/tomodel.rb +67 -53
  44. data/templates/sqlserver/spec/spec_helper.rb +20 -20
  45. data/templates/sqlserver/spec/tomodel_spec.rb +9 -9
  46. metadata +23 -4
@@ -1,39 +1,142 @@
1
- module FruitToLime
2
- class RootModel
3
- attr_accessor :organizations, :coworkers, :deals
4
- def serialize_variables
5
- [
6
- {:id=>:organizations,:type=>:organizations},
7
- {:id=>:coworkers, :type=>:coworkers}
8
- ]
9
- end
10
- def serialize_name
11
- "GoImport"
12
- end
13
- include SerializeHelper
14
- def initialize()
15
- @organizations = []
16
- @coworkers = []
17
- end
18
- def find_organization_by_reference(organization_reference)
19
- same_as_this = organization_reference.same_as_this_method
20
- return @organizations.find do |org|
21
- same_as_this.call(org)
22
- end
23
- end
24
-
25
- def validate()
26
- error = String.new
27
-
28
- @organizations.each do |o|
29
- validation_message = o.validate()
30
-
31
- if !validation_message.empty?
32
- error = "#{error}\n#{validation_message}"
33
- end
34
- end
35
-
36
- return error
37
- end
38
- end
39
- end
1
+ module FruitToLime
2
+ class RootModel
3
+ # the import_coworker is a special coworker that is set as
4
+ # responsible for objects that requires a coworker, eg a note.
5
+ attr_accessor :organizations, :coworkers, :deals, :notes, :import_coworker
6
+ def serialize_variables
7
+ [
8
+ {:id => :coworkers, :type => :coworkers},
9
+ {:id => :organizations, :type => :organizations},
10
+ {:id => :deals, :type => :deals},
11
+ {:id => :notes, :type => :notes},
12
+ ]
13
+ end
14
+
15
+ def serialize_name
16
+ "GoImport"
17
+ end
18
+
19
+ include SerializeHelper
20
+
21
+ def initialize()
22
+ @organizations = []
23
+ @coworkers = []
24
+ @import_coworker = Coworker.new
25
+ @import_coworker.integration_id = "import"
26
+ @import_coworker.first_name = "Import"
27
+ @coworkers.push @import_coworker
28
+ @deals = []
29
+ @notes = []
30
+ end
31
+
32
+ # Adds the specifed coworker object to the model.
33
+ def add_coworker(coworker)
34
+ @coworkers = [] if @coworkers == nil
35
+
36
+ if coworker != nil && coworker.is_a?(Coworker) && !@coworkers.include?(coworker)
37
+ @coworkers.push(coworker)
38
+ end
39
+ end
40
+
41
+ def add_note(text)
42
+ @notes = [] if @notes == nil
43
+ @notes.push(if text.is_a? Note then text else Note.new(text) end)
44
+ end
45
+
46
+ def with_note
47
+ @notes = [] if @notes == nil
48
+
49
+ note = Note.new
50
+ @notes.push note
51
+ yield note
52
+ end
53
+
54
+ # *** TODO:
55
+ #
56
+ # delete find_organization_by_reference and
57
+ #same_as_this_method from organization?
58
+ def find_organization_by_reference(organization_reference)
59
+ same_as_this = organization_reference.same_as_this_method
60
+ return @organizations.find do |org|
61
+ same_as_this.call(org)
62
+ end
63
+ end
64
+
65
+ def find_coworker_by_integration_id(integration_id)
66
+ return @coworkers.find do |coworker|
67
+ coworker == integration_id
68
+ end
69
+ end
70
+
71
+ def find_organization_by_integration_id(integration_id)
72
+ return @organizations.find do |organization|
73
+ organization == integration_id
74
+ end
75
+ end
76
+
77
+ def find_deals_for_organization(organization)
78
+ deals = []
79
+
80
+ deals = @deals.select do |deal|
81
+ !deal.customer.nil? && deal.customer.integration_id == organization.integration_id
82
+ end
83
+
84
+ return deals
85
+ end
86
+
87
+ def sanity_check
88
+ error = String.new
89
+
90
+ dups = get_duplicates(@coworkers) {|coworker| coworker.integration_id}
91
+ dups_error_items = (dups.collect{|coworker| coworker.integration_id}).compact
92
+ if dups.length > 0
93
+ error = "#{error}\nDuplicate coworker integration_id: #{dups_error_items.join(", ")}."
94
+ end
95
+
96
+ dups = get_duplicates(@organizations) {|org| org.integration_id}
97
+ dups_error_items = (dups.collect{|org| org.integration_id}).compact
98
+ if dups.length > 0
99
+ error = "#{error}\nDuplicate organization integration_id: #{dups_error_items.join(", ")}."
100
+ end
101
+
102
+ dups = get_duplicates(@deals) {|deal| deal.integration_id}
103
+ dups_error_items = (dups.collect{|deal| deal.integration_id}).compact
104
+ if dups_error_items.length > 0
105
+ error = "#{error}\nDuplicate deal integration_id: #{dups_error_items.join(", ")}."
106
+ end
107
+
108
+ return error.strip
109
+ end
110
+
111
+ # returns all items from the object array with duplicate keys.
112
+ # To get all organizations with the same integration_id use
113
+ # get_duplicates(organizations, {|org| org.integration_id})
114
+ def get_duplicates(objects, &key)
115
+ uniq_items = objects.uniq {|item| key.call(item)}.compact
116
+
117
+ return (objects - uniq_items).compact
118
+ end
119
+
120
+ def validate()
121
+ error = String.new
122
+
123
+ @organizations.each do |o|
124
+ validation_message = o.validate()
125
+
126
+ if !validation_message.empty?
127
+ error = "#{error}\n#{validation_message}"
128
+ end
129
+ end
130
+
131
+ @deals.each do |deal|
132
+ validation_message = deal.validate
133
+
134
+ if !validation_message.empty?
135
+ error = "#{error}\n#{validation_message}"
136
+ end
137
+ end
138
+
139
+ return error.strip
140
+ end
141
+ end
142
+ end
@@ -1,28 +1,33 @@
1
- # encoding: utf-8
2
- module FruitToLime
3
- class Tag
4
- def serialize_name
5
- "Tag"
6
- end
7
- attr_accessor :value
8
- def initialize(val=nil)
9
- if val
10
- @value = val
11
- end
12
- end
13
- def to_xml
14
- varn = serialize_name
15
- "<#{varn}>#{ @value.to_s.encode('utf-8').encode(:xml => :text) }</#{varn}>"
16
- end
17
- def to_s
18
- return "tag: '#{@value}'"
19
- end
20
- def ==(other)
21
- if other.respond_to?(:value)
22
- return @value == other.value
23
- else
24
- return false
25
- end
26
- end
27
- end
28
- end
1
+ # encoding: utf-8
2
+ module FruitToLime
3
+ class Tag
4
+ def serialize_name
5
+ "Tag"
6
+ end
7
+
8
+ attr_accessor :value
9
+
10
+ def initialize(val=nil)
11
+ if val
12
+ @value = val
13
+ end
14
+ end
15
+
16
+ def to_xml
17
+ varn = serialize_name
18
+ "<#{varn}>#{ @value.to_s.encode('utf-8').encode(:xml => :text) }</#{varn}>"
19
+ end
20
+
21
+ def to_s
22
+ return "tag: '#{@value}'"
23
+ end
24
+
25
+ def ==(other)
26
+ if other.respond_to?(:value)
27
+ return @value == other.value
28
+ else
29
+ return false
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ module FruitToLime
2
+ module ModelHasCustomFields
3
+ # attr_reader :custom_fields
4
+
5
+ #@custom_fields = []
6
+
7
+ def set_custom_field(obj)
8
+ @custom_fields = [] if @custom_fields==nil
9
+ @custom_fields.push CustomField.new(obj)
10
+ end
11
+ end
12
+
13
+ module ModelHasTags
14
+ def add_tag(str)
15
+ @tags = [] if @tags == nil
16
+ @tags.push(Tag.new(str))
17
+ end
18
+ end
19
+ end
@@ -1,54 +1,59 @@
1
- require "csv"
2
- module FruitToLime
3
- class RooHelper
4
- def initialize(data)
5
- @data = data
6
- @sheet = data.sheets.first
7
- @map = {}
8
- 1.upto(data.last_column(@sheet)) do |col|
9
- @map[col] = @data.cell(1, col, @sheet).encode('UTF-8')
10
- end
11
- end
12
-
13
- def rows
14
- rs = []
15
- 2.upto(@data.last_row(@sheet)) do |row|
16
- r={}
17
- 1.upto(@data.last_column(@sheet)) do |col|
18
- val = cell_to_csv(row, col, @sheet)
19
- r[@map[col]] = val
20
- end
21
- rs.push(r)
22
- end
23
- return rs
24
- end
25
-
26
- def cell_to_csv(row, col, sheet)
27
- if @data.empty?(row,col,sheet)
28
- ''
29
- else
30
- onecell = @data.cell(row,col,sheet)
31
- case @data.celltype(row,col,sheet)
32
- when :string
33
- unless onecell.empty?
34
- onecell.encode('UTF-8')
35
- end
36
- when :float, :percentage
37
- if onecell == onecell.to_i
38
- onecell.to_i.to_s
39
- else
40
- onecell.to_s
41
- end
42
- when :date, :datetime
43
- onecell.to_s
44
- when :time
45
- Roo::Base.integer_to_timestring(onecell)
46
- when :formula
47
- onecell.to_s
48
- else
49
- raise "unhandled celltype #{@data.celltype(row,col,sheet)} for cell at row: #{row}, col: #{col} in sheet #{sheet}"
50
- end || ""
51
- end
52
- end
53
- end
54
- end
1
+ require "csv"
2
+ module FruitToLime
3
+ class RooHelper
4
+ def initialize(data)
5
+ @data = data
6
+ @default_sheet = data.sheets.first
7
+ end
8
+
9
+ def rows
10
+ return rows_for_sheet(@default_sheet)
11
+ end
12
+
13
+ def rows_for_sheet(sheet)
14
+ column_headers = {}
15
+ 1.upto(@data.last_column(sheet)) do |col|
16
+ column_headers[col] = @data.cell(1, col, sheet).encode('UTF-8')
17
+ end
18
+
19
+ rs = []
20
+ 2.upto(@data.last_row(sheet)) do |row|
21
+ r = {}
22
+ 1.upto(@data.last_column(sheet)) do |col|
23
+ val = cell_to_csv(row, col, sheet)
24
+ r[column_headers[col]] = val
25
+ end
26
+ rs.push(r)
27
+ end
28
+ return rs
29
+ end
30
+
31
+ def cell_to_csv(row, col, sheet)
32
+ if @data.empty?(row,col,sheet)
33
+ ''
34
+ else
35
+ onecell = @data.cell(row,col,sheet)
36
+ case @data.celltype(row,col,sheet)
37
+ when :string
38
+ unless onecell.empty?
39
+ onecell.encode('UTF-8').strip
40
+ end
41
+ when :float, :percentage
42
+ if onecell == onecell.to_i
43
+ onecell.to_i.to_s
44
+ else
45
+ onecell.to_s
46
+ end
47
+ when :date, :datetime
48
+ onecell.to_s
49
+ when :time
50
+ Roo::Base.integer_to_timestring(onecell)
51
+ when :formula
52
+ onecell.to_s
53
+ else
54
+ raise "unhandled celltype #{@data.celltype(row,col,sheet)} for cell at row: #{row}, col: #{col} in sheet #{sheet}"
55
+ end || ""
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,139 +1,139 @@
1
- # encoding: utf-8
2
- module FruitToLime
3
- module SerializeHelper
4
-
5
- def serialize()
6
- SerializeHelper::serialize(self)
7
- end
8
-
9
- def serialize_to_file(file)
10
- SerializeHelper::serialize_to_file(file, self)
11
- end
12
-
13
- def self.serialize_variables(obj)
14
- if (obj.respond_to?(:serialize_variables))
15
- return obj.serialize_variables.map do |ivar|
16
- varn = ivar[:id].to_s.gsub(/^\@/,'').split('_').map do |m|
17
- m.capitalize
18
- end.join('')
19
- varv = obj.instance_variable_get("@#{ivar[:id].to_s}")
20
- if (varv.respond_to?(:serialize_variables))
21
- varv = serialize_variables(varv)
22
- elsif (varv.is_a?(Array))
23
- varv = varv.map { |elem| SerializeHelper::serialize(elem) }.join("\n")
24
- elsif (varv == nil)
25
- varv = nil
26
- else
27
- varv = varv.to_s.encode('UTF-8').encode(:xml => :text)
28
- end
29
- if varv != nil then "<#{varn}>#{ varv }</#{varn}>" else "" end
30
- if varv != nil then "<#{varn}>#{ varv }</#{varn}>" end
31
- end.join("\n")
32
- end
33
- raise "!!#{obj.class}"
34
- end
35
-
36
- def self.serialize(obj)
37
- if (obj.respond_to?(:serialize_variables))
38
- varn = obj.serialize_name
39
- "<#{varn}>#{ SerializeHelper::serialize_variables(obj) }</#{varn}>"
40
- elsif obj.respond_to?(:to_xml)
41
- obj.to_xml
42
- else
43
- obj.to_s.encode(:xml => :text)
44
- end
45
- end
46
-
47
- def self.serialize_to_file(file, obj)
48
- File.open(file, 'w') do |f|
49
- f.write(SerializeHelper::serialize(obj))
50
- end
51
- end
52
-
53
- def symbol_to_name(symbol)
54
- symbol.to_s.split('_').join(' ').capitalize
55
- end
56
-
57
- def map_symbol_to_row(symbol,type)
58
- {
59
- :id=>symbol.to_s,
60
- :name=> symbol==:id ? 'Go id' : symbol_to_name(symbol),
61
- :type=>type
62
- }
63
- end
64
-
65
- def map_to_row(p)
66
- case p[:type]
67
- when :string then
68
- map_symbol_to_row(p[:id],p[:type])
69
- when :bool then
70
- map_symbol_to_row(p[:id],p[:type])
71
- when :date then
72
- map_symbol_to_row(p[:id],p[:type])
73
- when :notes then
74
- {
75
- :id => p[:id].to_s,
76
- :name => symbol_to_name(p[:id]),
77
- :type => p[:type],
78
- :models => SerializeHelper.get_import_rows(:note)
79
- }
80
- when :tags then
81
- {
82
- :id => p[:id].to_s,
83
- :type => p[:type],
84
- :name => symbol_to_name(p[:id]),
85
- }
86
- when :persons then
87
- {
88
- :id => p[:id].to_s,
89
- :type => p[:type],
90
- :name => symbol_to_name(p[:id]),
91
- :models => SerializeHelper.get_import_rows(:person)
92
- }
93
- when :custom_fields then
94
- {
95
- :id => p[:id].to_s,
96
- :type => p[:type],
97
- :name => symbol_to_name(p[:id]),
98
- :models => SerializeHelper.get_import_rows(:custom_field)
99
- }
100
- else
101
- {
102
- :id => p[:id].to_s,
103
- :name => symbol_to_name(p[:id]),
104
- :type => p[:type],
105
- :model => SerializeHelper.get_import_rows(p[:type])
106
- }
107
- end
108
- end
109
-
110
- def get_import_rows
111
- serialize_variables.map do |p|
112
- map_to_row p
113
- end
114
- end
115
-
116
- def self.get_import_rows(type)
117
- case type
118
- when :person then
119
- Person.new
120
- when :source_ref then
121
- ReferenceToSource.new
122
- when :note then
123
- Note.new
124
- when :address then
125
- Address.new
126
- when :organization then
127
- Organization.new
128
- when :coworker_reference then
129
- CoworkerReference.new
130
- when :organization_reference then
131
- OrganizationReference.new
132
- when :custom_field then
133
- CustomField.new
134
- else
135
- raise "Unknown type: #{type}"
136
- end.get_import_rows
137
- end
138
- end
139
- end
1
+ # encoding: utf-8
2
+ module FruitToLime
3
+ module SerializeHelper
4
+ def serialize()
5
+ SerializeHelper::serialize(self)
6
+ end
7
+
8
+ def serialize_to_file(file)
9
+ SerializeHelper::serialize_to_file(file, self)
10
+ end
11
+
12
+ def self.serialize_variables(obj)
13
+ if (obj.respond_to?(:serialize_variables))
14
+ return obj.serialize_variables.map do |ivar|
15
+ varn = ivar[:id].to_s.gsub(/^\@/,'').split('_').map do |m|
16
+ m.capitalize
17
+ end.join('')
18
+
19
+ varv = obj.instance_variable_get("@#{ivar[:id].to_s}")
20
+ if (varv.respond_to?(:serialize_variables))
21
+ varv = serialize_variables(varv)
22
+ elsif (varv.is_a?(Array))
23
+ varv = varv.map { |elem| SerializeHelper::serialize(elem) }.join("\n")
24
+ elsif (varv == nil)
25
+ varv = nil
26
+ else
27
+ varv = varv.to_s.encode('UTF-8').encode(:xml => :text)
28
+ end
29
+ if varv != nil then "<#{varn}>#{ varv }</#{varn}>" else "" end
30
+ if varv != nil then "<#{varn}>#{ varv }</#{varn}>" end
31
+ end.join("\n")
32
+ end
33
+ raise "!!#{obj.class}"
34
+ end
35
+
36
+ def self.serialize(obj)
37
+ if (obj.respond_to?(:serialize_variables))
38
+ varn = obj.serialize_name
39
+ "<#{varn}>#{ SerializeHelper::serialize_variables(obj) }</#{varn}>"
40
+ elsif obj.respond_to?(:to_xml)
41
+ obj.to_xml
42
+ else
43
+ obj.to_s.encode(:xml => :text)
44
+ end
45
+ end
46
+
47
+ def self.serialize_to_file(file, obj)
48
+ File.open(file, 'w') do |f|
49
+ f.write(SerializeHelper::serialize(obj))
50
+ end
51
+ end
52
+
53
+ def symbol_to_name(symbol)
54
+ symbol.to_s.split('_').join(' ').capitalize
55
+ end
56
+
57
+ def map_symbol_to_row(symbol,type)
58
+ {
59
+ :id => symbol.to_s,
60
+ :name => symbol == :id ? 'Go id' : symbol_to_name(symbol),
61
+ :type =>type
62
+ }
63
+ end
64
+
65
+ def map_to_row(p)
66
+ case p[:type]
67
+ when :string then
68
+ map_symbol_to_row(p[:id],p[:type])
69
+ when :bool then
70
+ map_symbol_to_row(p[:id],p[:type])
71
+ when :date then
72
+ map_symbol_to_row(p[:id],p[:type])
73
+ when :notes then
74
+ {
75
+ :id => p[:id].to_s,
76
+ :name => symbol_to_name(p[:id]),
77
+ :type => p[:type],
78
+ :models => SerializeHelper.get_import_rows(:note)
79
+ }
80
+ when :tags then
81
+ {
82
+ :id => p[:id].to_s,
83
+ :type => p[:type],
84
+ :name => symbol_to_name(p[:id]),
85
+ }
86
+ when :persons then
87
+ {
88
+ :id => p[:id].to_s,
89
+ :type => p[:type],
90
+ :name => symbol_to_name(p[:id]),
91
+ :models => SerializeHelper.get_import_rows(:person)
92
+ }
93
+ when :custom_fields then
94
+ {
95
+ :id => p[:id].to_s,
96
+ :type => p[:type],
97
+ :name => symbol_to_name(p[:id]),
98
+ :models => SerializeHelper.get_import_rows(:custom_field)
99
+ }
100
+ else
101
+ {
102
+ :id => p[:id].to_s,
103
+ :name => symbol_to_name(p[:id]),
104
+ :type => p[:type],
105
+ :model => SerializeHelper.get_import_rows(p[:type])
106
+ }
107
+ end
108
+ end
109
+
110
+ def get_import_rows
111
+ serialize_variables.map do |p|
112
+ map_to_row p
113
+ end
114
+ end
115
+
116
+ def self.get_import_rows(type)
117
+ case type
118
+ when :person then
119
+ Person.new
120
+ when :source_ref then
121
+ ReferenceToSource.new
122
+ when :note then
123
+ Note.new
124
+ when :address then
125
+ Address.new
126
+ when :organization then
127
+ Organization.new
128
+ when :coworker_reference then
129
+ CoworkerReference.new
130
+ when :organization_reference then
131
+ OrganizationReference.new
132
+ when :custom_field then
133
+ CustomField.new
134
+ else
135
+ raise "Unknown type: #{type}"
136
+ end.get_import_rows
137
+ end
138
+ end
139
+ end