fruit_to_lime 0.7.3 → 0.8.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.
@@ -17,7 +17,7 @@ module FruitToLime
17
17
  def same_as_this_method()
18
18
  if @integration_id
19
19
  return lambda { |org|
20
- org.integration_id == @integration_id
20
+ org.integration_id == @integration_id
21
21
  }
22
22
  elsif @id
23
23
  return lambda { |org|
@@ -32,7 +32,7 @@ module FruitToLime
32
32
  end
33
33
  end
34
34
  end
35
-
35
+
36
36
  class Organization < OrganizationReference
37
37
  include SerializeHelper
38
38
 
@@ -62,10 +62,9 @@ module FruitToLime
62
62
  yield @source
63
63
  end
64
64
 
65
-
66
65
  def add_employee(val)
67
66
  @employees = [] if @employees==nil
68
- @employees.push(if val.is_a? Person then val else Person.new(val) end)
67
+ @employees.push(if val.is_a? Person then val else Person.new(val) end)
69
68
  end
70
69
 
71
70
  def tags
@@ -78,15 +77,18 @@ module FruitToLime
78
77
  end
79
78
 
80
79
  def serialize_variables
81
- [ :id, :integration_id, :name, :organization_number, :external_link, :email, :web_site ].map { |prop| { :id => prop, :type => :string } } +
82
80
  [
83
- { :id => :postal_address, :type => :address},
84
- { :id => :visit_address, :type => :address},
85
- { :id => :employees, :type => :persons},
86
- { :id => :tags, :type => :tags},
87
- { :id => :custom_fields, :type => :custom_fields},
88
- { :id => :source, :type => :source_ref}
89
- ]
81
+ :id, :integration_id, :name, :organization_number, :external_link, :email, :web_site ].map {
82
+ |prop| { :id => prop, :type => :string }
83
+ } +
84
+ [
85
+ { :id => :postal_address, :type => :address},
86
+ { :id => :visit_address, :type => :address},
87
+ { :id => :employees, :type => :persons},
88
+ { :id => :tags, :type => :tags},
89
+ { :id => :custom_fields, :type => :custom_fields},
90
+ { :id => :source, :type => :source_ref}
91
+ ]
90
92
  end
91
93
 
92
94
  def serialize_name
@@ -96,5 +98,24 @@ module FruitToLime
96
98
  def to_s
97
99
  return "#{name}"
98
100
  end
101
+
102
+ def validate
103
+ error = String.new
104
+
105
+ if @name.nil? || @name.empty?
106
+ error = "A name is required for organization.\n#{serialize()}"
107
+ end
108
+
109
+ if @employees != nil
110
+ @employees.each do |person|
111
+ validation_message = person.validate()
112
+ if !validation_message.empty?
113
+ error = "#{error}\n#{validation_message}"
114
+ end
115
+ end
116
+ end
117
+
118
+ return error
119
+ end
99
120
  end
100
- end
121
+ end
@@ -17,7 +17,7 @@ module FruitToLime
17
17
  def same_as_this_method()
18
18
  if @integration_id
19
19
  return lambda { |person|
20
- person.integration_id == @integration_id
20
+ person.integration_id == @integration_id
21
21
  }
22
22
  elsif @id
