sugarcube 0.11 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -17,7 +17,7 @@ view << subview.
17
17
 
18
18
  The basic idea of sugarcube is to turn some operations on their head. Insead of
19
19
 
20
- UIApplication.sharedApplication.openURL(NSUrl.URLWithString(url))
20
+ UIApplication.sharedApplication.openURL(NSURL.URLWithString(url))
21
21
 
22
22
  How about:
23
23
 
@@ -70,6 +70,16 @@ Examples
70
70
  # create a UIColor from a hex value
71
71
  0xffffff.uicolor # => UIColor.colorWithRed(1.0, green:1.0, blue:1.0, alpha:1.0)
72
72
  0xffffff.uicolor(0.5) # => UIColor.colorWithRed(1.0, green:1.0, blue:1.0, alpha:0.5)
73
+
74
+ # some number-to-string stuff
75
+ 1.nth # => 'st'
76
+ 2.nth # => 'nd'
77
+ 3.nth # => 'rd'
78
+ 4.nth # => 'th'
79
+ 11.nth # => 'th'
80
+ 13.nth # => 'th'
81
+ 21.nth # => 'st'
82
+ 23.nth # => 'rd'
73
83
  ```
74
84
 
75
85
  Numeric
@@ -86,6 +96,12 @@ Examples
86
96
  # convert multiples of pi
87
97
  2.pi # => 2 * Math::PI
88
98
  0.5.pi # => 0.5 * Math::PI
99
+
100
+ # if you thought conversion from degrees to radians looks weird, you'll hate
101
+ # conversion from meters to miles:
102
+ distance = 1500 # this is in meters. why? because all the methods that return
103
+ # a "distance" return it in meters
104
+ distance.miles # => 0.932056427001953
89
105
  ```
90
106
 
91
107
  NSCoder
@@ -375,6 +391,10 @@ image.uicolor # => UIColor.colorWithPatternImage(image)
375
391
  image.scale_to [37, 37]
376
392
  image.rounded # default: 5 pt radius
377
393
  image.rounded(10)
394
+
395
+ # these both use UIEdgeInsetsZero (for now)
396
+ image.tileable
397
+ image.stretchable
378
398
  ```
379
399
 
380
400
  UIAlertView
@@ -677,6 +697,10 @@ end
677
697
  # hemorrhaging):
678
698
  1.month || 2.months # 1.month = 30 days
679
699
  1.year || 2.years # 1.year = 365 days
700
+
701
+ # some comparison methods
702
+ date1.today?
703
+ date2.same_day? date1
680
704
  ```
681
705
 
682
706
  NSUserDefaults
@@ -836,6 +860,28 @@ CGRect([0.0, 0.0],{1.0 × 1.0})
836
860
 
837
861
  [CGGeometry Reference]: https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html
838
862
 
863
+ CoreLocation
864
+ --------------
865
+
866
+ Open up `CLLocationCoordinate2D` to provide handy-dandies
867
+
868
+ ```ruby
869
+ > denver_co = CLLocationCoordinate2D.new(39.739188,-104.985223)
870
+ => #<CLLocationCoordinate2D latitude=39.7391815185547 longitude=-104.985198974609>
871
+ > loveland_oh = CLLocationCoordinate2D.new(39.268128,-84.257648)
872
+ => #<CLLocationCoordinate2D latitude=39.2681274414062 longitude=-84.2576293945312>
873
+ > denver_co.distance_to(loveland_oh)
874
+ => 1773425.5 # in meters
875
+ > denver_co.distance_to(loveland_oh).miles
876
+ => 1101.955078125
877
+ > denver_co.delta_miles(1101.6, -32.556)
878
+ => #<CLLocationCoordinate2D latitude=39.2681427001953 longitude=-84.2577209472656>
879
+ > denver_co.delta_miles(1101.6, -32.556).distance_to(loveland_oh)
880
+ => 8.0804328918457 # this is in meters
881
+ > denver_co.delta_miles(1101.6, -32.556).distance_to(loveland_oh).miles
882
+ => 0.00502094626426697
883
+ ```
884
+
839
885
  REPL View adjustments
