motion-addressbook 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- motion-addressbook (0.1.0)
4
+ motion-addressbook (0.1.1)
5
5
  bubble-wrap
6
6
 
7
7
  GEM
@@ -1,5 +1,5 @@
1
1
  module Motion
2
2
  module Addressbook
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -1,30 +1,98 @@
1
1
  module AddressBook
2
2
  class MultiValue
3
- attr_reader :multivalueIdentifier, :ab_multi_values
3
+ attr_reader :attributes, :ab_multi_values
4
4
 
5
- def initialize(ab_multi_values=nil)
6
- @ab_multi_values = ab_multi_values ? ABMultiValueCreateMutableCopy(ab_multi_values) : ABMultiValueCreateMutable(KABMultiStringPropertyType)
7
- # @ab_multi_values = ABMultiValueCreateMutable(KABMultiStringPropertyType)
5
+ def self.attribute_map
6
+ { :mobile => KABPersonPhoneMobileLabel ,
7
+ :iphone => KABPersonPhoneIPhoneLabel ,
8
+ :main => KABPersonPhoneMainLabel ,
9
+ :home_fax => KABPersonPhoneHomeFAXLabel,
10
+ :work_fax => KABPersonPhoneWorkFAXLabel,
11
+ :pager => KABPersonPhonePagerLabel ,
12
+ :work => KABWorkLabel ,
13
+ :home => KABHomeLabel ,
14
+ :other => KABOtherLabel
15
+ }
16
+ end
17
+ def attribute_map
18
+ self.class.attribute_map
19
+ end
20
+
21
+ def alex
22
+ ABMultiValueGetIdentifierAtIndex @ab_multi_values, 0
8
23
  end
9
24
 
10
- def set(ab_label, value)
11
- return if values.include? value #temp because blank? not working
12
- if blank?(ab_label)
13
- ABMultiValueAddValueAndLabel(@ab_multi_values, value, ab_label, multivalueIdentifier) unless value.nil?
25
+ def initialize(attributes={}, existing_ab_multi_values=nil)
26
+ @attributes = {}
27
+ if existing_ab_multi_values
28
+ @ab_multi_values = ABMultiValueCreateMutableCopy(existing_ab_multi_values)
29
+ load_attributes_from_ab
30
+ else
31
+ @ab_multi_values = ABMultiValueCreateMutable(KABMultiStringPropertyType)
32
+ end
33
+ attributes.each do |attribute, value|
34
+ send("#{attribute}=", value)
35
+ end
36
+ end
37
+
38
+ def method_missing(name, *args)
39
+ if attribute_name = getter?(name)
40
+ get(attribute_name)
41
+ elsif attribute_name = setter?(name)
42
+ set(attribute_name, args.first)
43
+ else
44
+ super
45
+ end
46
+ end
47
+
48
+ def self.is_attribute?(attribute_name)
49
+ return false if attribute_name.nil?
50
+ attribute_map.include?(attribute_name.to_sym) || [:email, :phone_number].include?( attribute_name.to_sym)
51
+ end
52
+
53
+ def getter?(method_name)
54
+ if self.class.is_attribute? method_name
55
+ method_name
56
+ else
57
+ nil
58
+ end
59
+ end
60
+ def setter?(method_name)
61
+ method_name.to_s =~ /^(\w*)=$/
62
+ if self.class.is_attribute? $1
63
+ $1
14
64
  else
15
- # untested!!!
16
- ABMultiValueReplaceValueAtIndex(@ab_multi_values, value, index_for(ab_label))
65
+ nil
17
66
  end
18
67
  end
19
68
 
20
- def set_many(new_values)
21
- new_values.each do |ab_label, value|
22
- set(ab_label, value)
69
+ def get(attribute_name)
70
+ attributes[attribute_name.to_sym] ||= get_field(attribute_map[attribute_name])
71
+ end
72
+
73
+ def set(attribute_name, value)
74
+ set_field(attribute_map[attribute_name.to_sym], value)
75
+ attributes[attribute_name.to_sym] = value
76
+ end
77
+
78
+ def set_field(ab_label, value)
79
+ if blank?(ab_label)
80
+ ABMultiValueAddValueAndLabel(@ab_multi_values, value, ab_label, nil) unless value.nil?
81
+ else
82
+ ABMultiValueReplaceValueAtIndex(@ab_multi_values, value, 0)
23
83
  end
