cha_work 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,34 @@
1
+ module ChaWork
2
+ SETTINGS = {}
3
+
4
+
5
+ def self.use_weak_callbacks=(val)
6
+ ChaWork::SETTINGS[:use_weak_callbacks] = val
7
+ end
8
+
9
+ def self.use_weak_callbacks?
10
+ ChaWork::SETTINGS[:use_weak_callbacks]
11
+ end
12
+ end
13
+
14
+
15
+ module ChaWork
16
+ module_function
17
+
18
+ def look_in(where, here, here__deprecated={})
19
+ return here[where] if here.has_key? where
20
+ if here__deprecated[where]
21
+ translated = here__deprecated[where]
22
+ NSLog("The symbol #{where.inspect} has been deprecated in favor of #{translated.inspect}")
23
+ return here[translated]
24
+ end
25
+ raise SugarCubeNotFoundException.new(where.inspect)
26
+ end
27
+
28
+ end
29
+
30
+ class SugarCubeNotFoundException < Exception
31
+ end
32
+
33
+
34
+ CW = ChaWork unless defined?(CW)
@@ -0,0 +1,26 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module ChaWork
3
+ module Message
4
+
5
+ def msg(text, type=:normal)
6
+ type = text if text == :wait
7
+ case type
8
+ when :wait
9
+ SVProgressHUD.showWithStatus(TEXT_WAIT)
10
+ when :success
11
+ SVProgressHUD.showSuccessWithStatus(text)
12
+ when :failure
13
+ SVProgressHUD.showErrorWithStatus(text)
14
+ else
15
+ SVProgressHUD.showWithStatus(text)
16
+ end
17
+ end
18
+
19
+ def hide_msg
20
+ SVProgressHUD.dismiss
21
+ end
22
+
23
+ def wait_msg; msg('请稍候'); end
24
+
25
+ end
26
+ end
@@ -0,0 +1,58 @@
1
+ # Persistence module built on top of NSUserDefaults
2
+ module ChaWork
3
+ module Persistence
4
+ module_function
5
+
6
+ def app_key
7
+ @app_key ||= NSBundle.mainBundle.bundleIdentifier
8
+ end
9
+
10
+ def []=(key, value)
11
+ storage.setObject(value, forKey: storage_key(key))
12
+ storage.synchronize
13
+ end
14
+
15
+ def [](key)
16
+ value = storage.objectForKey storage_key(key)
17
+
18
+ # RubyMotion currently has a bug where the strings returned from
19
+ # standardUserDefaults are missing some methods (e.g. to_data).
20
+ # And because the returned object is slightly different than a normal
21
+ # String, we can't just use `value.is_a?(String)`
22
+ value.class.to_s == 'String' ? value.dup : value
23
+ end
24
+
25
+ def merge(values)
26
+ values.each do |key, value|
27
+ storage.setObject(value, forKey: storage_key(key))
28
+ end
29
+ storage.synchronize
30
+ end
31
+
32
+ def delete(key)
33
+ value = storage.objectForKey storage_key(key)
34
+ storage.removeObjectForKey(storage_key(key))
35
+ storage.synchronize
36
+ value
37
+ end
38
+
39
+ def storage
40
+ NSUserDefaults.standardUserDefaults
41
+ end
42
+
43
+ def storage_key(key)
44
+ "#{app_key}_#{key}"
45
+ end
46
+
47
+ def all
48
+ hash = storage.dictionaryRepresentation.select{|k,v| k.start_with?(app_key) }
49
+ new_hash = {}
50
+ hash.each do |k,v|
51
+ new_hash[k.sub("#{app_key}_", '')] = v
52
+ end
53
+ new_hash
54
+ end
55
+ end
56
+
57
+ end
58
+ ::Persistence = ChaWork::Persistence unless defined?(::Persistence)
@@ -0,0 +1,30 @@
1
+ class Fixnum
2
+
3
+ # 0xffeedd.uicolor
4
+ # =>
5
+ # UIColor.colorWithRed(0xFF / 255.0, green: 0xEE / 255.0, blue: 0xDD / 255.0, alpha: 1.0)
6
+ # # ≈ UIColor.colorWithRed(1.0, green: 0.933, blue: 0.867, alpha: 1.0)
7
+ #
8
+ # 0xffeedd.uicolor(0.25)
9
+ # =>
10
+ # UIColor.colorWithRed(0xFF / 255.0, green: 0xEE / 255.0, blue: 0xDD / 255.0, alpha: 0.25)
11
+ # # ≈ UIColor.colorWithRed(1.0, green: 0.933, blue: 0.867, alpha: 0.25)
12
+ def uicolor(alpha=nil)
13
+ alpha = 1.0 if alpha.nil?
14
+
15
+ red = ((self & 0xFF0000) >> 16).to_f / 255.0
16
+ green = ((self & 0xFF00) >> 8).to_f / 255.0
17
+ blue = (self & 0xFF).to_f / 255.0
18
+
19
+ UIColor.colorWithRed(red, green: green, blue: blue, alpha: alpha.to_f)
20
+ end
21
+
22
+ def cgcolor(alpha=nil)
23
+ uicolor(alpha).CGColor
24
+ end
25
+
26
+ def skcolor(alpha=nil)
27
+ uicolor(alpha)
28
+ end
29
+
30
+ end
@@ -0,0 +1,22 @@
1
+ class NSNotificationCenter
2
+ def observers
3
+ @observers ||= []
4
+ end
5
+
6
+ def observe(name, object=nil, &proc)
7
+ proc.weak! if proc && ChaWork.use_weak_callbacks?
8
+ observer = self.addObserverForName(name, object:object, queue:NSOperationQueue.mainQueue, usingBlock:proc)
9
+ observers << observer
10
+ observer
11
+ end
12
+
13
+ def unobserve(observer)
14
+ return unless observers.include?(observer)
15
+ removeObserver(observer)
16
+ observers.delete(observer)
17
+ end
18
+
19
+ def post(name, object=nil, info=nil)
20
+ self.postNotificationName(name, object: object, userInfo: info)
21
+ end
22
+ end
@@ -0,0 +1,261 @@
1
+ class NSDate
2
+ # these formatters are used in `string_with_format`. Symbols are converted to
3
+ # a string using string_with_format's templating, and strings are concatenated
4
+ # as-is
5
+ SugarCubeFormats = {
6
+ iso8601: [:yyyy, '-', :MM, '-', :dd, ' ', :'HH:mm:ss.SSS'],
7
+ ymd: [:yyyy, '-', :MM, '-', :dd],
8
+ hms: [:'HH:mm:ss.SSS'],
9
+ }
10
+
11
+ def self.from_components(components)
12
+ date_components = NSDateComponents.new
13
+ components.each do |property,value|
14
+ date_components.send("#{property}=", value)
15
+ end
16
+ calendar = NSCalendar.alloc.initWithCalendarIdentifier(NSGregorianCalendar)
17
+ return calendar.dateFromComponents(date_components)
18
+ end
19
+
20
+ # Time.now is defined, but not NSDate.now.
21
+ def self.now
22
+ NSDate.new
23
+ end
24
+
25
+ def self.today
26
+ NSDate.new.start_of_day
27
+ end
28
+
29
+ def self.tomorrow
30
+ NSDate.new.delta(days: 1).start_of_day
31
+ end
32
+
33
+ def self.yesterday
34
+ NSDate.new.delta(days: -1).start_of_day
35
+ end
36
+
37
+ def string_with_style(date_style=NSDateFormatterMediumStyle, time_style=NSDateFormatterNoStyle)
38
+ date_formatter = NSDateFormatter.new
39
+ date_style = date_style.nsdatestyle if date_style.is_a? Symbol
40
+ time_style = time_style.nsdatestyle if time_style.is_a? Symbol
41
+ date_formatter.setDateStyle(date_style)
42
+ date_formatter.setTimeStyle(time_style)
43
+ date_formatter.stringFromDate(self)
44
+ end
45
+
46
+ # Pass in a format string or a Symbol. The Symbol must exist in
47
+ # NSDate::SugarCubeFormats.
48
+ #
49
+ # See
50
+ # <https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1>
51
+ # and
52
+ # <http://www.unicode.org/reports/tr35/tr35-19.html#Date_Field_Symbol_Table>
53
+ # for more information about date format strings.
54
+ def string_with_format(format, options={})
55
+ locale = options[:locale] || NSLocale.currentLocale
56
+ timezone = options[:timezone] || NSTimeZone.defaultTimeZone
57
+
58
+ if format.is_a?(Symbol)
59
+ formatters = SugarCubeFormats[format]
60
+ raise "No format found for #{format.inspect}" unless formatters
61
+ locale = NSLocale.localeWithLocaleIdentifier "en_US"
62
+ retval = ''
63
+ formatters.each do |formatter|
64
+ case formatter
65
+ when Symbol
66
+ retval << string_with_format(formatter.to_s, locale:locale, timezone:timezone)
67
+ when String
68
+ retval << formatter
69
+ end
70
+ end
71
+ return retval
72
+ else
73
+ format_template = NSDateFormatter.dateFormatFromTemplate(format, options:0,
74
+ locale:locale)
75
+ date_formatter = NSDateFormatter.new
76
+ date_formatter.setDateFormat(format_template)
77
+ date_formatter.setTimeZone(timezone)
78
+ return date_formatter.stringFromDate(self)
79
+ end
80
+ end
81
+
82
+ def upto(last_date, delta={days: 1}, &block)
83
+ return if last_date < self
84
+
85
+ date = self
86
+ while date <= last_date
87
+ if block.arity == 0
88
+ block.call
89
+ else
90
+ block.call(date)
91
+ end
92
+ new_date = date.delta(delta)
93
+ break if new_date <= date
94
+ date = new_date
95
+ end
96
+ end
97
+
98
+ def downto(last_date, delta={days: -1}, &block)
99
+ return if last_date > self
100
+
101
+ date = self
102
+ while date >= last_date
103
+ if block.arity == 0
104
+ block.call
105
+ else
106
+ block.call(date)
107
+ end
108
+ new_date = date.delta(delta)
109
+ break if new_date >= date
110
+ date = new_date
111
+ end
112
+ end
113
+
114
+ def timezone
115
+ return _calendar_components(NSTimeZoneCalendarUnit).timeZone
116
+ end
117
+ alias timeZone timezone
118
+
119
+ def era
120
+ return _calendar_components(NSEraCalendarUnit).era
121
+ end
122
+
123
+ def today?
124
+ today = self.class.new
125
+ return same_day?(today)
126
+ end
127
+
128
+ def same_day?(other)
129
+ return other.day == self.day &&
130
+ other.month == self.month &&
131
+ other.year == self.year &&
132
+ other.era == self.era
133
+ end
134
+
135
+ # In the rare case you actually get an NSDate object - not a Time object - this
136
+ # method is actually useful.
137
+ def utc_offset
138
+ return self.timezone.secondsFromGMT
139
+ end
140
+
141
+ def leap_year?
142
+ self.year % 4 == 0 and self.year % 100 != 0 or self.year % 400 == 0
143
+ end
144
+
145
+ # (main)> t = Time.new
146
+ # => 2012-09-27 11:29:12 +0900
147
+ # (main)> t.time_array
148
+ # => [2012, 9, 27]
149
+ def date_array
150
+ return [self.year, self.month, self.day]
151
+ end
152
+
153
+ # (main)> t = Time.new
154
+ # => 2012-09-27 11:29:12 +0900
155
+ # (main)> t.time_array
156
+ # => [11, 29, 12]
157
+ def time_array
158
+ return [self.hour, self.min, self.sec]
159
+ end
160
+
161
+ # (main)> t = Time.new
162
+ # => 2012-09-27 11:29:12 +0900
163
+ # (main)> t.time_array
164
+ # => [2012, 9, 12, 11, 29, 12]
165
+ def datetime_array
166
+ return [self.year, self.month, self.day, self.hour, self.min, self.sec]
167
+ end
168
+
169
+ # (main)> t = Time.new
170
+ # => 2012-09-27 11:29:12 +0900
171
+ # (main)> t.start_of_day
172
+ # => 2012-09-27 00:00:00 +0900
173
+ def start_of_day
174
+ date_components = NSDateComponents.new
175
+ date_components.hour = 0
176
+ date_components.minute = 0
177
+ date_components.second = 0
178
+ date_components.day = self.day
179
+ date_components.month = self.month
180
+ date_components.year = self.year
181
+
182
+ calendar = NSCalendar.alloc.initWithCalendarIdentifier(NSGregorianCalendar)
183
+ calendar.timeZone = NSTimeZone.timeZoneForSecondsFromGMT(self.utc_offset)
184
+ date = calendar.dateFromComponents(date_components)
185
+
186
+ return date
187
+ end
188
+
189
+ # (main)> t = Time.new
190
+ # => 2012-09-27 11:29:12 +0900
191
+ # (main)> t.end_of_day
192
+ # => 2012-09-28 00:00:00 +0900
193
+ def end_of_day
194
+ return self.delta(days: 1).start_of_day
195
+ end
196
+
197
+ # (main)> t = Time.new
198
+ # => 2012-09-27 11:29:12 +0900
199
+ # (main)> t.start_of_week
200
+ # => 2012-09-23 00:00:00 +0900
201
+ def start_of_week(start_day=nil)
202
+ result = self - days_to_week_start(start_day).days
203
+ result.start_of_day
204
+ end
205
+
206
+ # (main)> t = Time.new
207
+ # => 2012-09-27 11:29:12 +0900
208
+ # (main)> t.start_of_week
209
+ # => 2012-09-30 00:00:00 +0900
210
+ def end_of_week(start_day=nil)
211
+ result = self - days_to_week_start(start_day).days + 6.days
212
+ result.end_of_day
213
+ end
214
+
215
+ # (main)> t = Time.new
216
+ # => 2012-09-27 11:29:12 +0900
217
+ # (main)> t.start_of_month
218
+ # => 2012-09-01 00:00:00 +0900
219
+ def start_of_month
220
+ return self.start_of_day.delta(days:1 - day)
221
+ end
222
+
223
+ # (main)> t = Time.new
224
+ # => 2012-09-27 11:29:12 +0900
225
+ # (main)> t.end_of_month
226
+ # => 2012-10-01 00:00:00 +0900
227
+ def end_of_month
228
+ return self.end_of_day.delta(days:days_in_month - day)
229
+ end
230
+
231
+ def days_in_month
232
+ NSCalendar.currentCalendar.rangeOfUnit(NSDayCalendarUnit, inUnit:NSMonthCalendarUnit, forDate:self).length
233
+ end
234
+
235
+ def days_in_year
236
+ leap_year? ? 366 : 365
237
+ end
238
+
239
+ private
240
+ def _calendar_components(components)
241
+ return NSCalendar.currentCalendar.components(components, fromDate:self)
242
+ end
243
+
244
+ def days_to_week_start(start_day=nil)
245
+ start_day_number = week_day_index(start_day) || local_week_start
246
+ current_day_number = _calendar_components(NSWeekdayCalendarUnit).weekday
247
+ (current_day_number - start_day_number) % 7
248
+ end
249
+
250
+ def local_week_start
251
+ NSCalendar.currentCalendar.firstWeekday
252
+ end
253
+
254
+ def week_day_index(week_day=:monday)
255
+ day = week_day.to_s.capitalizedString
256
+ index = NSDateFormatter.new.weekdaySymbols.index(day)
257
+ return nil unless index
258
+ index + 1
259
+ end
260
+
261
+ end
@@ -0,0 +1,73 @@
1
+ class NSString
2
+
3
+ def nsurl
4
+ @url ||= NSURL.alloc.initWithString(self)
5
+ end
6
+
7
+ # @return [UIColor]
8
+ def uicolor(alpha=nil)
9
+ if self[0,1] == '#'
10
+ if self.length == 4
11
+ return (self[1] * 2 + self[2] * 2 + self[3] * 2).to_i(16).uicolor(alpha)
12
+ end
13
+ return self[1..-1].to_i(16).uicolor(alpha)
14
+ end
15
+
16
+ img = UIImage.imageNamed(self)
17
+ img && img.uicolor(alpha)
18
+ end
19
+
20
+ def cgcolor(alpha=nil)
21
+ uicolor(alpha).CGColor
22
+ end
23
+
24
+ # @return [UIImage]
25
+ def uiimage
26
+ UIImage.imageNamed(self).tap do |retval|
27
+ NSLog("No image named #{self}") unless retval
28
+ end
29
+ end
30
+
31
+ # @return [UIFont]
32
+ def uifont(size=nil)
33
+ size ||= UIFont.systemFontSize
34
+ UIFont.fontWithName(self, size:size)
35
+ end
36
+
37
+ def height(hash={})
38
+ font_size = hash[:size] || 16
39
+ width = hash[:width] || 320
40
+ font = UIFont.systemFontOfSize(font_size)
41
+ #size = self.sizeWithFont(font, constrainedToSize:[width, 10000], lineBreakMode:NSLineBreakByCharWrapping)
42
+ rect = self.boundingRectWithSize([width, 10000],
43
+ options:NSStringDrawingUsesLineFragmentOrigin,
44
+ attributes:{NSFontAttributeName => font},
45
+ context:nil)
46
+ rect.size.height.to_i + 1
47
+ end
48
+
49
+ # @param font [UIFont] Optional, defaults to UIFont.systemFontOfSize(UIFont.systemFontSize)
50
+ # @return [UILabel]
51
+ def uilabel(hash={})
52
+ font_size = hash[:size] || 16
53
+ color= hash[:color].uicolor || UIColor.blackColor
54
+ left = hash[:l] || hash[:left] || 0
55
+ top = hash[:t] || hash[:top] || 0
56
+
57
+ font = UIFont.systemFontOfSize(font_size)
58
+ size = self.sizeWithFont(font)
59
+ label = UILabel.alloc.initWithFrame([[left, top], size])
60
+ label.text = self
61
+ label.font = font
62
+ label.textColor = color
63
+ # why isn't this just the default!?
64
+ label.backgroundColor = :clear.uicolor
65
+ label
66
+ end
67
+
68
+ # @return [UIImageView]
69
+ def uiimageview
70
+ self.uiimage.uiimageview
71
+ end
72
+
73
+ end