840
886
  -----------------------
841
887
 
@@ -18,9 +18,7 @@ module SugarCube
18
18
  return @@sugarcube_view if not view
19
19
 
20
20
  if view.is_a? Fixnum
21
- @@sugarcube_items ||= nil
22
- raise "no views have been assigned to SugarCube::Adjust::tree" unless @@sugarcube_items
23
-
21
+ @@sugarcube_items ||= SugarCube::Adjust::build_tree(UIApplication.sharedApplication.keyWindow, :subviews)
24
22
  view = @@sugarcube_items[view]
25
23
  end
26
24
 
@@ -283,7 +281,7 @@ module SugarCube
283
281
  end
284
282
  end
285
283
 
286
- @@sugarcube_items = []
284
+ @@sugarcube_items = SugarCube::Adjust::build_tree(item, selector)
287
285
  if self.respond_to? :draw_tree
288
286
  total = draw_tree(item, selector)
289
287
  else
@@ -295,8 +293,6 @@ module SugarCube
295
293
  end
296
294
 
297
295
  def draw_tree(item, selector, tab=nil, is_last=true, items_index=0)
298
- @@sugarcube_items << item
299
-
300
296
  space = ' '
301
297
  if items_index < 10
302
298
  print ' '
@@ -346,6 +342,19 @@ module SugarCube
346
342
  return items_index
347
343
  end
348
344
 
345
+ def build_tree(item, selector)
346
+ if selector.is_a? Proc
347
+ items = selector.call(item)
348
+ else
349
+ items = item.send(selector)
350
+ end
351
+ ret = [item]
352
+ items.each_with_index { |subview, index|
353
+ ret.concat SugarCube::Adjust::build_tree(subview, selector)
354
+ }
355
+ ret
356
+ end
357
+
349
358
  def root
350
359
  (UIApplication.sharedApplication.keyWindow || UIApplication.sharedApplication.windows[0]).rootViewController
351
360
  end
@@ -0,0 +1,29 @@
1
+ # denver = CLLocationCoordinate2D.new(39.764032,-104.963112)
2
+ # cincinnati = CLLocationCoordinate2D.new(39.267024,-84.251736)
3
+ # denver.distance_to(cincinnati).miles
4
+ # denver.delta_miles()
5
+ class CLLocationCoordinate2D
6
+
7
+ def delta_miles(dx, dy)
8
+ earth_radius = 3960.0
9
+
10
+ radius_at_latitude = earth_radius * Math.cos(self.latitude * Math::PI / 180.0)
11
+ if radius_at_latitude > 0
12
+ delta_lng = dx / radius_at_latitude
13
+ delta_lng *= 180.0 / Math::PI
14
+ else
15
+ delta_lng = 0
16
+ end
17
+
18
+ delta_lat = dy / earth_radius
19
+ delta_lat *= 180.0 / Math::PI
20
+ CLLocationCoordinate2D.new(self.latitude + delta_lat, self.longitude + delta_lng)
21
+ end
22
+
23
+ def distance_to(cl_location_2d)
24
+ my_location = CLLocation.alloc.initWithLatitude(self.latitude, longitude:self.longitude)
25
+ other_location = CLLocation.alloc.initWithLatitude(cl_location_2d.latitude, longitude:cl_location_2d.longitude)
26
+ my_location.distanceFromLocation(other_location)
27
+ end
28
+
29
+ end
@@ -15,4 +15,21 @@ class Fixnum
15
15
  UIColor.colorWithRed(red, green:green, blue:blue, alpha:alpha.to_f)
16
16
  end
17
17
 
18
+ def nth
19
+ # if the first two digits of rank are between 11 and 20, it's an
20
+ # 'up-teenth' kinda number
21
+ if self % 100 < 10 || self % 100 > 20
22
+ case self % 10
23
+ when 1
24
+ return "st"
25
+ when 2
26
+ return "nd"
27
+ when 3
28
+ return "rd"
29
+ end
30
+ end
31
+
32
+ return "th"
33
+ end
34
+
18
35
  end