24
84
  end
25
85
 
86
+ def get_field(ab_label)
87
+ puts
88
+ puts [__FILE__, __LINE__, ab_label].inspect
89
+ index = ABMultiValueGetIndexForIdentifier(@ab_multi_values, ab_label)
90
+ puts [__FILE__, __LINE__, index].inspect
91
+ ABMultiValueCopyValueAtIndex(@ab_multi_values, index)
92
+ end
93
+
26
94
  def values
27
- ABMultiValueCopyArrayOfAllValues(@ab_multi_values) || []
95
+ attributes.values
28
96
  end
29
97
 
30
98
  def include? value
@@ -32,14 +100,20 @@ module AddressBook
32
100
  values.include? value
33
101
  end
34
102
 
35
- private
36
- def blank?(ab_label)
37
- index_for(ab_label) == -1
103
+ def size
104
+ ABMultiValueGetCount(@ab_multi_values)
38
105
  end
39
106
 
40
- def index_for(ab_label)
41
- return -1 # Keep getting this error :( <ArgumentError: invalid value for Integer: "_$!<Work>!$_">
42
- # ABMultiValueGetIndexForIdentifier(@ab_multi_values, ab_label)
107
+ def load_attributes_from_ab
108
+ (0...size).to_a.each do |i|
109
+ label = ABMultiValueCopyLabelAtIndex(@ab_multi_values, i)
110
+ @attributes[attribute_map.invert[label]] = ABMultiValueCopyValueAtIndex(@ab_multi_values, i)
111
+ end
112
+ end
113
+
114
+ private
115
+ def blank?(ab_label)
116
+ attributes[ab_label].nil?
43
117
  end
44
118
 
45
119
  end
@@ -2,12 +2,12 @@ module AddressBook
2
2
  class Person
3
3
  attr_reader :attributes, :error, :ab_person
4
4
 
5
- def initialize(attributes={}, ab_person = nil)
5
+ def initialize(attributes={}, existing_ab_person = nil)
6
6
  @attributes = attributes
7
- if ab_person.nil?
7
+ if existing_ab_person.nil?
8
8
  @new_record = true
9
9
  else
10
- @ab_person = ab_person
10
+ @ab_person = existing_ab_person
11
11
  load_ab_person
12
12
  @new_record = false
13
13
  end
@@ -175,7 +175,38 @@ module AddressBook
175
175
  @attributes[:phone_number] ||= phone_number_values.first
176
176
  end
177
177
 
178
- # private
178
+ def ab_person
179
+ if @ab_person.nil?
180
+ @ab_person = ABPersonCreate()
181
+ load_ab_person
182
+ end
183
+ @ab_person
184
+ end
185
+
186
+ def find_or_new
187
+ if new_record?
188
+ new_ab_person
189
+ else
190
+ existing_record
191
+ end
192
+ end
193
+
194
+ def new_record?
195
+ !!@new_record
196
+ end
197
+ def exists?
198
+ !new_record?
199
+ end
200
+ def inspect
201
+ # ensure all attributes loaded
202
+ attribute_map.keys.each do |attribute|
203
+ self.send(attribute)
204
+ end
205
+
206
+ super
207
+ end
208
+
209
+ private
179
210
 
180
211
  def load_ab_person
181
212
  set_field(KABPersonFirstNameProperty, attributes[:first_name ]) unless attributes[:first_name ].nil?
@@ -183,8 +214,8 @@ module AddressBook
183
214
  set_field(KABPersonJobTitleProperty, attributes[:job_title ]) unless attributes[:job_title ].nil?
