sugarcube 0.9.2 → 0.9.3
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.
- data/README.md +109 -20
- data/lib/sugarcube/modal.rb +12 -0
- data/lib/sugarcube/notifications.rb +5 -5
- data/lib/sugarcube/nsdate.rb +26 -4
- data/lib/sugarcube/numeric.rb +4 -58
- data/lib/sugarcube/symbol.rb +12 -0
- data/lib/sugarcube/timer.rb +63 -0
- data/lib/sugarcube/uicontrol.rb +9 -2
- data/lib/sugarcube/uiimage.rb +0 -1
- data/lib/sugarcube/uitextview.rb +78 -0
- data/lib/sugarcube/version.rb +1 -1
- metadata +9 -6
data/README.md
CHANGED
@@ -80,9 +80,12 @@ Examples
|
|
80
80
|
100.0.percent # => 1.00
|
81
81
|
55.0.percent # => 0.55
|
82
82
|
|
83
|
-
|
84
|
-
|
85
|
-
|
83
|
+
# convert to radians. does this look weird?
|
84
|
+
180.degrees # => Math::PI
|
85
|
+
|
86
|
+
# convert multiples of pi
|
87
|
+
2.pi # => 2 * Math::PI
|
88
|
+
0.5.pi # => 0.5 * Math::PI
|
86
89
|
```
|
87
90
|
|
88
91
|
NSDate
|
@@ -93,10 +96,10 @@ That's the good news. The bad news? That still doesn't help a lot with some of
|
|
93
96
|
the everyday date&time crap we have to deal with. (I hate dates, especially
|
94
97
|
recurring events)
|
95
98
|
|
96
|
-
1. Adds the following methods to get date and time components: `
|
99
|
+
1. Adds the following methods to get date and time components: `date_array, time_array, datetime_array`.
|
97
100
|
|
98
101
|
These methods return arrays. Comparing dates, times, or both become
|
99
|
-
simple `date1.
|
102
|
+
simple `date1.date_array == date2.date_array`.
|
100
103
|
2. While I would love to support `date + 1.month` and have that support "smart"
|
101
104
|
calendar math (e.g. "2/13/2013" + 1.month => "3/13/2013"), I can't fudge with
|
102
105
|
the return value of `1.month` (=> `Fixnum`), and I won't make the terrible
|
@@ -110,11 +113,11 @@ recurring events)
|
|
110
113
|
```
|
111
114
|
(main)> now = NSDate.new # Time.new is the same thing
|
112
115
|
=> 2012-09-13 09:19:06 -0600
|
113
|
-
(main)> now.
|
116
|
+
(main)> now.date_array
|
114
117
|
=> [2012, 9, 13]
|
115
|
-
(main)> now.
|
118
|
+
(main)> now.time_array
|
116
119
|
=> [9, 19, 6]
|
117
|
-
(main)> now.
|
120
|
+
(main)> now.datetime_array
|
118
121
|
=> [2012, 9, 13, 9, 19, 6]
|
119
122
|
```
|
120
123
|
|
@@ -154,33 +157,58 @@ the offset in hours. `utc_offset` is built into `Time`, not added by SugarCube.
|
|
154
157
|
The `delta` method is smart.
|
155
158
|
|
156
159
|
```ruby
|
157
|
-
(main)>
|
158
|
-
=> 1330473600
|
159
|
-
(main)> feb_28_2012 = Time.at(feb_28_2012_stamp)
|
160
|
+
(main)> feb_28_2012 = Time.at(1330473600)
|
160
161
|
=> 2012-02-28 17:00:00 -0700
|
161
162
|
|
163
|
+
# add an hour or two
|
162
164
|
(main)> feb_28_2012.delta(hours:1)
|
163
165
|
=> 2012-02-28 18:00:00 -0700
|
164
166
|
(main)> feb_28_2012.delta(hours:2)
|
165
167
|
=> 2012-02-28 19:00:00 -0700
|
166
168
|
|
169
|
+
# add some days
|
167
170
|
(main)> feb_28_2012.delta(days:1)
|
168
171
|
=> 2012-02-29 17:00:00 -0700
|
169
172
|
(main)> feb_28_2012.delta(days:2)
|
170
173
|
=> 2012-03-01 17:00:00 -0700
|
171
174
|
|
175
|
+
# how about a month?
|
172
176
|
(main)> feb_28_2012.delta(months:1)
|
173
177
|
=> 2012-03-28 17:00:00 -0600 # look, the time didn't change, event though there was a DST change!
|
178
|
+
|
179
|
+
# cool, but if you want a more literal "24 hours", specify a time unit
|
174
180
|
(main)> feb_28_2012.delta(months:1, hours:0)
|
175
181
|
=> 2012-03-28 18:00:00 -0600 # disable the DST fix by specifying hours, minutes, or seconds (a "precise" delta)
|
176
182
|
|
183
|
+
# in one year, it will still be Feb 28th
|
177
184
|
(main)> feb_28_2012.delta(years:1)
|
178
185
|
=> 2013-02-28 17:00:00 -0700
|
186
|
+
|
187
|
+
# and we already know what adding a day looks like
|
188
|
+
(main)> feb_28_2012.delta(days:1)
|
189
|
+
=> 2012-02-29 17:00:00 -0700
|
190
|
+
|
191
|
+
# a year and a day is tricky, because do we add a day, then a year? or add a
|
192
|
+
# year and then a day? well, i'll tell you, **I** add a day and then a year,
|
193
|
+
# which is feb 29th, which is no good, and the algorithm rolls back days to the
|
194
|
+
# last day of the month, so we get the 28th.
|
179
195
|
(main)> feb_28_2012.delta(days:1, years:1)
|
180
196
|
=> 2013-02-28 17:00:00 -0700
|
197
|
+
|
198
|
+
# adding 2 days puts us into March, which then "looks right", but it's both
|
199
|
+
# right AND wrong, depending on how you look at it. Another example is below,
|
200
|
+
# where we add a month to January 30th. Really, though, think of this: how
|
201
|
+
# often do you need to add a year AND a day!? Adding a year is more common, and
|
202
|
+
# this is showing that adding a year to Feb 29th will give you Feb 28th, which I
|
203
|
+
# think is better than March 1st.
|
204
|
+
(main)> feb_28_2012.delta(days:2, years:1)
|
205
|
+
=> 2013-03-01 17:00:00 -0700
|
206
|
+
|
207
|
+
# Crazier: add a day (fab 29th), then a month (march 29th), THEN a year.
|
181
208
|
(main)> feb_28_2012.delta(days:1, years:1, months:1)
|
182
209
|
=> 2013-03-29 17:00:00 -0600
|
183
210
|
|
211
|
+
# k, for the next examples, we need a new date, and this is a non-leap year.
|
184
212
|
(main)> jan_29_2013 = feb_28_2012.delta(days:1, months:11)
|
185
213
|
=> 2013-01-29 17:00:00 -0700
|
186
214
|
|
@@ -188,19 +216,22 @@ The `delta` method is smart.
|
|
188
216
|
(main)> jan_29_2013.delta(months:2)
|
189
217
|
=> 2013-03-29 17:00:00 -0600
|
190
218
|
|
191
|
-
#
|
192
|
-
#
|
193
|
-
#
|
194
|
-
#
|
219
|
+
# Yeah, smart guy? Well then what is 1/29/2013 plus ONE month. It's feb 28th.
|
220
|
+
# When someone says "see you in a month!" they mean "next month", not "in the
|
221
|
+
# early part of two months in the future", which is where the math will take you
|
222
|
+
# if you don't add a "day of month" correction.
|
195
223
|
(main)> jan_29_2013.delta(months:1)
|
196
224
|
=> 2013-02-28 17:00:00 -0700
|
225
|
+
# but last year was a leap year, so we should get Feb 29th, 2012:
|
226
|
+
(main)> jan_29_2013.delta(months:1, years: -1)
|
227
|
+
=> 2012-02-29 17:00:00 -0700 # success!
|
197
228
|
|
198
|
-
#
|
229
|
+
# do other deltas work in reverse? fuuuuuu...
|
199
230
|
(main)> jan_29_2013.delta(months:-11)
|
200
231
|
=> 2012-02-29 17:00:00 -0700
|
201
232
|
# ...ck yeah! :-)
|
202
233
|
|
203
|
-
# unfortunately you will end up with stuff like this:
|
234
|
+
# unfortunately you will, in the edge cases, end up with stuff like this:
|
204
235
|
(main)> feb_28_2012 == feb_28_2012.delta(days:1, months:12).delta(months:-12)
|
205
236
|
=> true
|
206
237
|
```
|
@@ -273,7 +304,7 @@ This is the "big daddy". Lots of sugar here...
|
|
273
304
|
:rounded.uibuttontype # => UIButtonTypeRoundedRect
|
274
305
|
:highlighted.uicontrolstate # => UIControlStateHighlighted
|
275
306
|
:touch.uicontrolevent # => UIControlEventTouchUpInside
|
276
|
-
:
|
307
|
+
:change.uicontrolevent # => UIControlEventValueChanged
|
277
308
|
:all.uicontrolevent # => UIControlEventAllEvents
|
278
309
|
:blue.uicolor # UIColor.blueColor
|
279
310
|
# all CSS colors are supported, and alpha
|
@@ -441,6 +472,31 @@ button.off(:all)
|
|
441
472
|
You can only remove handlers by "type", not by the action. e.g. If you bind
|
442
473
|
three `:touch` events, calling `button.off(:touch)` will remove all three.
|
443
474
|
|
475
|
+
UIViewController
|
476
|
+
------------------
|
477
|
+
|
478
|
+
It is nice that *any* `UIViewController` can present a modal, but if you have
|
479
|
+
tabs or navs or crap in the way, this is actually *NOT* what you want. You
|
480
|
+
should use the `rootViewController` (whatever it may be) to present to modal.
|
481
|
+
|
482
|
+
And since this is a property on `UIWindow`, which is more-or-less a constant, we
|
483
|
+
can make this the easiest to do!
|
484
|
+
|
485
|
+
```ruby
|
486
|
+
include SugarCube::Modal
|
487
|
+
view_ctlr = EditSomethingViewController.new
|
488
|
+
present_modal(view_ctlr)
|
489
|
+
# ...later, when all is well...
|
490
|
+
dismiss_modal
|
491
|
+
```
|
492
|
+
|
493
|
+
These accept completion blocks:
|
494
|
+
|
495
|
+
```ruby
|
496
|
+
present_modal(view_ctlr) { puts "here!" }
|
497
|
+
dismiss_modal { puts "gone!" }
|
498
|
+
```
|
499
|
+
|
444
500
|
UINavigationController
|
445
501
|
------------------------
|
446
502
|
|
@@ -473,6 +529,37 @@ tabbar_ctlr.setViewControllers(controllers, animated: true)
|
|
473
529
|
# =>
|
474
530
|
|
475
531
|
tabbar_ctlr << new_ctlr
|
532
|
+
```
|
533
|
+
|
534
|
+
UITextView
|
535
|
+
------------
|
536
|
+
|
537
|
+
Added some `UIControl`-like event binding. You MUST call the `off` methods,
|
538
|
+
because these methods use `NSNotification`s, and you must turn off listeners.
|
539
|
+
|
540
|
+
There are two aliases for each event. I prefer the present tense (jQuery-style `on :change`),
|
541
|
+
but UIKit prefers past simple (`UITextViewTextDidBeginEditingNotification`).
|
542
|
+
|
543
|
+
So these are all the same:
|
544
|
+
|
545
|
+
:editing_did_begin :begin
|
546
|
+
:editing_did_change :change
|
547
|
+
:editing_did_end :end
|
548
|
+
|
549
|
+
```ruby
|
550
|
+
text_view = UITextView.new
|
551
|
+
text_view.on :editing_did_begin do
|
552
|
+
p 'wait for it...'
|
553
|
+
end
|
554
|
+
text_view.on :editing_did_change do
|
555
|
+
p text_view.text
|
556
|
+
end
|
557
|
+
text_view.on :editing_did_end do
|
558
|
+
p 'done!'
|
559
|
+
end
|
560
|
+
|
561
|
+
# later... like in `viewWillDisappear`. I'll use the alternative aliases here
|
562
|
+
text_view.off :change, :end, :begin
|
476
563
|
```
|
477
564
|
|
478
565
|
NSNotificationCenter
|
@@ -488,9 +575,11 @@ Makes it easy to post a notification to some or all objects.
|
|
488
575
|
|
489
576
|
# very similar to add or remove an observer
|
490
577
|
"my notification".add_observer(observer, :method_name)
|
491
|
-
"my notification".add_observer(observer, :method_name,
|
578
|
+
"my notification".add_observer(observer, :method_name, object)
|
579
|
+
|
580
|
+
# remove the observer
|
492
581
|
"my notification".remove_observer(observer)
|
493
|
-
"my notification".remove_observer(observer,
|
582
|
+
"my notification".remove_observer(observer, object)
|
494
583
|
```
|
495
584
|
|
496
585
|
NSTimer
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module SugarCube
|
2
|
+
module Modal
|
3
|
+
module_function
|
4
|
+
def present_modal(view_ctlr, &block)
|
5
|
+
UIApplication.sharedApplication.keyWindow.rootViewController.presentViewController(view_ctlr, animated:true, completion:block)
|
6
|
+
end
|
7
|
+
|
8
|
+
def dismiss_modal(&block)
|
9
|
+
UIApplication.sharedApplication.keyWindow.rootViewController.dismissViewControllerAnimated(true, completion:block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -12,15 +12,15 @@ class NSString
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
def add_observer(
|
16
|
-
NSNotificationCenter.defaultCenter.addObserver(
|
17
|
-
selector:
|
15
|
+
def add_observer(target, action, object=nil)
|
16
|
+
NSNotificationCenter.defaultCenter.addObserver(target,
|
17
|
+
selector: action,
|
18
18
|
name: self,
|
19
19
|
object: object)
|
20
20
|
end
|
21
21
|
|
22
|
-
def remove_observer(
|
23
|
-
NSNotificationCenter.defaultCenter.removeObserver(
|
22
|
+
def remove_observer(target, object=nil)
|
23
|
+
NSNotificationCenter.defaultCenter.removeObserver(target, name:self, object:object)
|
24
24
|
end
|
25
25
|
|
26
26
|
end
|
data/lib/sugarcube/nsdate.rb
CHANGED
@@ -4,6 +4,8 @@ class NSDate
|
|
4
4
|
end
|
5
5
|
alias timeZone timezone
|
6
6
|
|
7
|
+
# In the rare case you actually get an NSDate object - not a Time object - this
|
8
|
+
# method is actually useful.
|
7
9
|
def utc_offset
|
8
10
|
return self.timezone.secondsFromGMT
|
9
11
|
end
|
@@ -12,25 +14,45 @@ class NSDate
|
|
12
14
|
self.year % 4 == 0 and self.year % 100 != 0 or self.year % 400 == 0
|
13
15
|
end
|
14
16
|
|
15
|
-
|
17
|
+
# (main)> t = Time.new
|
18
|
+
# => 2012-09-27 11:29:12 +0900
|
19
|
+
# (main)> t.time_array
|
20
|
+
# => [2012, 9, 27]
|
21
|
+
def date_array
|
16
22
|
return [self.year, self.month, self.day]
|
17
23
|
end
|
18
24
|
|
19
|
-
|
25
|
+
# (main)> t = Time.new
|
26
|
+
# => 2012-09-27 11:29:12 +0900
|
27
|
+
# (main)> t.time_array
|
28
|
+
# => [11, 29, 12]
|
29
|
+
def time_array
|
20
30
|
return [self.hour, self.min, self.sec]
|
21
31
|
end
|
22
32
|
|
23
|
-
|
33
|
+
# (main)> t = Time.new
|
34
|
+
# => 2012-09-27 11:29:12 +0900
|
35
|
+
# (main)> t.time_array
|
36
|
+
# => [2012, 9, 12, 11, 29, 12]
|
37
|
+
def datetime_array
|
24
38
|
return [self.year, self.month, self.day, self.hour, self.min, self.sec]
|
25
39
|
end
|
26
40
|
|
41
|
+
# (main)> t = Time.new
|
42
|
+
# => 2012-09-27 11:29:12 +0900
|
43
|
+
# (main)> t.start_of_day
|
44
|
+
# => 2012-09-27 00:00:00 +0900
|
27
45
|
def start_of_day
|
28
46
|
time_interval = self.hour.hours + self.min.minutes + self.sec
|
29
47
|
return self - time_interval
|
30
48
|
end
|
31
49
|
|
50
|
+
# (main)> t = Time.new
|
51
|
+
# => 2012-09-27 11:29:12 +0900
|
52
|
+
# (main)> t.end_of_day
|
53
|
+
# => 2012-09-28 00:00:00 +0900
|
32
54
|
def end_of_day
|
33
|
-
time_interval = (23 - self.hour).hours + (59 - self.min).minutes
|
55
|
+
time_interval = (23 - self.hour).hours + (59 - self.min).minutes - self.sec + 60
|
34
56
|
return self + time_interval
|
35
57
|
end
|
36
58
|
|
data/lib/sugarcube/numeric.rb
CHANGED
@@ -4,66 +4,12 @@ class Numeric
|
|
4
4
|
self / 100.0
|
5
5
|
end
|
6
6
|
|
7
|
-
def
|
8
|
-
self
|
7
|
+
def degrees
|
8
|
+
self / 180.0 * Math::PI
|
9
9
|
end
|
10
|
-
alias second seconds
|
11
|
-
alias sec seconds
|
12
|
-
alias secs seconds
|
13
10
|
|
14
|
-
def
|
15
|
-
self *
|
11
|
+
def pi
|
12
|
+
self * Math::PI
|
16
13
|
end
|
17
|
-
alias minute minutes
|
18
|
-
alias min minutes
|
19
|
-
alias mins minutes
|
20
14
|
|
21
|
-
def hours
|
22
|
-
self * 3600
|
23
|
-
end
|
24
|
-
alias hour hours
|
25
|
-
|
26
|
-
def days
|
27
|
-
self.hours * 24
|
28
|
-
end
|
29
|
-
alias day days
|
30
|
-
|
31
|
-
def weeks
|
32
|
-
self.days * 7
|
33
|
-
end
|
34
|
-
alias week weeks
|
35
|
-
|
36
|
-
def months
|
37
|
-
self.days * 30
|
38
|
-
end
|
39
|
-
alias month months
|
40
|
-
|
41
|
-
def years
|
42
|
-
self.days * 365
|
43
|
-
end
|
44
|
-
alias year years
|
45
|
-
|
46
|
-
def later(user_info=nil, &fire)
|
47
|
-
NSTimer.scheduledTimerWithTimeInterval(self.to_f, target: fire, selector: 'call:', userInfo: user_info, repeats: false)
|
48
|
-
end
|
49
|
-
|
50
|
-
def every(user_info=nil, &fire)
|
51
|
-
NSTimer.scheduledTimerWithTimeInterval(self.to_f, target: fire, selector: 'call:', userInfo: user_info, repeats: true)
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
|
57
|
-
module SugarCube
|
58
|
-
module Timer
|
59
|
-
module_function
|
60
|
-
|
61
|
-
def every(time, user_info=nil, &fire)
|
62
|
-
time.every user_info, &fire
|
63
|
-
end
|
64
|
-
|
65
|
-
def after(time, user_info=nil, &fire)
|
66
|
-
time.later user_info, &fire
|
67
|
-
end
|
68
|
-
end
|
69
15
|
end
|
data/lib/sugarcube/symbol.rb
CHANGED
@@ -119,6 +119,9 @@ class Symbol
|
|
119
119
|
touch: UIControlEventTouchUpInside,
|
120
120
|
touch_up: UIControlEventTouchUpInside,
|
121
121
|
touch_down: UIControlEventTouchDown,
|
122
|
+
change: UIControlEventValueChanged|UIControlEventEditingChanged,
|
123
|
+
# I'm leaving this for backwards compatibility. please use 'change' or
|
124
|
+
# 'editing_did_change':
|
122
125
|
changed: UIControlEventValueChanged|UIControlEventEditingChanged,
|
123
126
|
|
124
127
|
touch_down_repeat: UIControlEventTouchDownRepeat,
|
@@ -133,7 +136,10 @@ class Symbol
|
|
133
136
|
value_changed: UIControlEventValueChanged,
|
134
137
|
|
135
138
|
editing_did_begin: UIControlEventEditingDidBegin,
|
139
|
+
# nice. very consistent APPLE:
|
136
140
|
editing_changed: UIControlEventEditingChanged,
|
141
|
+
# now here's consistency:
|
142
|
+
editing_did_change: UIControlEventEditingChanged,
|
137
143
|
editing_did_end: UIControlEventEditingDidEnd,
|
138
144
|
editing_did_endonexit: UIControlEventEditingDidEndOnExit,
|
139
145
|
|
@@ -159,6 +165,7 @@ class Symbol
|
|
159
165
|
|
160
166
|
@returnkeys = {
|
161
167
|
default: UIReturnKeyDefault,
|
168
|
+
return: UIReturnKeyDefault,
|
162
169
|
go: UIReturnKeyGo,
|
163
170
|
google: UIReturnKeyGoogle,
|
164
171
|
join: UIReturnKeyJoin,
|
@@ -271,12 +278,17 @@ class Symbol
|
|
271
278
|
@keyboardtypes = {
|
272
279
|
default: UIKeyboardTypeDefault,
|
273
280
|
asciicapable: UIKeyboardTypeASCIICapable,
|
281
|
+
ascii: UIKeyboardTypeASCIICapable,
|
274
282
|
numbersandpunctuation: UIKeyboardTypeNumbersAndPunctuation,
|
275
283
|
url: UIKeyboardTypeURL,
|
276
284
|
numberpad: UIKeyboardTypeNumberPad,
|
285
|
+
number: UIKeyboardTypeNumberPad,
|
277
286
|
phonepad: UIKeyboardTypePhonePad,
|
287
|
+
phone: UIKeyboardTypePhonePad,
|
278
288
|
namephonepad: UIKeyboardTypeNamePhonePad,
|
289
|
+
nameandphone: UIKeyboardTypeNamePhonePad,
|
279
290
|
emailaddress: UIKeyboardTypeEmailAddress,
|
291
|
+
email: UIKeyboardTypeEmailAddress,
|
280
292
|
}
|
281
293
|
|
282
294
|
private
|
@@ -0,0 +1,63 @@
|
|
1
|
+
class Numeric
|
2
|
+
def seconds
|
3
|
+
self
|
4
|
+
end
|
5
|
+
alias second seconds
|
6
|
+
alias sec seconds
|
7
|
+
alias secs seconds
|
8
|
+
|
9
|
+
def minutes
|
10
|
+
self * 60
|
11
|
+
end
|
12
|
+
alias minute minutes
|
13
|
+
alias min minutes
|
14
|
+
alias mins minutes
|
15
|
+
|
16
|
+
def hours
|
17
|
+
self * 3600
|
18
|
+
end
|
19
|
+
alias hour hours
|
20
|
+
|
21
|
+
def days
|
22
|
+
self.hours * 24
|
23
|
+
end
|
24
|
+
alias day days
|
25
|
+
|
26
|
+
def weeks
|
27
|
+
self.days * 7
|
28
|
+
end
|
29
|
+
alias week weeks
|
30
|
+
|
31
|
+
def months
|
32
|
+
self.days * 30
|
33
|
+
end
|
34
|
+
alias month months
|
35
|
+
|
36
|
+
def years
|
37
|
+
self.days * 365
|
38
|
+
end
|
39
|
+
alias year years
|
40
|
+
|
41
|
+
def later(user_info=nil, &fire)
|
42
|
+
NSTimer.scheduledTimerWithTimeInterval(self.to_f, target: fire, selector: 'call:', userInfo: user_info, repeats: false)
|
43
|
+
end
|
44
|
+
|
45
|
+
def every(user_info=nil, &fire)
|
46
|
+
NSTimer.scheduledTimerWithTimeInterval(self.to_f, target: fire, selector: 'call:', userInfo: user_info, repeats: true)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
module SugarCube
|
52
|
+
module Timer
|
53
|
+
module_function
|
54
|
+
|
55
|
+
def every(time, user_info=nil, &fire)
|
56
|
+
time.every user_info, &fire
|
57
|
+
end
|
58
|
+
|
59
|
+
def after(time, user_info=nil, &fire)
|
60
|
+
time.later user_info, &fire
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/sugarcube/uicontrol.rb
CHANGED
@@ -7,7 +7,8 @@ class UIControl
|
|
7
7
|
@sugarcube_callbacks ||= Hash.new { |hash, key| hash[key] = [] }
|
8
8
|
end
|
9
9
|
|
10
|
-
# Add event handlers to UIControls
|
10
|
+
# Add event handlers to UIControls. See symbol.rb for the uicontrolevent
|
11
|
+
# constant aliases.
|
11
12
|
#
|
12
13
|
# @example
|
13
14
|
# button = UIButton.alloc.initWithFrame([0, 0, 10, 10])
|
@@ -24,12 +25,18 @@ class UIControl
|
|
24
25
|
self
|
25
26
|
end
|
26
27
|
|
27
|
-
# Removes all events that were bound with `on`.
|
28
|
+
# Removes all events that were bound with `on`. See symbol.rb for the
|
29
|
+
# uicontrolevent constant aliases.
|
28
30
|
#
|
29
31
|
# @example
|
30
32
|
# button.off(:touch)
|
31
33
|
# button.off(:touchupoutside, :touchcancel)
|
34
|
+
# button.off # all events
|
32
35
|
def off(*events)
|
36
|
+
if events.length == 0
|
37
|
+
events = sugarcube_callbacks.keys
|
38
|
+
end
|
39
|
+
|
33
40
|
events.each do |event|
|
34
41
|
event = event.uicontrolevent unless event.is_a? Fixnum
|
35
42
|
|
data/lib/sugarcube/uiimage.rb
CHANGED
@@ -0,0 +1,78 @@
|
|
1
|
+
class UITextView
|
2
|
+
|
3
|
+
def sugarcube_callbacks
|
4
|
+
@sugarcube_callbacks ||= Hash.new { |h,k| h[k] = [] }
|
5
|
+
end
|
6
|
+
|
7
|
+
# Add event handlers to UITextView, with the same syntax as `UIControl`
|
8
|
+
# objects. Present tense and past simple aliases are provided. (e.g.
|
9
|
+
# `editing_did_change` and `change`)
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# text_view = UITextView.alloc.initWithFrame([0, 0, 10, 10])
|
13
|
+
# text_view.on(:editing_did_change) { my_code }
|
14
|
+
# # alias:
|
15
|
+
# text_view.on(:change) { my_code }
|
16
|
+
# text_view.on(:editing_did_begin, :editing_did_end) { my_code }
|
17
|
+
def on(*events, &block)
|
18
|
+
events.each do |event|
|
19
|
+
case event
|
20
|
+
when :editing_did_begin, :begin, UITextViewTextDidBeginEditingNotification
|
21
|
+
_onEventNotification(UITextViewTextDidBeginEditingNotification, &block)
|
22
|
+
when :editing_did_change, :change, UITextViewTextDidChangeNotification
|
23
|
+
_onEventNotification(UITextViewTextDidChangeNotification, &block)
|
24
|
+
when :editing_did_end, :end, UITextViewTextDidEndEditingNotification
|
25
|
+
_onEventNotification(UITextViewTextDidEndEditingNotification, &block)
|
26
|
+
else
|
27
|
+
raise "Unknown or unsupported event #{event} in UITextView#on"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
# Removes all events that were bound with `on`. Present tense and past simple
|
35
|
+
# aliases are provided. (e.g. `editing_did_change` and `change`)
|
36
|
+
#
|
37
|
+
# @example
|
38
|
+
# text_view.off(:change)
|
39
|
+
# # alias:
|
40
|
+
# text_view.off(:editing_did_change)
|
41
|
+
# text_view.off(:editing_did_begin, :editing_did_end)
|
42
|
+
# text_view.off # all events
|
43
|
+
def off(*events)
|
44
|
+
if events.length == 0
|
45
|
+
events = self.sugarcube_callbacks.keys
|
46
|
+
end
|
47
|
+
|
48
|
+
events.each do |event|
|
49
|
+
case event
|
50
|
+
when :editing_did_begin, :begin, UITextViewTextDidBeginEditingNotification
|
51
|
+
_offEventNotification(UITextViewTextDidBeginEditingNotification)
|
52
|
+
when :editing_did_change, :change, UITextViewTextDidChangeNotification
|
53
|
+
_offEventNotification(UITextViewTextDidChangeNotification)
|
54
|
+
when :editing_did_end, :end, UITextViewTextDidEndEditingNotification
|
55
|
+
_offEventNotification(UITextViewTextDidEndEditingNotification)
|
56
|
+
else
|
57
|
+
raise "Unknown or unsupported event #{event} in UITextView#on"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
def _onEventNotification(notication, &block)
|
66
|
+
self.sugarcube_callbacks[notication] << NSNotificationCenter.defaultCenter.addObserverForName(notication,
|
67
|
+
object: self,
|
68
|
+
queue: NSOperationQueue.mainQueue,
|
69
|
+
usingBlock: block)
|
70
|
+
end
|
71
|
+
|
72
|
+
def _offEventNotification(nofication)
|
73
|
+
self.sugarcube_callbacks[nofication].each do |callback_observer|
|
74
|
+
NSNotificationCenter.defaultCenter.removeObserver(callback_observer)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
data/lib/sugarcube/version.rb
CHANGED
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.9.
|
4
|
+
version: 0.9.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-09-
|
13
|
+
date: 2012-09-27 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
17
|
-
requirement: &
|
17
|
+
requirement: &70363874967320 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70363874967320
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: rspec
|
28
|
-
requirement: &
|
28
|
+
requirement: &70363874965460 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,7 +33,7 @@ dependencies:
|
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *70363874965460
|
37
37
|
description: ! '== Description
|
38
38
|
|
39
39
|
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- lib/sugarcube/document.rb
|
74
74
|
- lib/sugarcube/exceptions.rb
|
75
75
|
- lib/sugarcube/fixnum.rb
|
76
|
+
- lib/sugarcube/modal.rb
|
76
77
|
- lib/sugarcube/notifications.rb
|
77
78
|
- lib/sugarcube/nsdate.rb
|
78
79
|
- lib/sugarcube/nsdate_delta.rb
|
@@ -83,6 +84,7 @@ files:
|
|
83
84
|
- lib/sugarcube/numeric.rb
|
84
85
|
- lib/sugarcube/symbol.rb
|
85
86
|
- lib/sugarcube/symbol/symbol_uicolor.rb
|
87
|
+
- lib/sugarcube/timer.rb
|
86
88
|
- lib/sugarcube/to_s/nsset.rb
|
87
89
|
- lib/sugarcube/to_s/uievent.rb
|
88
90
|
- lib/sugarcube/to_s/uitouch.rb
|
@@ -96,6 +98,7 @@ files:
|
|
96
98
|
- lib/sugarcube/uipickerview.rb
|
97
99
|
- lib/sugarcube/uisegmentedcontrol.rb
|
98
100
|
- lib/sugarcube/uitableview.rb
|
101
|
+
- lib/sugarcube/uitextview.rb
|
99
102
|
- lib/sugarcube/uiview.rb
|
100
103
|
- lib/sugarcube/uiviewcontroller.rb
|
101
104
|
- lib/sugarcube/uuid.rb
|