@@ -5,6 +5,14 @@ module SugarCube
5
5
  UIApplication.sharedApplication.keyWindow.rootViewController.presentViewController(view_ctlr, animated:true, completion:block)
6
6
  end
7
7
 
8
+ def present_modal_in_nav(view_ctlr, &block)
9
+ ctlr = UINavigationController.alloc.initWithRootViewController(view_ctlr)
10
+ ctlr.modalTransitionStyle = UIModalTransitionStyleCoverVertical
11
+
12
+ SugarCube::Modal.present_modal(ctlr, &block)
13
+ ctlr
14
+ end
15
+
8
16
  def dismiss_modal(&block)
9
17
  UIApplication.sharedApplication.keyWindow.rootViewController.dismissViewControllerAnimated(true, completion:block)
10
18
  end
@@ -4,6 +4,22 @@ class NSDate
4
4
  end
5
5
  alias timeZone timezone
6
6
 
7
+ def era
8
+ return _calendar_components(NSEraCalendarUnit).era
9
+ end
10
+
11
+ def today?
12
+ today = self.class.new
13
+ return same_day?(today)
14
+ end
15
+
16
+ def same_day?(other)
17
+ return other.day == self.day &&
18
+ other.month == self.month &&
19
+ other.year == self.year &&
20
+ other.era == self.era
21
+ end
22
+
7
23
  # In the rare case you actually get an NSDate object - not a Time object - this
8
24
  # method is actually useful.
9
25
  def utc_offset
@@ -0,0 +1,25 @@
1
+ class NSString
2
+
3
+ def document
4
+ @@sugarcube_docs ||= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0]
5
+ return self if self.hasPrefix(@@sugarcube_docs)
6
+
7
+ @@sugarcube_docs.stringByAppendingPathComponent(self)
8
+ end
9
+
10
+ def exists?
11
+ NSFileManager.defaultManager.fileExistsAtPath(self.document)
12
+ end
13
+
14
+ def resource
15
+ @@sugarcube_resources ||= NSBundle.mainBundle.resourcePath
16
+ return self if self.hasPrefix(@@sugarcube_resources)
17
+
18
+ @@sugarcube_resources.stringByAppendingPathComponent(self)
19
+ end
20
+
21
+ def resource_exists?
22
+ NSFileManager.defaultManager.fileExistsAtPath(self.resource)
23
+ end
24
+
25
+ end
@@ -1,11 +1,11 @@
1
1
  class NSURL
2
2
 
3
3
  def open
4
- NSUIApplication.sharedApplication.openURL(NSUrl.URLWithString(self))
4
+ UIApplication.sharedApplication.openURL(NSURL.URLWithString(self))
5
5
  end
6
6
 
7
7
  def can_open?
8
- NSUIApplication.sharedApplication.canOpenURL(NSUrl.URLWithString(self))
8
+ UIApplication.sharedApplication.canOpenURL(NSURL.URLWithString(self))
9
9
  end
10
10
 
11
11
  end
@@ -4,6 +4,10 @@ class Numeric
4
4
  self / 100.0
5
5
  end
6
6
 
7
+ def radians
8
+ self
9
+ end
10
+
7
11
  def degrees
8
12
  self / 180.0 * Math::PI
9
13
  end
@@ -12,4 +16,20 @@ class Numeric
12
16
  self * Math::PI
13
17
  end
14
18
 
19
+ def meters
20
+ self
21
+ end
22
+
23
+ def kilometers
24
+ self / 1000.0
25
+ end
26
+
27
+ def miles
28
+ self * 0.000621371
29
+ end
30
+
31
+ def feet
32
+ self * 3.28084
33
+ end
34
+
15
35
  end
data/lib/sugarcube/ror.rb CHANGED
@@ -6,4 +6,8 @@ class Object
6
6
  def blank?
7
7
  respond_to?(:empty?) ? empty? : !self
8
8
  end
9
+
10
+ def present?
11
+ !blank?
12
+ end
9
13
  end
@@ -52,6 +52,11 @@ class Symbol
52
52
  attr_accessor :tableview_cellstyles
