sugarcube 0.20.1 → 0.20.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -514,6 +514,7 @@ NSError.new('Error Message', code: 404, userInfo: { warnings: ['blabla'] })
514
514
  # file operations
515
515
  "my.plist".exists? # => NSFileManager.defaultManager.fileExistsAtPath("my.plist")
516
516
  "my.plist".document # => NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0].stringByAppendingPathComponent("my.plist")
517
+ "my.plist".cache # => NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true)[0].stringByAppendingPathComponent("my.plist")
517
518
  "my.plist".remove! # => NSFileManager.defaultManager.removeItemAtPath("my.plist".document, error: error) (returns error, if any occurred)
518
519
 
519
520
  # get the resource path, useful if you include json files or images you manipulate in the app
@@ -1184,6 +1185,16 @@ UIBarButtonItem.imaged(['portrate'.uiimage, 'landscape'.uiimage) {
1184
1185
  UIBarButtonItem.alloc.initWithImage('portrate'.uiimage, landscapeImagePhone:'landscape'.uiimage, style: :bordered.uibarbuttonstyle, target:self, action:"action:")
1185
1186
  ```
1186
1187
 
1188
+ Example Usage:
1189
+ ```ruby
1190
+ toolbar = UIToolbar.new
1191
+ toolbar.items = [
1192
+ @image_picker_button = UIBarButtonItem.camera { presentImagePickerController(self) },
1193
+ UIBarButtonItem.flexiblespace,
1194
+ @saveButton = UIBarButtonItem.save { save_photo(self) }
1195
+ ]
1196
+ ```
1197
+
1187
1198
  NSNotificationCenter
1188
1199
  ----------------------
1189
1200
 
@@ -1208,15 +1219,24 @@ Makes it easy to post a notification to some or all objects.
1208
1219
  ---------
1209
1220
 
1210
1221
  ```ruby
1222
+ # once
1211
1223
  1.second.later do
1212
1224
  @view.shake
1213
1225
  end
1214
-
1226
+ # repeating
1215
1227
  1.second.every do
1216
1228
  @view.shake
1217
1229
  end
1218
1230
 
1219
- # since that looks funny, an every method is available in the SugarCube::Timer module
1231
+ # you can assign the return value (an NSTimer)
1232
+ timer = 1.second.every do
1233
+ @view.shake
1234
+ end
1235
+ # and invalidate it
1236
+ timer.invalidate
1237
+
1238
+ # the `every` method is available in the SugarCube::Timer module,
1239
+ # which you might find more readable
1220
1240
  include SugarCube::Timer
1221
1241
  every 1.minute do
1222
1242
  puts "tick"
@@ -26,13 +26,14 @@ module SugarCube
26
26
  key = symbol
27
27
  key = symbol.to_s unless self.include? key
28
28
  if self.include?(key)
29
+ self[key] = self[key].to_object
29
30
  return self[key]
30
31
  end
31
32
  elsif args.size == 1 && /(.*)=$/ =~ symbol.to_s
32
33
  key = $1.to_sym
33
34
  key = key.to_s unless self.include? key
34
35
  if self.include?(key)
35
- return self[key] = args.first
36
+ return self[key] = args.first.to_object
36
37
  end
37
38
  end
38
39
  return super
@@ -42,10 +43,24 @@ module SugarCube
42
43
  self
43
44
  end
44
45
 
46
+ # replace enumerable methods
47
+ alias :anonymous_each :each
48
+
49
+ def each
50
+ anonymous_each {|k,v| yield k, v.to_object}
51
+ end
52
+
45
53
  end
46
54
 
47
55
  end
48
56
 
57
+ class NSObject
58
+
59
+ def to_object
60
+ self
61
+ end
62
+
63
+ end
49
64
 
50
65
  class NSDictionary
51
66
 
@@ -0,0 +1,46 @@
1
+ module SugarCube
2
+ class AnonymousArray < Array
3
+
4
+ def [] index
5
+ o = super.to_object
6
+ self[index] = o
7
+ end
8
+
9
+ def first
10
+ self.size == 0 ? nil : self[0]
11
+ end
12
+
13
+ def last
14
+ c = self.size
15
+ c == 0 ? nil : self[c - 1]
16
+ end
17
+
18
+ def to_object
19
+ self
20
+ end
21
+
22
+
23
+ # replace enumerable methods
24
+ #
25
+ # In AnonymousArray:
26
+ # It was not satisfied with the :each method only.
27
+
28
+ [:each, :map, :collect].each do |method|
29
+ base_method = "anonymousarray_#{method.to_s}".to_sym
30
+ alias_method base_method, method
31
+ define_method method do
32
+ send(base_method) {|o| yield o.to_object}
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+
40
+ class NSArray
41
+
42
+ def to_object
43
+ SugarCube::AnonymousArray.new(self)
44
+ end
45
+
46
+ end
@@ -49,5 +49,9 @@ class NSArray
49
49
  blue = self[2] / 255.0
50
50
  UIColor.colorWithRed(red, green:green, blue:blue, alpha:alpha.to_f)
51
51
  end
52
+
53
+ def nsset
54
+ NSSet.setWithArray self
55
+ end
52
56
 
53
57
  end
@@ -3,7 +3,9 @@ class NSDate
3
3
  # a string using string_with_format's templating, and strings are concatenated
4
4
  # as-is
5
5
  SugarCubeFormats = {
6
- iso8601: [:yyyy, '-', :MM, '-', :dd, ' ', :'HH:mm:ss.SSS']
6
+ iso8601: [:yyyy, '-', :MM, '-', :dd, ' ', :'HH:mm:ss.SSS'],
7
+ ymd: [:yyyy, '-', :MM, '-', :dd],
8
+ hms: [:'HH:mm:ss.SSS'],
7
9
  }
8
10
 
9
11
  def self.from_components(components)
@@ -15,6 +17,18 @@ class NSDate
15
17
  return calendar.dateFromComponents(date_components)
16
18
  end
17
19
 
20
+ def self.today
21
+ NSDate.new.start_of_day
22
+ end
23
+
24
+ def self.tomorrow
25
+ NSDate.new.delta(days: 1).start_of_day
26
+ end
27
+
28
+ def self.yesterday
29
+ NSDate.new.delta(days: -1).start_of_day
30
+ end
31
+
18
32
  def string_with_style(date_style=NSDateFormatterMediumStyle,time_style=NSDateFormatterNoStyle)
19
33
  date_formatter = NSDateFormatter.new
20
34
  date_style = date_style.nsdatestyle if date_style.is_a? Symbol
@@ -6,6 +6,13 @@ class NSString
6
6
 
7
7
  @@sugarcube_docs.stringByAppendingPathComponent(self)
8
8
  end
9
+
10
+ def cache
11
+ @@sugarcube_caches ||= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true)[0]
12
+ return self if self.hasPrefix(@@sugarcube_caches)
13
+
14
+ @@sugarcube_caches.stringByAppendingPathComponent(self)
15
+ end
9
16
 
10
17
  def exists?
11
18
  NSFileManager.defaultManager.fileExistsAtPath(self.document)
@@ -8,4 +8,8 @@ class NSURL
8
8
  UIApplication.sharedApplication.canOpenURL(self)
9
9
  end
10
10
 
11
+ def nsdata
12
+ NSData.dataWithContentsOfURL(self)
13
+ end
14
+
11
15
  end
@@ -3,5 +3,7 @@ class NSSet
3
3
  def to_s
4
4
  "NSSet(#{self.count}){#{self.allObjects.map{|o| o.inspect}.join(', ')}}"
5
5
  end
6
+
7
+ alias :to_a :allObjects
6
8
 
7
9
  end
@@ -2,7 +2,6 @@ class UIViewController
2
2
 
3
3
  def push(view_controller)
4
4
  self.addChildViewController(view_controller)
5
- view_controller.didMoveToParentViewController(self)
6
5
  self
7
6
  end
8
7
 
@@ -12,6 +11,11 @@ class UIViewController
12
11
  push view_controller
13
12
  end
14
13
 
14
+ def pop
15
+ to_pop = self.childViewControllers[-1]
16
+ to_pop.removeFromParentViewController
17
+ end
18
+
15
19
  end
16
20
 
17
21
 
@@ -1,3 +1,3 @@
1
1
  module SugarCube
2
- Version = '0.20.1'
2
+ Version = '0.20.3'
3
3
  end
@@ -59,3 +59,163 @@ describe "SugarCube::Anonymous" do
59
59
  end
60
60
 
61
61
  end
62
+
63
+
64
+ describe "SugarCube::Anonymous nested case" do
65
+
66
+ before do
67
+ @h = SugarCube::Anonymous[
68
+ {
69
+ dictionary: { foo: 'FOO', array: [ { is_fine: 'is_fine' } ] },
70
+ array: [ { bar: 'BAR' }, [ 1, 2, 3 ] ]
71
+ }
72
+ ]
73
+ end
74
+
75
+ describe "dictionary" do
76
+
77
+ it 'should return an instance of SugarCube::Anonymous' do
78
+ @h.dictionary.class.should == SugarCube::Anonymous
79
+ end
80
+
81
+ it 'should return a hash which contains a string and an array' do
82
+ @h.dictionary.should == { foo: 'FOO', array: [ { is_fine: 'is_fine' } ] }
83
+ end
84
+
85
+ it 'should not copy again' do
86
+ @h.dictionary.object_id.should == @h.dictionary.object_id
87
+ end
88
+
89
+ it 'should return is_fine' do
90
+ @h.dictionary.array[0].is_fine.should == 'is_fine'
91
+ end
92
+
93
+ describe "Enumerable" do
94
+
95
+ describe "each" do
96
+
97
+ it 'should return an instance of [SugarCube::Anonymous, SugarCube::AnonymousArray]' do
98
+ a = []
99
+ @h.each{|k,v| a << v.class}
100
+ a[0].should == SugarCube::Anonymous
101
+ a[1].should == SugarCube::AnonymousArray
102
+ end
103
+
104
+ end
105
+
106
+ describe "map" do
107
+
108
+ it 'should return an instance of [SugarCube::Anonymous, SugarCube::AnonymousArray]' do
109
+ a = @h.map{|k,v| v.class}
110
+ a[0].should == SugarCube::Anonymous
111
+ a[1].should == SugarCube::AnonymousArray
112
+ end
113
+
114
+ end
115
+
116
+ describe "collect" do
117
+
118
+ it 'should return an instance of [SugarCube::Anonymous, SugarCube::AnonymousArray]' do
119
+ a = @h.collect{|k,v| v.class}
120
+ a[0].should == SugarCube::Anonymous
121
+ a[1].should == SugarCube::AnonymousArray
122
+ end
123
+
124
+ end
125
+
126
+ end
127
+
128
+ end
129
+
130
+ describe "array" do
131
+
132
+ it 'should return an instance of SugarCube::AnonymousArray' do
133
+ @h.array.class.should == SugarCube::AnonymousArray
134
+ end
135
+
136
+ it 'should not copy again' do
137
+ @h.array.object_id.should == @h.array.object_id
138
+ end
139
+
140
+ describe "inner object" do
141
+
142
+ it 'should return an instance of SugarCube::Anonymous' do
143
+ @h.array[0].class.should == SugarCube::Anonymous
144
+ end
145
+
146
+ it 'should return an instance of SugarCube::Anonymous' do
147
+ @h.array.first.class.should == SugarCube::Anonymous
148
+ end
149
+
150
+ it 'should return an instance of SugarCube::AnonymousArray' do
151
+ @h.array[1].class.should == SugarCube::AnonymousArray
152
+ end
153
+
154
+ it 'should return an instance of SugarCube::AnonymousArray' do
155
+ @h.array.last.class.should == SugarCube::AnonymousArray
156
+ end
157
+
158
+ end
159
+
160
+ describe "Enumerable" do
161
+
162
+ describe "each" do
163
+
164
+ it 'should return an instance of [SugarCube::Anonymous, SugarCube::AnonymousArray]' do
165
+ a = []
166
+ @h.array.each{|e| a << e.class}
167
+ a[0].should == SugarCube::Anonymous
168
+ a[1].should == SugarCube::AnonymousArray
169
+ end
170
+
171
+ end
172
+
173
+ describe "each_with_index" do
174
+
175
+ it 'should return an instance of [0,1]' do
176
+ a = []
177
+ @h.array.each_with_index{|e,i| a << i}
178
+ a.should == [0,1]
179
+ end
180
+
181
+ end
182
+
183
+ describe "map" do
184
+
185
+ it 'should return an instance of [SugarCube::Anonymous, SugarCube::AnonymousArray]' do
186
+ a = @h.array.map{|e| e.class}
187
+ a[0].should == SugarCube::Anonymous
188
+ a[1].should == SugarCube::AnonymousArray
189
+ end
190
+
191
+ end
192
+
193
+ describe "collect" do
194
+
195
+ it 'should return an instance of [SugarCube::Anonymous, SugarCube::AnonymousArray]' do
196
+ a = @h.array.collect{|e| e.class}
197
+ a[0].should == SugarCube::Anonymous
198
+ a[1].should == SugarCube::AnonymousArray
199
+ end
200
+
201
+ end
202
+
203
+ describe "find" do
204
+
205
+ it 'should return then first object' do
206
+ r = @h.array.find{|e| e.is_a? Hash}
207
+ r.should == @h.array[0]
208
+ end
209
+
210
+ it 'should return the second object' do
211
+ r = @h.array.find{|e| e.is_a? Array}
212
+ r.should == @h.array[1]
213
+ end
214
+
215
+ end
216
+
217
+ end
218
+
219
+ end
220
+
221
+ end
data/spec/nsarray_spec.rb CHANGED
@@ -42,5 +42,10 @@ describe "NSArray" do
42
42
  color.blue.should == 0
43
43
  color.alpha.should == 0.5
44
44
  end
45
+
46
+ it "should have a method #nsset" do
47
+ set = [0,1,0].nsset
48
+ set.count.should == 2
49
+ end
45
50
 
46
51
  end
data/spec/nsdate_spec.rb CHANGED
@@ -7,6 +7,37 @@ describe "NSDate" do
7
7
  date.day.should == 2
8
8
  end
9
9
 
10
+ it "Should have an NSDate##today method" do
11
+ now = NSDate.new
12
+ date = NSDate.today
13
+ date.year.should == now.year
14
+ date.month.should == now.month
15
+ date.day.should == now.day
16
+ date.hour.should == 0
17
+ date.min.should == 0
18
+ date.sec.should == 0
19
+ end
20
+
21
+ it "Should have an NSDate##yesterday method" do
22
+ now = NSDate.today
23
+ date = NSDate.yesterday
24
+ date.hour.should == 0
25
+ date.min.should == 0
26
+ date.sec.should == 0
27
+ (now - date).in_hours.should >= 23 # keep in mind daylight
28
+ (now - date).in_hours.should <= 25 # savings time.
29
+ end
30
+
31
+ it "Should have an NSDate##tomorrow method" do
32
+ now = NSDate.today
33
+ date = NSDate.tomorrow
34
+ date.hour.should == 0
35
+ date.min.should == 0
36
+ date.sec.should == 0
37
+ (date - now).in_hours.should >= 23 # keep in mind daylight
38
+ (date - now).in_hours.should <= 25 # savings time.
39
+ end
40
+
10
41
  before do
11
42
  @date = NSDate.from_components(year: 2013, month: 1, day: 2, hour:12, minute: 15, second: 30)
12
43
  end
@@ -0,0 +1,7 @@
1
+ describe "NSSet" do
2
+
3
+ it "should have a method #to_a" do
4
+ [0,1].nsset.to_a.should == [0,1]
5
+ end
6
+
7
+ end
@@ -2,6 +2,12 @@ describe 'NSString' do
2
2
 
3
3
  it "should have a #document method" do
4
4
  'foo'.document.hasPrefix('/Users').should == true
5
+ 'foo'.document.hasSuffix('Documents/foo').should == true
6
+ end
7
+
8
+ it "should have a #cache method" do
9
+ 'foo'.cache.hasPrefix('/Users').should == true
10
+ 'foo'.cache.hasSuffix('Library/Caches/foo').should == true
5
11
  end
6
12
 
7
13
  it "should have an #exists? method" do
data/spec/nsurl_spec.rb CHANGED
@@ -8,4 +8,8 @@ describe "NSURL" do
8
8
  'https://github.com'.nsurl.can_open?.should == true
9
9
  end
10
10
 
11
+ it "should have a method #nsdata?" do
12
+ NSData.should === 'https://github.com'.nsurl.nsdata
13
+ end
14
+
11
15
  end
@@ -1,19 +1,31 @@
1
1
  describe 'UIViewController' do
2
2
 
3
+ before do
4
+ @root_controller = UIViewController.new
5
+ @second_controller = UIViewController.new
6
+ @third_controller = UIViewController.new
7
+ end
8
+
3
9
  it 'should have `push` method' do
4
- controller = UIViewController.new
5
- controller.push UIViewController.new
6
- controller.childViewControllers.length.should == 1
7
- controller.push UIViewController.new
8
- controller.childViewControllers.length.should == 2
10
+ @root_controller.push @second_controller
11
+ @root_controller.childViewControllers.length.should == 1
12
+ @root_controller.push @third_controller
13
+ @root_controller.childViewControllers.length.should == 2
9
14
  end
10
15
 
11
16
  it 'should have `<<` method' do
12
- controller = UIViewController.new
13
- controller << UIViewController.new
14
- controller.childViewControllers.length.should == 1
15
- controller << UIViewController.new
16
- controller.childViewControllers.length.should == 2
17
+ @root_controller << @second_controller
18
+ @root_controller.childViewControllers.length.should == 1
19
+ @root_controller << @third_controller
20
+ @root_controller.childViewControllers.length.should == 2
21
+ end
22
+
23
+ it 'should have `pop()` method' do
24
+ @root_controller.push(@second_controller)
25
+ length = @root_controller.childViewControllers.length
26
+ popped = @root_controller.pop
27
+ @root_controller.childViewControllers.length.should == length - 1
28
+ popped.should == @second_controller
17
29
  end
18
30
 
19
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sugarcube
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.1
4
+ version: 0.20.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2013-03-22 00:00:00.000000000 Z
16
+ date: 2013-04-01 00:00:00.000000000 Z
17
17
  dependencies: []
18
18
  description: ! '== Description
19
19
 
@@ -54,6 +54,7 @@ files:
54
54
  - lib/sugarcube-568/uiimage.rb
55
55
  - lib/sugarcube-anonymous.rb
56
56
  - lib/sugarcube-anonymous/anonymous.rb
57
+ - lib/sugarcube-anonymous/anonymous_array.rb
57
58
  - lib/sugarcube-attributedstring.rb
58
59
  - lib/sugarcube-attributedstring/nsattributedstring.rb
59
60
  - lib/sugarcube-gestures.rb
@@ -144,6 +145,7 @@ files:
144
145
  - spec/nsdate_delta_spec.rb
145
146
  - spec/nsdate_spec.rb
146
147
  - spec/nsdate_upto_spec.rb
148
+ - spec/nsset_spec.rb
147
149
  - spec/nsstring_files_spec.rb
148
150
  - spec/nsstring_spec.rb
149
151
  - spec/nsurl_spec.rb
@@ -207,6 +209,7 @@ test_files:
207
209
  - spec/nsdate_delta_spec.rb
208
210
  - spec/nsdate_spec.rb
209
211
  - spec/nsdate_upto_spec.rb
212
+ - spec/nsset_spec.rb
210
213
  - spec/nsstring_files_spec.rb
211
214
  - spec/nsstring_spec.rb
212
215
  - spec/nsurl_spec.rb