fruit_to_lime 2.1.2 → 2.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,13 @@
1
1
  require "csv"
2
2
  module FruitToLime
3
3
  module CsvHelper
4
- def self.detect_col_sep(text)
5
- firstline = text.split('\n').first
6
- col_seps = [';','\t',',']
7
- return col_seps.find do |c|
8
- firstline.include? c
9
- end
10
- end
11
-
4
+
5
+ # @example Detect column separator and transform to hashes
6
+ # hashes = FruitToLime::CsvHelper.text_to_hashes(text)
7
+ #
8
+ # @example Use specific column separator and transform to hashes
9
+ # column_separator = ','
10
+ # hashes = FruitToLime::CsvHelper.text_to_hashes(text, column_separator)
12
11
  def self.text_to_hashes(text, column_separator = nil)
13
12
  if !text
14
13
  raise "Missing text"
@@ -34,5 +33,15 @@ module FruitToLime
34
33
  end
35
34
  return rs
36
35
  end
36
+
37
+ private
38
+ def self.detect_col_sep(text)
39
+ firstline = text.split('\n').first
40
+ col_seps = [';','\t',',']
41
+ return col_seps.find do |c|
42
+ firstline.include? c
43
+ end
44
+ end
45
+
37
46
  end
38
47
  end
@@ -117,19 +117,19 @@ module FruitToLime
117
117
  def sanity_check
118
118
  error = String.new
119
119
 
120
- dups = get_duplicates(@coworkers) {|coworker| coworker.integration_id}
120
+ dups = get_integration_id_duplicates(with_non_empty_integration_id(@coworkers))
121
121
  dups_error_items = (dups.collect{|coworker| coworker.integration_id}).compact
122
122
  if dups.length > 0
123
123
  error = "#{error}\nDuplicate coworker integration_id: #{dups_error_items.join(", ")}."
124
124
  end
125
125
 
126
- dups = get_duplicates(@organizations) {|org| org.integration_id}
126
+ dups = get_integration_id_duplicates(with_non_empty_integration_id(@organizations))
127
127
  dups_error_items = (dups.collect{|org| org.integration_id}).compact
128
128
  if dups.length > 0
129
129
  error = "#{error}\nDuplicate organization integration_id: #{dups_error_items.join(", ")}."
130
130
  end
131
131
 
132
- dups = get_duplicates(@deals) {|deal| deal.integration_id}
132
+ dups = get_integration_id_duplicates(with_non_empty_integration_id(@deals))
133
133
  dups_error_items = (dups.collect{|deal| deal.integration_id}).compact
134
134
  if dups_error_items.length > 0
135
135
  error = "#{error}\nDuplicate deal integration_id: #{dups_error_items.join(", ")}."
@@ -138,17 +138,6 @@ module FruitToLime
138
138
  return error.strip
139
139
  end
140
140
 
141
- # returns all items from the object array with duplicate keys.
142
- # To get all organizations with the same integration_id use
143
- # @example Get all the organization duplicates with the same integration id
144
- # rm.get_duplicates(rm.organizations, {|org| org.integration_id})
145
- #
146
- def get_duplicates(objects, &key)
147
- uniq_items = objects.uniq {|item| key.call(item)}.compact
148
-
149
- return (objects - uniq_items).compact
150
- end
151
-
152
141
  def validate()
153
142
  error = String.new
154
143
 
@@ -177,5 +166,22 @@ module FruitToLime
177
166
  elem = doc.add_element(element_name,{"Version"=>"v2_0"})
178
167
  SerializeHelper::serialize_variables_rexml(elem, self)
179
168
  end
169
+
170
+ private
171
+ # returns all items from the object array with duplicate integration ids.
172
+ # To get all organizations with the same integration_id use
173
+ # @example Get all the organization duplicates with the same integration id
174
+ # rm.get_integration_id_duplicates(rm.organizations)
175
+ def get_integration_id_duplicates(objects)
176
+ uniq_items = objects.uniq {|item| item.integration_id}.compact
177
+
178
+ return (objects - uniq_items).compact
179
+ end
180
+
181
+ def with_non_empty_integration_id(objects)
182
+ return objects.select do |obj|
183
+ obj.integration_id!=nil && !obj.integration_id.empty?
184
+ end
185
+ end
180
186
  end
181
187
  end
