sugarcube 0.8.5 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -85,6 +85,74 @@ Examples
85
85
  end
86
86
  ```
87
87
 
88
+ NSDate
89
+ --------
90
+
91
+ NSDate does not make it easy to get the sensible attributes of your datetime
92
+ object. This *does* make sense, if you are truly thinking globally, but I am
93
+ not. :-P
94
+
95
+ Adds the following methods to get a date component: `year, month, day, hour, minute, second, yms, hms, datetime`
96
+
97
+ `ymd`, `hms`, and `datetime` return arrays, so that comparing dates, times, or
98
+ both become simple `date1.ymd == date2.ymd`. If you need to *compare* dates,
99
+ use the Foundation method `date1.compare(date2)` => `-1 or 0 or 1`.
100
+
101
+ ```
102
+ (main)> now = NSDate.new
103
+ => 2012-09-13 09:19:06 -0600
104
+ (main)> now.year
105
+ => 2012
106
+ (main)> now.month
107
+ => 9
108
+ (main)> now.day
109
+ => 13
110
+ (main)> now.hour
111
+ => 9
112
+ (main)> now.minute
113
+ => 19
114
+ (main)> now.second
115
+ => 6
116
+ (main)> now.weekday
117
+ => 5
118
+ (main)> now.ymd
119
+ => [2012, 9, 13]
120
+ (main)> now.hms
121
+ => [9, 19, 6]
122
+ (main)> now.datetime
123
+ => [2012, 9, 13, 9, 19, 6]
124
+ ```
125
+
126
+ And it is easy to add seconds to the date, and don't forget about the
127
+ time-related methods added to `Numeric`!
128
+
129
+ ```ruby
130
+ (main)> now + 5
131
+ => 2012-09-13 09:19:11 -0600
132
+ (main)> now - 5
133
+ => 2012-09-13 09:19:01 -0600
134
+ (main)> now + 5.minutes
135
+ => 2012-09-13 09:24:06 -0600
136
+ (main)> now + 5.days
137
+ => 2012-09-18 09:19:06 -0600
138
+ ```
139
+
140
+ Time zone objects are available, but the `utc_offset` is a little more
141
+ immediately useful. It returns the offset *in seconds*, so divide by `1.0.hour`
142
+ to get the offset in hours.
143
+
144
+ ```ruby
145
+ (main)> now.timezone
146
+ => #<__NSTimeZone:0x9384c70>
147
+ (main)> now.timezone.name
148
+ => "America/Denver"
149
+ (main)> now.utc_offset
150
+ => -21600
151
+ (main)> now.utc_offset / 1.0.hour
152
+ => -6.0
153
+ ```
154
+
155
+
88
156
  NSURL
89
157
  -------
90
158
 
@@ -201,10 +269,10 @@ UIAlertView.alert("This is happening, OK?", buttons: ["Nevermind", "OK"],
201
269
  # Full on whiz bangery. Note the success block takes the pressed button, but as
202
270
  # a string instead of an index. The cancel button should be the first entry in
203
271
  # `buttons:`
204
- UIAlertView.alert "I mean, is this cool?", buttons: %w[No! Sure! Hmmmm]
272
+ UIAlertView.alert "I mean, is this cool?", buttons: %w[No! Sure! Hmmmm],
205
273
  message: "No going back now",
206
- cancel: { self.cancel },
207
- success: { |pressed| self.proceed if pressed == "Sure!" }
274
+ cancel: proc { self.cancel },
275
+ success: proc { |pressed| self.proceed if pressed == "Sure!" }
208
276
  ```
209
277
 
210
278
  UIView
@@ -365,6 +433,12 @@ Makes it easy to post a notification to some or all objects.
365
433
  "my notification".post_notification # => NSNotificationCenter.defaultCenter.postNotificationName("my notification", object:nil)
366
434
  "my notification".post_notification(obj) # => NSNotificationCenter.defaultCenter.postNotificationName("my notification", object:obj)