184
215
  set_field(KABPersonDepartmentProperty, attributes[:department ]) unless attributes[:department ].nil?
185
216
  set_field(KABPersonOrganizationProperty, attributes[:organization]) unless attributes[:organization].nil?
186
- set_multi_field(KABPersonPhoneProperty, KABPersonPhoneMobileLabel => attributes[:mobile_phone], KABWorkLabel => attributes[:office_phone])
187
- set_multi_field(KABPersonEmailProperty, KABWorkLabel => attributes[:email])
217
+ set_multi_field(KABPersonPhoneProperty, :mobile => attributes[:mobile_phone], :work => attributes[:office_phone])
218
+ set_multi_field(KABPersonEmailProperty, :work => attributes[:email])
188
219
  end
189
220
 
190
221
  def set_field(field, value)
@@ -195,38 +226,11 @@ module AddressBook
195
226
  end
196
227
 
197
228
  def set_multi_field(field, values)
198
- multi_field = MultiValue.new(ABRecordCopyValue(ab_person, field))
199
- multi_field.set_many(values)
229
+ multi_field = MultiValue.new(values, ABRecordCopyValue(ab_person, field))
200
230
  ABRecordSetValue(ab_person, field, multi_field.ab_multi_values, error )
201
231
  end
202
232
  def get_multi_field(field)
203
- MultiValue.new(ABRecordCopyValue(ab_person, field))
204
- end
205
-
206
- def ab_person
207
- if @ab_person.nil?
208
- @ab_person = new_ab_person
209
- load_ab_person
210
- end
211
- @ab_person
212
- end
213
-
214
- def find_or_new
215
- if new_record?
216
- new_ab_person
217
- else
218
- existing_record
219
- end
220
- end
221
-
222
- def new_ab_person
223
- ab_person = ABPersonCreate()
224
- # ABAddressBookAddRecord(address_book, ab_person, error )
225
- ab_person
226
- end
227
-
228
- def new_record?
229
- !!@new_record
233
+ MultiValue.new({}, ABRecordCopyValue(ab_person, field))
230
234
  end
231
235
 
232
236
  def existing_records
@@ -237,12 +241,6 @@ module AddressBook
237
241
  end
238
242
  end
239
243
 
240
- def exists?
241
- !new_record?
242
- end
243
- # def new_record?
244
- # existing_record.nil?
245
- # end
246
244
  def existing_record
247
245
  # what if there are more than one match? email should be unique but ...
248
246
  existing_records.first
@@ -252,17 +250,5 @@ module AddressBook
252
250
  @address_book ||= ABAddressBookCreate()
253
251
  end
254
252
 
255
- def inspect
256
- # ensure all attributes loaded
257
- attribute_map.keys.each do |attribute|
258
- self.send(attribute)
259
- end
260
-
261
- super
262
- end
263
-
264
- # def define_some_constants
265
- # [KABWorkLabel, KABOtherLabel, KABHomeLabel]
266
- # end
267
253
  end
268
254
  end
@@ -1,66 +1,125 @@
1
1
  describe AddressBook::MultiValue do
2
- describe 'new field' do
3
- before do
4
- @multi_value = AddressBook::MultiValue.new
2
+ describe 'properties on a new multivalue' do
3
+ describe 'initializing with values' do
4
+ before do
5
+ @attributes = { :mobile => '123-456-7890', :iphone => '222-333-4444', :main => '555-1212',
6
+ :home_fax => '1-617-555-8000', :work_fax => '1-212-555-0000', :pager => '99-9999-9999',
7
+ :work => 'alex@work.com', :home => 'alex@home.com', :other => 'alex@other.com'}
8
+ @multi_value = AddressBook::MultiValue.new @attributes
9
+ end
10
+
11
+ it 'should be able to get each of the single value fields' do
12
+ @multi_value.mobile.should.equal @attributes[:mobile ]
13
+ @multi_value.iphone.should.equal @attributes[:iphone ]
14
+ @multi_value.main.should.equal @attributes[:main ]
15
+ @multi_value.home_fax.should.equal @attributes[:home_fax]
16
+ @multi_value.work_fax.should.equal @attributes[:work_fax]
17
+ @multi_value.pager.should.equal @attributes[:pager ]
18
+ @multi_value.work.should.equal @attributes[:work ]
19
+ @multi_value.home.should.equal @attributes[:home ]
20
+ @multi_value.other.should.equal @attributes[:other ]
21
+ end
22
+
23
+ it 'should give all the values in a list' do
24
+ @multi_value.values.should == ["123-456-7890", "222-333-4444", "555-1212", "1-617-555-8000", "1-212-555-0000", "99-9999-9999", "alex@work.com", "alex@home.com", "alex@other.com"]
25
+ end
5
26
  end