53
53
  attr_accessor :tableview_cellaccessorytype
54
54
  attr_accessor :tableview_cellselectionstyle
55
+
56
+ attr_accessor :image_sourcetypes
57
+ attr_accessor :image_capturemode
58
+ attr_accessor :image_cameradevice
59
+ attr_accessor :image_quality
55
60
  end
56
61
 
57
62
  @devices = {
@@ -313,6 +318,32 @@ class Symbol
313
318
  bottom: UIViewAutoresizingFlexibleBottomMargin,
314
319
  }
315
320
 
321
+ @image_sourcetypes = {
322
+ camera: UIImagePickerControllerSourceTypeCamera,
323
+ library: UIImagePickerControllerSourceTypePhotoLibrary,
324
+ album: UIImagePickerControllerSourceTypeSavedPhotosAlbum,
325
+ }
326
+ @image_capturemode = {
327
+ photo: UIImagePickerControllerCameraCaptureModePhoto,
328
+ video: UIImagePickerControllerCameraCaptureModeVideo,
329
+ }
330
+ @image_cameradevice = {
331
+ front: UIImagePickerControllerCameraDeviceFront,
332
+ rear: UIImagePickerControllerCameraDeviceRear,
333
+ }
334
+ @image_quality = {
335
+ high: UIImagePickerControllerQualityTypeHigh,
336
+ medium: UIImagePickerControllerQualityTypeMedium,
337
+ low: UIImagePickerControllerQualityTypeLow,
338
+ vga: UIImagePickerControllerQualityType640x480,
339
+ i1280x720: UIImagePickerControllerQualityTypeIFrame1280x720,
340
+ i1280: UIImagePickerControllerQualityTypeIFrame1280x720,
341
+ i720: UIImagePickerControllerQualityTypeIFrame1280x720,
342
+ i960x540: UIImagePickerControllerQualityTypeIFrame960x540,
343
+ i960: UIImagePickerControllerQualityTypeIFrame960x540,
344
+ i540: UIImagePickerControllerQualityTypeIFrame960x540,
345
+ }
346
+
316
347
  private
317
348
  def look_in(here)
318
349
  return here[self] if here.has_key? self
@@ -433,6 +464,27 @@ class Symbol
433
464
  end
434
465
  alias uiviewautoresizing uiautoresize
435
466
 
467
+ def uiimagesource
468
+ look_in(Symbol.image_sourcetypes)
469
+ end
470
+ alias uiimagesourcetype uiimagesource
471
+
472
+ def uiimagecapture
473
+ look_in(Symbol.image_capturemode)
474
+ end
475
+ alias uiimagecapturemode uiimagecapture
476
+
477
+ def uiimagecamera
478
+ look_in(Symbol.image_cameradevice)
479
+ end
480
+ alias uiimagecameradevice uiimagecamera
481
+ alias uiimagedevice uiimagecamera
482
+
483
+ def uiimagequality
484
+ look_in(Symbol.image_quality)
485
+ end
486
+ alias uiimagequalitytype uiimagequality
487
+
436
488
  def uifont(size=UIFont.systemFontSize)
437
489
  # system fonts
438
490
  if Symbol.system_fonts.has_key? self
@@ -39,11 +39,11 @@ class Numeric
39
39
  alias year years
40
40
 
41
41
  def later(user_info=nil, &fire)
42
- NSTimer.scheduledTimerWithTimeInterval(self.to_f, target: fire, selector: 'call:', userInfo: user_info, repeats: false)
42
+ NSTimer.scheduledTimerWithTimeInterval(self, target: fire, selector: 'call:', userInfo: user_info, repeats: false)
43
43
  end
44
44
 
45
45
  def every(user_info=nil, &fire)
46
- NSTimer.scheduledTimerWithTimeInterval(self.to_f, target: fire, selector: 'call:', userInfo: user_info, repeats: true)
46
+ NSTimer.scheduledTimerWithTimeInterval(self, target: fire, selector: 'call:', userInfo: user_info, repeats: true)
47
47
  end