367
435
  "my notification".post_notification(obj, user: 'dict') # => NSNotificationCenter.defaultCenter.postNotificationName("my notification", object:obj, userInfo:{user: 'dict'})
436
+
437
+ # very similar to add or remove an observer
438
+ "my notification".add_observer(observer, :method_name)
439
+ "my notification".add_observer(observer, :method_name, target)
440
+ "my notification".remove_observer(observer)
441
+ "my notification".remove_observer(observer, target)
368
442
  ```
369
443
 
370
444
  NSTimer
@@ -12,4 +12,15 @@ class NSString
12
12
  end
13
13
  end
14
14
 
15
+ def add_observer(observer, message, object=nil)
16
+ NSNotificationCenter.defaultCenter.addObserver(observer,
17
+ selector: message,
18
+ name: self,
19
+ object: object)
20
+ end
21
+
22
+ def remove_observer(observer, object=nil)
23
+ NSNotificationCenter.defaultCenter.removeObserver(observer, name:self, object:object)
24
+ end
25
+
15
26
  end
@@ -0,0 +1,62 @@
1
+ class NSDate
2
+ def year
3
+ return components(NSYearCalendarUnit).year
4
+ end
5
+
6
+ def month
7
+ return components(NSMonthCalendarUnit).month
8
+ end
9
+
10
+ def day
11
+ return components(NSDayCalendarUnit).day
12
+ end
13
+
14
+ def ymd
15
+ return [self.year, self.month, self.day]
16
+ end
17
+
18
+ def weekday
19
+ return components(NSWeekdayCalendarUnit).weekday
20
+ end
21
+
22
+ def hour
23
+ return components(NSHourCalendarUnit).hour
24
+ end
25
+
26
+ def minute
27
+ return components(NSMinuteCalendarUnit).minute
28
+ end
29
+
30
+ def second
31
+ return components(NSSecondCalendarUnit).second
32
+ end
33
+
34
+ def timezone
35
+ return components(NSTimeZoneCalendarUnit).timeZone
36
+ end
37
+ alias timeZone timezone
38
+
39
+ def utc_offset
40
+ return self.timezone.secondsFromGMT
41
+ end
42
+
43
+ def hms
44
+ return [self.hour, self.minute, self.second]
45
+ end
46
+
47
+ def datetime
48
+ return [self.year, self.month, self.day, self.hour, self.minute, self.second]
49
+ end
50
+
51
+ def +(time_interval)
52
+ return self.dateByAddingTimeInterval(time_interval)
53
+ end
54
+
55
+ private
56
+ def components(components)
57
+ unless (@@calendar ||= nil)
58
+ @@calendar = NSCalendar.alloc.initWithCalendarIdentifier(NSGregorianCalendar)
59
+ end
60
+ return @@calendar.components(components, fromDate:self)
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module SugarCube
2
- Version = '0.8.5'
2
+ Version = '0.8.6'
3
3
  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.8.5
4
+ version: 0.8.6
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-11 00:00:00.000000000 Z
13
+ date: 2012-09-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
17
- requirement: &70246812196120 !ruby/object:Gem::Requirement
17
+ requirement: &70094164450360 !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: *70246812196120
25
+ version_requirements: *70094164450360
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &70246812195480 !ruby/object:Gem::Requirement
28
+ requirement: &70094164449820 !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: *70246812195480
36
+ version_requirements: *70094164449820
37
37
  description: ! '== Description
38
38
 
39
39
 
@@ -74,6 +74,7 @@ files:
74
74
  - lib/sugarcube/exceptions.rb
75
75
  - lib/sugarcube/fixnum.rb
76
76
  - lib/sugarcube/notifications.rb
77
+ - lib/sugarcube/nsdate.rb
77
78
  - lib/sugarcube/nsindexpath.rb
78
79
  - lib/sugarcube/nsindexset.rb
79
80
  - lib/sugarcube/nsstring.rb