6
27
 
7
- describe 'setting one value' do
8
- before { @multi_value.set(KABWorkLabel, 'alex@work.com') }
9
- it 'gives access to the values' do
10
- @multi_value.values.should == ['alex@work.com']
11
- end
12
- it 'should know if the value exists' do
13
- @multi_value.include?('alex@work.com').should.be.true
28
+ describe 'setting each field' do
29
+ before do
30
+ @multi_value = AddressBook::MultiValue.new
14
31
  end
15
32
 
16
- it 'should know if the value exists' do
17
- @multi_value.include?('not@there.com').should.be.false
33
+ it 'should be able to set the first name' do
34
+ @multi_value.mobile = '123456789'
35
+ @multi_value.mobile.should.equal '123456789'
18
36
  end
19
- end
20
- describe 'setting many values' do
21
- before { @multi_value.set_many(KABWorkLabel => 'alex@work.com', KABHomeLabel => 'alex@home.com') }
22
- it 'gives access to the values' do
23
- @multi_value.values.should == ['alex@work.com', 'alex@home.com']
37
+ it 'should be able to set the iphone' do
38
+ @multi_value.iphone = '123456789'
39
+ @multi_value.iphone.should.equal '123456789'
24
40
  end
25
- it 'should know both values exist' do
26
- @multi_value.include?('alex@work.com').should.be.true
27
- @multi_value.include?('alex@home.com').should.be.true
41
+ it 'should be able to set the main' do
42
+ @multi_value.main = '123456789'
43
+ @multi_value.main.should.equal '123456789'
28
44
  end
29
-
30
- it 'should know if the value exists' do
31
- @multi_value.include?('not@there.com').should.be.false
45
+ it 'should be able to set the home fax' do
46
+ @multi_value.home_fax = '123456789'
47
+ @multi_value.home_fax.should.equal '123456789'
48
+ end
49
+ it 'should be able to set the work fax' do
50
+ @multi_value.work_fax = '123456789'
51
+ @multi_value.work_fax.should.equal '123456789'
52
+ end
53
+ it 'should be able to set the pager' do
54
+ @multi_value.pager = '123456789'
55
+ @multi_value.pager.should.equal '123456789'
56
+ end
57
+ it 'should be able to set the work' do
58
+ @multi_value.work = 'a@work.com'
59
+ @multi_value.work.should.equal 'a@work.com'
60
+ end
61
+ it 'should be able to set the home' do
62
+ @multi_value.home = 'a@home.com'
63
+ @multi_value.home.should.equal 'a@home.com'
64
+ end
65
+ it 'should be able to set the other' do
66
+ @multi_value.other = 'a@other.com'
67
+ @multi_value.other.should.equal 'a@other.com'
32
68
  end
33
69
  end
34
70
  end
35
-
36
- describe 'existing field' do
71
+
72
+ describe 'an existing multivalue' do
37
73
  before do
38
- old_multi_value = AddressBook::MultiValue.new
39
- old_multi_value.set(KABWorkLabel, 'alex@work.com')
40
-
41
- @multi_value = AddressBook::MultiValue.new old_multi_value.ab_multi_values
42
- end
43
-
44
- it 'should have the value' do
45
- @multi_value.values.should == ['alex@work.com']
74
+ @first_multi_value = AddressBook::MultiValue.new :mobile => '123-456-7890', :iphone => '99-8888888-7777777-66'
75
+ @multi_value = AddressBook::MultiValue.new({:mobile => '987654321', :home_fax => '777-6666-4444'}, @first_multi_value.ab_multi_values)
46
76
  end
47
77
 
48
- it 'should be able to update the value' do
49
- @multi_value.set(KABWorkLabel, 'alex@new_work.com')
50
- puts '******PENDING****** Waiting for RubyMotion ABMultiValueGetIndexForIdentifier fix'
51
- @multi_value.values.should == ['alex@work.com', 'alex@new_work.com'] # ['alex@new_work.com']
78
+ it 'should ' do
79
+ @multi_value.mobile.should == '987654321'
80
+ @multi_value.iphone.should == '99-8888888-7777777-66'
81
+ @multi_value.home_fax.should == '777-6666-4444'
52
82
  end
83
+ end
53
84
 
54
- it 'should ignore when updating an existing value to the same value' do
55
- @multi_value.set(KABWorkLabel, 'alex@work.com')
56
- @multi_value.values.should == ['alex@work.com']
85
+ describe 'method missing magic' do
86
+ before do
87
+ @multi_value = AddressBook::MultiValue.new
88
+ end
89
+ describe 'getters' do
90
+ it 'should have a getter for each attribute' do
91
+ @multi_value.getter?('mobile' ).should.be truthy
92
+ @multi_value.getter?('iphone' ).should.be truthy
93
+ @multi_value.getter?('main' ).should.be truthy
94
+ @multi_value.getter?('home_fax').should.be truthy
95
+ @multi_value.getter?('work_fax').should.be truthy
96
+ @multi_value.getter?('pager' ).should.be truthy
97
+ @multi_value.getter?('work' ).should.be truthy
98
+ @multi_value.getter?('home' ).should.be truthy
99
+ @multi_value.getter?('other' ).should.be truthy
57
100
  end
58
-
59
- it 'should be able to add an additional value' do
60
- @multi_value.set(KABHomeLabel, 'alex@home.com')
61
- @multi_value.values.should == ['alex@work.com', 'alex@home.com']
101
+ it 'should know what is not a getter' do
102
+ @multi_value.getter?('nonesense').should.be falsey
103
+ @multi_value.getter?('mobile=' ).should.be falsey
62
104
  end
63
-
64
105
  end
65
-
106
+ describe 'setters' do
107
+ it 'should have a setter for each attribute' do
108
+ @multi_value.setter?('mobile=' ).should.be truthy
109
+ @multi_value.setter?('iphone=' ).should.be truthy
110
+ @multi_value.setter?('main=' ).should.be truthy
111
+ @multi_value.setter?('home_fax=').should.be truthy
112
+ @multi_value.setter?('work_fax=').should.be truthy
113
+ @multi_value.setter?('pager=' ).should.be truthy
114
+ @multi_value.setter?('work=' ).should.be truthy
115
+ @multi_value.setter?('home=' ).should.be truthy
116
+ @multi_value.setter?('other=' ).should.be truthy
117
+ end
118
+ it 'should know what is not a setter' do
119
+ @multi_value.setter?('nonesense=').should.be falsey
120
+ @multi_value.setter?('mobile' ).should.be falsey
121
+ end
122
+ end
123
+ end
124
+
66
125
  end
@@ -1,49 +1,31 @@
1
- def unique_email
2
- "alex_#{Time.now.to_i}@example.com"
3
- end
4
1
  describe AddressBook::Person do
5
2
 
6
- describe '.all' do
7
- before do
8
- @person = AddressBook::Person.create({:first_name => 'Alex', :last_name=>'Rothenberg'})
9
- end
10
- it 'should have the person we created' do
11
- all_names = AddressBook::Person.all.map do |person|
12
- [person.first_name, person.last_name]
13
- end
14
- all_names.should.include? ['Alex', 'Rothenberg']
15
- end
16
-
17
- it 'should get bigger when we create another' do
18
- initial_people_count = AddressBook::Person.all.size
19
- @person = AddressBook::Person.create({:first_name => 'Alex2', :last_name=>'Rothenberg2'})
20
- AddressBook::Person.all.size.should == (initial_people_count + 1)
21
- end
22
- end
23
-
24
3
  describe 'ways of creating and finding people' do