48
48
  end
49
49
 
@@ -2,10 +2,16 @@ class UIView
2
2
 
3
3
  def to_s(options={})
4
4
  options[:superview] = true if options[:superview].nil?
5
+ if self.respond_to? :stylename and self.stylename
6
+ suffix = ' stylename: ' + self.stylename.inspect
7
+ else
8
+ suffix = ''
9
+ end
5
10
  "#{self.class.name}(##{self.object_id.to_s(16)}, #{SugarCube::Adjust::format_frame(self.frame)}" +
6
11
  (options[:inner] ? ', ' + options[:inner] : '') +
7
12
  ')' +
8
- (options[:superview] && self.superview ? ", child of #{self.superview.class.name}(##{self.superview.object_id.to_s(16)})" : '')
13
+ (options[:superview] && self.superview ? ", child of #{self.superview.class.name}(##{self.superview.object_id.to_s(16)})" : '') +
14
+ suffix
9
15
  end
10
16
 
11
17
  end
@@ -36,4 +36,12 @@ class UIImage
36
36
  return image
37
37
  end
38
38
 
39
+ def tileable
40
+ resizableImageWithCapInsets(UIEdgeInsetsZero, resizingMode:UIImageResizingModeTile)
41
+ end
42
+
43
+ def stretchable
44
+ resizableImageWithCapInsets(UIEdgeInsetsZero, resizingMode:UIImageResizingModeStretch)
45
+ end
46
+
39
47
  end
@@ -4,7 +4,12 @@ class UIViewController
4
4
  self.addChildViewController(view_controller)
5
5
  self
6
6
  end
7
- alias << push
7
+
8
+ # `alias << push` won't work. it doesn't "respect" subclasses overriding the
9
+ # `push` method.
10
+ def <<(view_controller)
11
+ push view_controller
12
+ end
8
13
 
9
14
  end
10
15
 
@@ -15,7 +20,6 @@ class UINavigationController
15
20
  self.pushViewController(view_controller, animated: true)
16
21
  self
17
22
  end
18
- alias << push
19
23
 
20
24
  def pop(to_view=nil)
21
25
  if to_view
@@ -36,6 +40,5 @@ class UITabBarController
36
40
  self.setViewControllers(view_controllers, animated: true)
37
41
  self
38
42
  end
39
- alias << push
40
43
 
41
44
  end
@@ -1,3 +1,3 @@
1
1
  module SugarCube
2
- Version = '0.11'
2
+ Version = '0.11.1'
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.11'
4
+ version: 0.11.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-10-24 00:00:00.000000000 Z
13
+ date: 2012-11-01 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: ! '== Description
16
16
 
@@ -47,8 +47,8 @@ files:
47
47
  - lib/sugarcube/adjust.rb
48
48
  - lib/sugarcube/array.rb
49
49
  - lib/sugarcube/core_graphics.rb
50
+ - lib/sugarcube/core_location.rb
50
51
  - lib/sugarcube/defaults.rb
51
- - lib/sugarcube/document.rb
52
52
  - lib/sugarcube/exceptions.rb
53
53
  - lib/sugarcube/fixnum.rb
54
54
  - lib/sugarcube/modal.rb
@@ -59,6 +59,7 @@ files:
59
59
  - lib/sugarcube/nsindexpath.rb
60
60
  - lib/sugarcube/nsindexset.rb
61
61
  - lib/sugarcube/nsstring.rb
62
+ - lib/sugarcube/nsstring_files.rb
62
63
  - lib/sugarcube/nsurl.rb
63
64
  - lib/sugarcube/nsuserdefaults.rb
64
65
  - lib/sugarcube/numeric.rb
@@ -1,14 +0,0 @@
1
- class NSString
2
-
3
- def document
4
- @@docs ||= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0]
5
- return self if self.hasPrefix(@@docs)
6
-
7
- @@docs.stringByAppendingPathComponent(self)
8
- end
9
-
10
- def exists?
11
- NSFileManager.defaultManager.fileExistsAtPath(self.document)
12
- end
13
-
14
- end