23
23
  return lambda { |person|
@@ -31,7 +31,7 @@ module FruitToLime
31
31
 
32
32
  class Person < PersonReference
33
33
  include SerializeHelper
34
- attr_accessor :first_name, :last_name,
34
+ attr_accessor :first_name, :last_name,
35
35
  :direct_phone_number, :fax_phone_number, :mobile_phone_number, :home_phone_number,
36
36
  :position, :email, :alternative_email, :postal_address, :currently_employed,
37
37
  :notes, :organization
@@ -75,7 +75,7 @@ module FruitToLime
75
75
  end
76
76
 
77
77
  def serialize_variables
78
- [ :id, :integration_id, :first_name, :last_name,
78
+ [ :id, :integration_id, :first_name, :last_name,
79
79
  :direct_phone_number, :fax_phone_number, :mobile_phone_number, :home_phone_number,
80
80
  :position, :email, :alternative_email].map { |prop| {:id=>prop,:type=>:string} } +
81
81
  [{:id=>:postal_address,:type=>:address},
@@ -85,16 +85,27 @@ module FruitToLime
85
85
  {:id=>:custom_fields,:type=>:custom_fields},
86
86
  {:id=>:source, :type=> :source_ref}
87
87
  ]
88
- end
88
+ end
89
89
 
90
90
  def get_import_rows
91
91
  (serialize_variables+[{:id=>:organization, :type=>:organization_reference}]).map do |p|
92
92
  map_to_row p
93
- end
93
+ end
94
94
  end
95
95
 
96
96
  def to_s
97
97
  return "#{first_name} #{last_name}"
98
98
  end
99
+
100
+ def validate
101
+ error = String.new
102
+
103
+ if (@first_name.nil? || @first_name.empty?) &&
104
+ (@last_name.nil? || @last_name.empty?)
105
+ error = "A firstname or lastname is required for person.\n#{serialize()}"
106
+ end
107
+
108
+ return error
109
+ end
99
110
  end
100
- end
111
+ end
@@ -21,5 +21,19 @@ module FruitToLime
21
21
  same_as_this.call(org)
22
22
  end
23
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
24
38
  end
25
- end
39
+ end
@@ -13,11 +13,11 @@ module SerializeHelper
13
13
  def self.serialize_variables(obj)
14
14
  if (obj.respond_to?(:serialize_variables))
15
15
  return obj.serialize_variables.map do |ivar|
16
- varn = ivar[:id].to_s.gsub(/^\@/,'').split('_').map do |m|
17
- m.capitalize
16
+ varn = ivar[:id].to_s.gsub(/^\@/,'').split('_').map do |m|
17
+ m.capitalize
18
18
  end.join('')
19
19
  varv = obj.instance_variable_get("@#{ivar[:id].to_s}")
20
- if (varv.respond_to?(:serialize_variables))
20
+ if (varv.respond_to?(:serialize_variables))
21
21
  varv = serialize_variables(varv)
22
22
  elsif (varv.is_a?(Array))
23
23
  varv = varv.map { |elem| SerializeHelper::serialize(elem) }.join("\n")
@@ -27,13 +27,14 @@ module SerializeHelper
27
27
  varv = varv.to_s.encode('UTF-8').encode(:xml => :text)
28
28
  end
29
29
  if varv != nil then "<#{varn}>#{ varv }</#{varn}>" else "" end
30
+ if varv != nil then "<#{varn}>#{ varv }</#{varn}>" end
30
31
  end.join("\n")
31
32
  end
32
33
  raise "!!#{obj.class}"
33
34
  end
34
35
 
35
36
  def self.serialize(obj)
36
- if (obj.respond_to?(:serialize_variables))
37
+ if (obj.respond_to?(:serialize_variables))
37
38
  varn = obj.serialize_name
38
39
  "<#{varn}>#{ SerializeHelper::serialize_variables(obj) }</#{varn}>"
39
40
  elsif obj.respond_to?(:to_xml)
@@ -114,7 +115,7 @@ module SerializeHelper
114
115
 
115
116
  def self.get_import_rows(type)
116
117
  case type
117
- when :person then
118
+ when :person then
118
119
  Person.new
119
120
  when :source_ref then
120
121
  ReferenceToSource.new
@@ -135,4 +136,4 @@ module SerializeHelper
135
136
  end.get_import_rows
136
137
  end
137
138
  end
138
- end
139
+ end
data/spec/person_spec.rb CHANGED
@@ -3,17 +3,42 @@ require 'fruit_to_lime'
3
3
 
4
4
  describe "Person" do
5
5
  before (:all) do
6
- @person = FruitToLime::Person.new
6
+ @person = FruitToLime::Person.new
7
7
  end
8
+
8
9
  it "can set a customfield" do
9
- @person.set_custom_field({:integration_id=>'the key',
10
+ @person.set_custom_field({:integration_id=>'the key',
10
11
  :title=> 'the title',
11
12
  :value=> 'the value'})
12
-
13
+
13
14
  field = @person.custom_fields[0]
14
15
  field.integration_id.should eq 'the key'
15
16
  field.title.should eq 'the title'
16
17
  field.value.should eq 'the value'
17
18
  end
19
+
20
+ it "should have a firstname if no lastname" do
21
+ @person.first_name = "Vincent"
22
+ @person.last_name = nil
23
+
24
+ error = @person.validate
25
+ error.should be_empty
26
+ end
27
+
28
+ it "should have a lastname if no firstname" do
29
+ @person.first_name = String.new
30
+ @person.last_name = "Vega"
31
+
32
+ error = @person.validate
33
+ error.should be_empty
34
+ end
35
+
36
+ it "shouldnt pass validation with no firstname and lastname" do
37
+ @person.first_name = String.new
38
+ @person.last_name = nil
39
+
40
+ error = @person.validate
41
+ error.should start_with("A firstname or lastname is required for person")
42
+ end
18
43
  end
19
44
 
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: 0.7.3
4
+ version: 0.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-11-29 00:00:00.000000000 Z
14
+ date: 2013-12-05 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: iso_country_codes