25
4
  describe 'new' do
26
5
  before do
27
- @alex = AddressBook::Person.new(:first_name => 'Alex', :last_name => 'Testy', :email => 'alex@example.com')
6
+ @alex = AddressBook::Person.new(:first_name => 'Alex', :last_name => 'Testy', :email => 'alex_testy@example.com')
28
7
  end
29
8
  it 'should create but not save in the address book' do
30
9
  @alex.should.be.new_record
31
10
  @alex.first_name.should == 'Alex'
32
11
  @alex.last_name.should == 'Testy'
33
- @alex.email_values.should == ['alex@example.com']
12
+ @alex.email_values.should == ['alex_testy@example.com']
34
13
  end
35
14
  end
36
-
15
+
37
16
  describe 'existing' do
38
17
  before do
39
- AddressBook::Person.create(:first_name => 'Alex', :last_name => 'Testy', :email => 'alex@example.com')
18
+ @email = unique_email
19
+ @alex = AddressBook::Person.create(:first_name => 'Alex', :last_name => 'Testy', :email => @email)
40
20
  end
41
21
  describe '.find_by_all_email' do
42
22
  it 'should find matches' do
43
- alexes = AddressBook::Person.find_all_by_email 'alex@example.com'
23
+ alexes = AddressBook::Person.find_all_by_email @email
44
24
  alexes.should.not.be.empty
45
25
  alexes.each do |alex|
46
- alex.email.should == 'alex@example.com'
26
+ alex.email.should == @email
27
+ alex.first_name.should == 'Alex'
28
+ alex.last_name.should == 'Testy'
47
29
  end
48
30
  end
49
31
  it 'should give empty list when nothing matches' do
@@ -53,8 +35,10 @@ describe AddressBook::Person do
53
35
  end
54
36
  describe '.find_by_email' do
55
37
  it 'should find match' do
56
- alex = AddressBook::Person.find_by_email 'alex@example.com'
57
- alex.email.should == 'alex@example.com'
38
+ alex = AddressBook::Person.find_by_email @email
39
+ alex.email.should == @email
40
+ alex.first_name.should == 'Alex'
41
+ alex.last_name.should == 'Testy'
58
42
  end
59
43
  it 'should give empty list when nothing matches' do
60
44
  alexes = AddressBook::Person.find_by_email unique_email
@@ -63,10 +47,12 @@ describe AddressBook::Person do
63
47
  end
64
48
  describe '.where' do
65
49
  it 'should find matches' do
66
- alexes = AddressBook::Person.where(:email => 'alex@example.com')
50
+ alexes = AddressBook::Person.where(:email => @email)
67
51
  alexes.should.not.be.empty
68
52
  alexes.each do |alex|
69
- alex.email.should == 'alex@example.com'
53
+ alex.email.should == @email
54
+ alex.first_name.should == 'Alex'
55
+ alex.last_name.should == 'Testy'
70
56
  end
71
57
  end
72
58
  it 'should give empty list when nothing matches' do
@@ -74,27 +60,43 @@ describe AddressBook::Person do
74
60
  alexes.should == []
75
61
  end
76
62
  end
63
+
64
+ describe '.all' do
65
+ it 'should have the person we created' do
66
+ all_names = AddressBook::Person.all.map do |person|
67
+ [person.first_name, person.last_name]
68
+ end
69
+ all_names.should.include? [@alex.first_name, @alex.last_name]
70
+ end
71
+
72
+ it 'should get bigger when we create another' do
73
+ initial_people_count = AddressBook::Person.all.size
74
+ @person = AddressBook::Person.create({:first_name => 'Alex2', :last_name=>'Rothenberg2'})
75
+ AddressBook::Person.all.size.should == (initial_people_count + 1)
76
+ end
77
+ end
77
78
  end
78
79
 
79
- # describe 'new or existing' do
80
- # before do
81
- # AddressBook::Person.create(:first_name => 'Alex', :last_name => 'Testy', :email => 'alex@example.com')
82
- # end
83
- # it 'should find an existing person' do
84
- # alex = AddressBook::Person.find_or_new_by_email('alex@example.com')
85
- # alex.should.not.be.new_record
86
- # alex.email.should == 'alex@example.com'
87
- # alex.first_name.should == 'Alex'
88
- # alex.last_name.should == 'Testy'
89
- # end
90
- # it 'should return new person when no match found' do
91
- # never_before_used_email = unique_email
92
- # alex = AddressBook::Person.find_or_new_by_email(never_before_used_email)
93
- # alex.should.be.new_record
94
- # alex.email.should == never_before_used_email
95
- # alex.first_name.should == nil
96
- # end
97
- # end
80
+ describe '.find_or_new_by_XXX - new or existing' do
81
+ before do
82
+ @email = unique_email
83
+ AddressBook::Person.create(:first_name => 'Alex', :last_name => 'Testy', :email => @email)
84
+ end
85
+ it 'should find an existing person' do
86
+ alex = AddressBook::Person.find_or_new_by_email(@email)
87
+ alex.should.not.be.new_record
88
+ alex.email.should == @email
89
+ alex.first_name.should == 'Alex'
90
+ alex.last_name.should == 'Testy'
91
+ end
92
+ it 'should return new person when no match found' do
93
+ never_before_used_email = unique_email
94
+ alex = AddressBook::Person.find_or_new_by_email(never_before_used_email)
95
+ alex.should.be.new_record
96
+ alex.email.should == never_before_used_email
97
+ alex.first_name.should == nil
98
+ end
99
+ end
98
100
  end
99
101
 
100
102
  describe 'save' do
@@ -105,17 +107,17 @@ describe AddressBook::Person do
105
107
  :email => unique_email
106
108
  }
107
109
  end
108
-
110
+
109
111
  describe 'a new person' do
110
112
  before do
111
113
  @ab_person = AddressBook::Person.new(@attributes)
112
114
  end
113
-
115
+
114
116
  it 'should not be existing' do
115
117
  @ab_person.should.be.new_record
116
118
  @ab_person.should.not.be.exists
117
119
  end
118
-
120
+
119
121
  it 'should be able to get each of the single value fields' do
120
122
  @ab_person.first_name.should.equal @attributes[:first_name ]
121
123
  @ab_person.last_name.should.equal @attributes[:last_name ]
@@ -123,7 +125,7 @@ describe AddressBook::Person do
123
125
  @ab_person.department.should.equal @attributes[:department ]
124
126
  @ab_person.organization.should.equal @attributes[:organization]
125
127
  end
126
-
128
+
127
129
  describe 'setting each field' do
128
130
  it 'should be able to set the first name' do
129
131
  @ab_person.first_name = 'new first name'
@@ -145,7 +147,7 @@ describe AddressBook::Person do
145
147
  @ab_person.organization = 'new organization'
146
148
  @ab_person.organization.should.equal 'new organization'
147
149
  end
148
-
150
+
149
151
  it 'should be able to set the phot' do
150
152
  image = CIImage.emptyImage
151
153
  data = UIImagePNGRepresentation(UIImage.imageWithCIImage image)
@@ -153,11 +155,11 @@ describe AddressBook::Person do
153
155
  UIImagePNGRepresentation(@ab_person.photo).should.equal data
154
156
  end
155
157
  end
156
-
158
+
157
159
  it 'should be able to get the phone numbers' do
158
160
  @ab_person.phone_number_values.should.equal [@attributes[:mobile_phone], @attributes[:office_phone] ]
159
161
  end
160
-
162
+
161
163
  it 'should be able to get the emails' do
162
164
  @ab_person.email_values.should.equal [@attributes[:email] ]
163
165
  end
@@ -171,22 +173,23 @@ describe AddressBook::Person do
171
173
  end
172
174
  end
173
175
  end
174
-
176
+
175
177
  describe 'updating an existing person' do
176
178
  before do
177
179
  AddressBook::Person.new(@attributes).save
178
180
  @attributes[:job_title ] = 'i got promoted'
179
181
  @attributes[:office_phone] = '111 222 3333'
180
- # @attributes[:department ] = nil
182
+ @attributes[:department ] = nil
181
183
  @ab_person = AddressBook::Person.find_or_new_by_email(@attributes[:email])
182
184
  end
183
-
185
+
184
186
  it 'should know it is not new' do
185
187
  @ab_person.should.not.be.new_record
186
188
  @ab_person.should.be.exists
189
+ @ab_person.first_name.should == 'Alex'
187
190
  @ab_person.department.should == 'Development'
188
191
  end
189
-
192
+
190
193
  describe 'updating' do
191
194
  it 'should be able to get each of the single value fields' do
192
195
  @ab_person.save
@@ -196,11 +199,11 @@ describe AddressBook::Person do
196
199
  AddressBook::Person.find_by_email(@ab_person.email).first_name.should == 'New First Name'
197
200
  end
198
201
  end
199
-
202
+
200
203
  end
201
-
204
+
202
205
  end
203
-
206
+
204
207
  describe 'method missing magic' do
205
208
  before do
206
209
  @person = AddressBook::Person.new
@@ -265,6 +268,5 @@ describe AddressBook::Person do
265
268
  AddressBook::Person.first_finder?('find_all_by_email').should.be falsey
266
269
  end
267
270
  end
268
- end
269
-
271
+ end
270
272
  end
@@ -0,0 +1,3 @@
1
+ # The value of these constants is undefined until one of the following functions has been called: ABAddressBookCreate, ABPersonCreate, ABGroupCreate.
2
+ # see https://developer.apple.com/library/ios/#documentation/AddressBook/Reference/ABPersonRef_iPhoneOS/Reference/reference.html and search for "Discussion"
3
+ ABAddressBookCreate()
@@ -1,5 +1,4 @@
1
- def unique_email(email='alex@example.com')
1
+ def unique_email(email='alex_testy@example.com')
2
2
  name, domain = email.split('@')
3
- "#{name}_#{Time.now.to_i}@#{domain}"
3
+ "#{name}_#{Time.now.to_i}_#{rand(1000)}@#{domain}"
4
4
  end
5
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-addressbook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-02 00:00:00.000000000 Z
12
+ date: 2012-08-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bubble-wrap
@@ -80,6 +80,7 @@ files:
80
80
  - spec/address_book/multi_value_spec.rb
81
81
  - spec/address_book/person_spec.rb
82
82
  - spec/helpers/bacon_matchers.rb
83
+ - spec/helpers/hacks.rb
83
84
  - spec/helpers/person_helpers.rb
84
85
  homepage: ''
85
86
  licenses: []
@@ -95,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
96
  version: '0'
96
97
  segments:
97
98
  - 0
98
- hash: 2222188653506619315
99
+ hash: 2733254916178873065
99
100
  required_rubygems_version: !ruby/object:Gem::Requirement
100
101
  none: false
101
102
  requirements:
@@ -104,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
105
  version: '0'
105
106
  segments:
106
107
  - 0
107
- hash: 2222188653506619315
108
+ hash: 2733254916178873065
108
109
  requirements: []
109
110
  rubyforge_project:
110
111
  rubygems_version: 1.8.24
@@ -115,4 +116,5 @@ test_files:
115
116
  - spec/address_book/multi_value_spec.rb
116
117
  - spec/address_book/person_spec.rb
117
118
  - spec/helpers/bacon_matchers.rb
119
+ - spec/helpers/hacks.rb
118
120
  - spec/helpers/person_helpers.rb