sugarcube 1.4.2 → 1.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +22 -13
- data/lib/sugarcube-events/uicontrol.rb +12 -5
- data/lib/sugarcube-events/uitextview.rb +40 -6
- data/lib/sugarcube/version.rb +1 -1
- data/spec/nsstring_files_spec.rb +54 -8
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -306,32 +306,41 @@ three `:touch` events, calling `button.off(:touch)` will remove all three.
|
|
306
306
|
UITextView
|
307
307
|
------------
|
308
308
|
|
309
|
-
|
310
|
-
|
309
|
+
These handlers are functionally identical in usage to the same methods in
|
310
|
+
`UIControl`. They use the `NSNotificationCenter#addObserverForName:object:queue:usingBlock:`
|
311
|
+
method, but you do not have to worry about un-observing. When the text view is
|
312
|
+
released, these observers will be removed.
|
311
313
|
|
312
|
-
There are two aliases for each event
|
313
|
-
|
314
|
+
There are two aliases for each event, or you can use the event notification. I
|
315
|
+
prefer the present tense (jQuery-style `on :change`), but UIKit prefers past
|
316
|
+
simple (`:editing_did_begin`). The notifications, on the other hand, are in
|
317
|
+
present simple (`UITextViewTextDidBeginEditingNotification`). Whatever floats
|
318
|
+
your boat.
|
314
319
|
|
315
|
-
|
320
|
+
Anyway, these are all the same:
|
316
321
|
|
317
|
-
:editing_did_begin
|
318
|
-
:editing_did_change
|
319
|
-
:editing_did_end
|
322
|
+
:editing_did_begin :begin UITextViewTextDidBeginEditingNotification
|
323
|
+
:editing_did_change :change UITextViewTextDidChangeNotification
|
324
|
+
:editing_did_end :end UITextViewTextDidEndEditingNotification
|
320
325
|
|
321
326
|
```ruby
|
322
327
|
text_view = UITextView.new
|
323
|
-
text_view.on :begin do
|
328
|
+
text_view.on :begin do |notification| # <= you have to accept the notification in your block
|
324
329
|
p 'wait for it...'
|
325
330
|
end
|
326
|
-
text_view.on :change do
|
331
|
+
text_view.on :change do |notification|
|
327
332
|
p text_view.text
|
328
333
|
end
|
329
|
-
text_view.on :end do
|
334
|
+
text_view.on :end do |notification|
|
330
335
|
p 'done!'
|
331
336
|
end
|
332
337
|
|
333
|
-
#
|
334
|
-
text_view.off :editing_did_change
|
338
|
+
# if you want to remove the block, use the off method
|
339
|
+
text_view.off :editing_did_change
|
340
|
+
# or
|
341
|
+
text_view.off :change
|
342
|
+
# or
|
343
|
+
text_view.off UITextViewTextDidChangeNotification
|
335
344
|
```
|
336
345
|
|
337
346
|
Gestures
|
@@ -16,8 +16,8 @@ class UIControl
|
|
16
16
|
events.each do |event|
|
17
17
|
event = event.uicontrolevent if event.respond_to?(:uicontrolevent)
|
18
18
|
|
19
|
-
sugarcube_callbacks
|
20
|
-
addTarget(handler, action:'call:event:', forControlEvents:event)
|
19
|
+
sugarcube_callbacks(event).push(handler)
|
20
|
+
self.addTarget(handler, action:'call:event:', forControlEvents:event)
|
21
21
|
end
|
22
22
|
|
23
23
|
self
|
@@ -38,11 +38,12 @@ class UIControl
|
|
38
38
|
events.each do |event|
|
39
39
|
event = event.uicontrolevent if event.respond_to?(:uicontrolevent)
|
40
40
|
|
41
|
-
sugarcube_callbacks
|
41
|
+
sugarcube_callbacks(event).each do |handler|
|
42
42
|
self.removeTarget(handler, action:'call:event:', forControlEvents:event)
|
43
43
|
end
|
44
44
|
sugarcube_callbacks.delete(event)
|
45
45
|
end
|
46
|
+
|
46
47
|
self
|
47
48
|
end
|
48
49
|
|
@@ -64,8 +65,14 @@ private
|
|
64
65
|
# event blocks need to be retained, and the addTarget method explicitly does
|
65
66
|
# *not* retain `target`. This makes sure that callbacks are retained by
|
66
67
|
# pushing the block onto a stack.
|
67
|
-
def sugarcube_callbacks
|
68
|
-
@sugarcube_callbacks ||=
|
68
|
+
def sugarcube_callbacks(event=nil)
|
69
|
+
@sugarcube_callbacks ||= {}
|
70
|
+
if event
|
71
|
+
@sugarcube_callbacks[event] ||= []
|
72
|
+
return @sugarcube_callbacks[event]
|
73
|
+
else
|
74
|
+
return @sugarcube_callbacks
|
75
|
+
end
|
69
76
|
end
|
70
77
|
|
71
78
|
end
|
@@ -1,7 +1,13 @@
|
|
1
1
|
class UITextView
|
2
2
|
|
3
|
-
def sugarcube_callbacks
|
4
|
-
@sugarcube_callbacks ||=
|
3
|
+
def sugarcube_callbacks(notification=nil)
|
4
|
+
@sugarcube_callbacks ||= {}
|
5
|
+
if notification
|
6
|
+
@sugarcube_callbacks[notification] ||= SugarCubeNotificationForgetter.new
|
7
|
+
return @sugarcube_callbacks[notification]
|
8
|
+
else
|
9
|
+
return @sugarcube_callbacks
|
10
|
+
end
|
5
11
|
end
|
6
12
|
|
7
13
|
# Add event handlers to UITextView, with the same syntax as `UIControl`
|
@@ -62,17 +68,45 @@ class UITextView
|
|
62
68
|
end
|
63
69
|
|
64
70
|
private
|
65
|
-
def _onEventNotification(
|
66
|
-
self.sugarcube_callbacks
|
71
|
+
def _onEventNotification(notification, &block)
|
72
|
+
self.sugarcube_callbacks(notification) << NSNotificationCenter.defaultCenter.addObserverForName(notification,
|
67
73
|
object: self,
|
68
74
|
queue: NSOperationQueue.mainQueue,
|
69
75
|
usingBlock: block.weak!)
|
70
76
|
end
|
71
77
|
|
72
|
-
def _offEventNotification(
|
73
|
-
self.sugarcube_callbacks
|
78
|
+
def _offEventNotification(notification)
|
79
|
+
self.sugarcube_callbacks(notification).tap { |s| ap s }.remove_all
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
class SugarCubeNotificationForgetter
|
86
|
+
|
87
|
+
def initialize
|
88
|
+
@observers = []
|
89
|
+
end
|
90
|
+
|
91
|
+
def <<(observer)
|
92
|
+
@observers << observer
|
93
|
+
end
|
94
|
+
|
95
|
+
def remove_all
|
96
|
+
@observers.each do |callback_observer|
|
97
|
+
self.remove(callback_observer)
|
98
|
+
end
|
99
|
+
@observers = []
|
100
|
+
end
|
101
|
+
|
102
|
+
def remove(callback_observer)
|
103
|
+
if @observers.delete(callback_observer)
|
74
104
|
NSNotificationCenter.defaultCenter.removeObserver(callback_observer)
|
75
105
|
end
|
76
106
|
end
|
77
107
|
|
108
|
+
def dealloc
|
109
|
+
self.remove_all
|
110
|
+
end
|
111
|
+
|
78
112
|
end
|
data/lib/sugarcube/version.rb
CHANGED
data/spec/nsstring_files_spec.rb
CHANGED
@@ -20,9 +20,18 @@ describe 'NSString' do
|
|
20
20
|
'foo'.temporary_path.should.end_with?('tmp/foo')
|
21
21
|
end
|
22
22
|
|
23
|
-
it "should have
|
24
|
-
'
|
25
|
-
'
|
23
|
+
it "should have a resource_path method" do
|
24
|
+
'little_square.png'.resource_path.should.start_with?("/Users")
|
25
|
+
'little_square.png'.resource_path.should.end_with?("SugarCube_spec.app/little_square.png")
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should have a resource_exists? method" do
|
29
|
+
'little_square.png'.resource_exists?.should == true
|
30
|
+
'foo'.resource_exists?.should == false
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should have a #file_exists? method" do
|
34
|
+
'foo'.document_path.file_exists?.should == false
|
26
35
|
end
|
27
36
|
|
28
37
|
it "should have a remove_file! method" do
|
@@ -34,11 +43,6 @@ describe 'NSString' do
|
|
34
43
|
filename.file_exists?.should == false
|
35
44
|
end
|
36
45
|
|
37
|
-
it "should have a resource_exists? method" do
|
38
|
-
'little_square.png'.resource_exists?.should == true
|
39
|
-
'foo'.resource_exists?.should == false
|
40
|
-
end
|
41
|
-
|
42
46
|
describe "file_exists?" do
|
43
47
|
|
44
48
|
it "should not file_exists" do
|
@@ -201,3 +205,45 @@ describe 'NSString' do
|
|
201
205
|
|
202
206
|
end
|
203
207
|
|
208
|
+
|
209
|
+
describe 'NSString deprecated methods' do
|
210
|
+
|
211
|
+
it "should have a #document method" do
|
212
|
+
'foo'.document.should.start_with?('/Users')
|
213
|
+
'foo'.document.should.end_with?('Documents/foo')
|
214
|
+
end
|
215
|
+
|
216
|
+
it "should have a #cache method" do
|
217
|
+
'foo'.cache.should.start_with?('/Users')
|
218
|
+
'foo'.cache.should.end_with?('Library/Caches/foo')
|
219
|
+
end
|
220
|
+
|
221
|
+
it "should have a #app_support method" do
|
222
|
+
'foo'.app_support.should.start_with?('/Users')
|
223
|
+
'foo'.app_support.should.end_with?('Library/Application Support/foo')
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should have a #temporary method" do
|
227
|
+
'foo'.temporary.should.start_with?('/Users')
|
228
|
+
'foo'.temporary.should.end_with?('tmp/foo')
|
229
|
+
end
|
230
|
+
|
231
|
+
it "should have a resource method" do
|
232
|
+
'little_square.png'.resource.should.start_with?("/Users")
|
233
|
+
'little_square.png'.resource.should.end_with?("SugarCube_spec.app/little_square.png")
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should have an #exists? method" do
|
237
|
+
'foo'.document_path.exists?.should == false
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should have a remove! method" do
|
241
|
+
filename = 'remove_me'.document_path
|
242
|
+
unless filename.file_exists?
|
243
|
+
NSData.data.writeToFile(filename, atomically: true)
|
244
|
+
end
|
245
|
+
filename.remove!
|
246
|
+
filename.file_exists?.should == false
|
247
|
+
end
|
248
|
+
|
249
|
+
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: 1.4.
|
4
|
+
version: 1.4.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2014-01-
|
15
|
+
date: 2014-01-29 00:00:00.000000000 Z
|
16
16
|
dependencies: []
|
17
17
|
description: ! '== Description
|
18
18
|
|
@@ -276,7 +276,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
276
276
|
version: '0'
|
277
277
|
requirements: []
|
278
278
|
rubyforge_project:
|
279
|
-
rubygems_version: 1.8.
|
279
|
+
rubygems_version: 1.8.11
|
280
280
|
signing_key:
|
281
281
|
specification_version: 3
|
282
282
|
summary: Extensions for Ruby to make Rubymotion development more enjoyable, and hopefully
|