@@ -1,5 +1,7 @@
1
1
  module FruitToLime
2
2
  module ModelHasCustomFields
3
+ # @example
4
+ # obj.set_custom_value(row['business_value_partner_info'], "partner_info")
3
5
  def set_custom_value(value, field)
4
6
  @custom_values = [] if @custom_values == nil
5
7
  custom_value = CustomValue.new()
@@ -36,7 +38,9 @@ module FruitToLime
36
38
  end
37
39
  end
38
40
 
39
- module ModelHasTags
41
+ module ModelHasTags
42
+ # @example
43
+ # obj.set_tag("partner")
40
44
  def set_tag(str)
41
45
  @tags = [] if @tags == nil
42
46
  if ! @tags.any? {|tag| tag.value == str }
@@ -1,15 +1,28 @@
1
1
  require "csv"
2
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
3
6
  class RooHelper
7
+
4
8
  def initialize(data)
5
9
  @data = data
6
10
  @default_sheet = data.sheets.first
7
11
  end
8
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'}]
9
19
  def rows
10
20
  return rows_for_sheet(@default_sheet)
11
21
  end
12
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])
13
26
  def rows_for_sheet(sheet)
14
27
  column_headers = {}
15
28
  1.upto(@data.last_column(sheet)) do |col|
@@ -10,24 +10,6 @@ module FruitToLime
10
10
  Dir.entries(@path).select { |d| d != '.' && d != '..' }
11
11
  end
12
12
 
13
- def exec_but_dont_show_unless_error(cmd)
14
- std_out_value = []
15
- Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
16
- while std_line = stdout.gets
17
- std_out_value << std_line
18
- end
19
-
20
- exit_status = wait_thr.value
21
- if !exit_status.success?
22
- puts "Failed with #{exit_status}"
23
- puts "std_out_value"
24
- puts std_out_value
25
-
26
- raise "failed exec #{cmd}"
27
- end
28
- end
29
- end
30
-
31
13
  def unpack(name, path)
32
14
  template = list.find { |t| t == name }
33
15
  if template
@@ -47,5 +29,24 @@ module FruitToLime
47
29
  false
48
30
  end
49
31
  end
32
+
33
+ private
34
+ def exec_but_dont_show_unless_error(cmd)
35
+ std_out_value = []
36
+ Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
37
+ while std_line = stdout.gets
38
+ std_out_value << std_line
39
+ end
40
+
41
+ exit_status = wait_thr.value
42
+ if !exit_status.success?
43
+ puts "Failed with #{exit_status}"
44
+ puts "std_out_value"
45
+ puts std_out_value
46
+
47
+ raise "failed exec #{cmd}"
48
+ end
49
+ end
50
+ end
50
51
  end
51
52
  end
@@ -42,7 +42,7 @@ describe "RootModel" do
42
42
  :email=>"kalle.anka@vonanka.com"
43
43
  })
44
44
  rootmodel.coworkers.length.should eq 2
45
- expect {
45
+ expect {
46
46
  rootmodel.add_coworker({
47
47
  :integration_id=>"123key",
48
48
  :first_name=>"Knatte",
@@ -54,5 +54,29 @@ describe "RootModel" do
54
54
  rootmodel.coworkers.length.should eq 2
55
55
  end
56
56
 
57
- end
57
+ it "will ignore empty integration ids during sanity check" do
58
+ org1 = FruitToLime::Organization.new
59
+ org1.name = "company 1"
60
+ rootmodel.organizations.push org1
61
+
62
+ org2 = FruitToLime::Organization.new
63
+ org2.name = "company 2"
64
+ rootmodel.organizations.push org2
65
+
66
+ rootmodel.sanity_check.should eq ""
67
+ end
68
+
69
+ it "will report when the same integration id is used during sanity check" do
70
+ org1 = FruitToLime::Organization.new
71
+ org1.integration_id = "1"
72
+ org1.name = "company 1"
73
+ rootmodel.organizations.push org1
58
74
 
75
+ org2 = FruitToLime::Organization.new
76
+ org2.integration_id = "1"
77
+ org2.name = "company 2"
78
+ rootmodel.organizations.push org2
79
+
80
+ rootmodel.sanity_check.should eq "Duplicate organization integration_id: 1."
81
+ end
82
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fruit_to_lime
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: