sugarcube 0.20.7 → 0.20.8

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sugarcube (0.20.6)
4
+ sugarcube (0.20.7)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -314,6 +314,15 @@ recurring events)
314
314
  (main)> feb_1_2013.end_of_day
315
315
  # NOTE! end_of_day is the NEXT DAY. this is not an accident, it makes comparisons cleaner. deal with it.
316
316
  => 2013-02-02 00:00:00 -0700
317
+ (main)> feb_1_2013.start_of_week # in the USA, start of week is Sunday
318
+ => 2013-01-27 00:00:00 -0700
319
+ => 2013-01-28 00:00:00 -0700 # in most other countries you will get Monday
320
+ (main)> feb_1_2013.start_of_week(:monday) # or you can specify it!
321
+ => 2013-01-28 00:00:00 -0700
322
+ (main)> feb_1_2013.end_of_week # Just like end_of_day, end_of_week returns midnight of the *next day*
323
+ => 2013-02-03 00:00:00 -0700
324
+ (main)> feb_1_2013.end_of_week(:monday)
325
+ => 2013-02-04 00:00:00 -0700
317
326
  (main)> feb_1_2013.days_in_month
318
327
  => 28
319
328
  (main)> feb_1_2013.days_in_year
@@ -661,21 +670,25 @@ some problems with on RubyMotion (it worked, but not *always*. Very strange).
661
670
  UIAlertView
662
671
  --------
663
672
 
664
- Accepts multiple buttons and success and cancel handlers. In its simplest
665
- form, you can pass just a title and block.
673
+ Accepts multiple buttons and handlers. In its simplest form, you can pass just
674
+ a title and block.
666
675
 
667
676
  ```ruby
668
677
  # simple
669
678
  UIAlertView.alert "This is happening, OK?" { self.happened! }
670
- # a little more complex
679
+
680
+ # a little more complex - the cancel button should be first, and the block will
681
+ # receive a string, not an index
671
682
  UIAlertView.alert("This is happening, OK?", buttons: ["Nevermind", "OK"],
672
- message: "Don't worry, it'll be fine.") {
673
- self.happened!
683
+ message: "Don't worry, it'll be fine.") { |button|
684
+ if button == "OK"
685
+ self.happened!
686
+ end
674
687
  }
675
688
 
676
- # Full on whiz-bangery. Note the success block takes the pressed button, but as
677
- # a string instead of an index. The cancel button should be the first entry in
678
- # `buttons:`
689
+ # Full on whiz-bangery. The cancel button should be the first entry in
690
+ # `buttons:`. When you specify the success and cancel button handlers this way,
691
+ # you need not assign both.
679
692
  UIAlertView.alert "I mean, is this cool?", buttons: %w[No! Sure! Hmmmm],
680
693
  message: "No going back now",
681
694
  cancel: proc { self.cancel },
@@ -244,9 +244,19 @@ module SugarCube
244
244
  end
245
245
  alias h shadow
246
246
 
247
- ##| TREE
247
+ # @param item this can be a tree-like item (UIView, UIViewController,
248
+ # CALayer) or an integer, in which case it will select that window from
249
+ # UIApplication.sharedApplication.window. Defalt is to display the
250
+ # keyWindow
251
+ # @param selector If you pass an unsupported object to tree, you will need
252
+ # to pass a selector as well - this method should return an array of
253
+ # items which are passed recursively to tree
254
+ # @block sel_blk If a block is passed, it will be used to build the array of
255
+ # items that are called recursively
248
256
  def tree(item=nil, selector=nil, &sel_blk)
249
- unless item
257
+ if item.is_a?(Fixnum)
258
+ item = UIApplication.sharedApplication.windows[item]
259
+ elsif ! item
250
260
  item = UIApplication.sharedApplication.keyWindow
251
261
  end
252
262
  if not item
@@ -291,6 +301,7 @@ module SugarCube
291
301
  return item
292
302
  end
293
303
 
304
+ # Draws the tree items
294
305
  def draw_tree(item, selector, tab=nil, is_last=true, items_index=0)
295
306
  space = ' '
296
307
  if items_index < 10
@@ -30,5 +30,5 @@ class NSData
30
30
  false
31
31
  end
32
32
  end
33
-
33
+
34
34
  end
@@ -1,5 +1,5 @@
1
1
  class NSDate
2
- # these formatters are used in `string_with_format`. Symbols are convertd to
2
+ # these formatters are used in `string_with_format`. Symbols are converted to
3
3
  # a string using string_with_format's templating, and strings are concatenated
4
4
  # as-is
5
5
  SugarCubeFormats = {
@@ -174,6 +174,24 @@ class NSDate
174
174
  return self + time_interval
175
175
  end
176
176
 
177
+ # (main)> t = Time.new
178
+ # => 2012-09-27 11:29:12 +0900
179
+ # (main)> t.start_of_week
180
+ # => 2012-09-23 00:00:00 +0900
181
+ def start_of_week(start_day=nil)
182
+ result = self - days_to_week_start(start_day).days
183
+ result.start_of_day
184
+ end
185
+
186
+ # (main)> t = Time.new
187
+ # => 2012-09-27 11:29:12 +0900
188
+ # (main)> t.start_of_week
189
+ # => 2012-09-30 00:00:00 +0900
190
+ def end_of_week(start_day=nil)
191
+ result = self - days_to_week_start(start_day).days + 6.days
192
+ result.end_of_day
193
+ end
194
+
177
195
  # (main)> t = Time.new
178
196
  # => 2012-09-27 11:29:12 +0900
179
197
  # (main)> t.start_of_month
@@ -202,4 +220,22 @@ class NSDate
202
220
  def _calendar_components(components)
203
221
  return NSCalendar.currentCalendar.components(components, fromDate:self)
204
222
  end
223
+
224
+ def days_to_week_start(start_day=nil)
225
+ start_day_number = week_day_index(start_day) || local_week_start
226
+ current_day_number = _calendar_components(NSWeekdayCalendarUnit).weekday
227
+ (current_day_number - start_day_number) % 7
228
+ end
229
+
230
+ def local_week_start
231
+ NSCalendar.currentCalendar.firstWeekday
232
+ end
233
+
234
+ def week_day_index(week_day=:monday)
235
+ day = week_day.to_s.capitalizedString
236
+ index = NSDateFormatter.new.weekdaySymbols.index(day)
237
+ return nil unless index
238
+ index + 1
239
+ end
240
+
205
241
  end
@@ -1,4 +1,5 @@
1
1
  class NSIndexPath
2
+
2
3
  def to_a
3
4
  indexes_ptr = Pointer.new(:uint, self.length)
4
5
  self.getIndexes indexes_ptr
@@ -8,9 +9,17 @@ class NSIndexPath
8
9
  end
9
10
  a
10
11
  end
12
+
13
+ def to_s
14
+ "<NSIndexPath #{to_a.to_s}>"
15
+ end
16
+
11
17
  end
12
18
 
13
19
 
20
+ # This class is used in `switch` statements. Easy pattern matching, so instead
21
+ # of `if index_path.row...index_path.section`, you can use
22
+ # `when IndexPath[row, section]`
14
23
  class IndexPath
15
24
 
16
25
  def self.[] *values
@@ -4,7 +4,7 @@ class NSString
4
4
  def nsurl
5
5
  @url ||= NSURL.alloc.initWithString(self)
6
6
  end
7
-
7
+
8
8
  # @return [NSURL]
9
9
  def fileurl
10
10
  @fileurl ||= NSURL.fileURLWithPath(self)
@@ -12,4 +12,8 @@ class NSURL
12
12
  NSData.dataWithContentsOfURL(self)
13
13
  end
14
14
 
15
+ def nsurlrequest
16
+ NSURLRequest.requestWithURL(self)
17
+ end
18
+
15
19
  end
@@ -100,3 +100,14 @@ module SugarCube
100
100
  end
101
101
  end
102
102
  end
103
+
104
+
105
+ class NSTimer
106
+ def every(time, user_info=nil, &fire)
107
+ time.every user_info, &fire
108
+ end
109
+
110
+ def after(time, user_info=nil, &fire)
111
+ time.later user_info, &fire
112
+ end
113
+ end
@@ -15,8 +15,9 @@ class UIAlertView
15
15
 
16
16
  # create the delegate
17
17
  delegate = SugarCube::AlertViewDelegate.new
18
- delegate.on_success = options[:success] || block
18
+ delegate.on_success = options[:success]
19
19
  delegate.on_cancel = options[:cancel]
20
+ delegate.on_default = block
20
21
  delegate.send(:retain)
21
22
 
22
23
  args = [title] # initWithTitle:
@@ -26,10 +27,18 @@ class UIAlertView
26
27
  buttons = options[:buttons] || []
27
28
  if buttons.empty?
28
29
  # cancelButtonTitle: is first, so check for cancel
29
- buttons << "Cancel" if options[:cancel]
30
+ if options[:cancel]
31
+ buttons << "Cancel"
32
+ end
33
+
30
34
  # otherButtonTitles:
31
- buttons << "OK" if options[:success] or buttons.empty?
32
- elsif buttons.length == 1 and options[:cancel]
35
+ if buttons.empty?
36
+ buttons << nil # cancel button => nil
37
+ buttons << "OK" # other buttons => "OK"
38
+ elsif options[:success]
39
+ buttons << "OK"
40
+ end
41
+ elsif buttons.length == 1 && options[:cancel]
33
42
  raise "If you only have one button, use a :success handler, not :cancel (and definitely not BOTH)"
34
43
  end
35
44
 
@@ -37,7 +46,7 @@ class UIAlertView
37
46
  delegate.buttons = buttons
38
47
 
39
48
  # uses localized buttons in the actual alert
40
- args.concat(buttons.map{ |s| s.localized })
49
+ args.concat(buttons.map { |m| m && m.localized })
41
50
  args << nil # otherButtonTitles:..., nil
42
51
 
43
52
  alert = self.alloc
@@ -59,17 +68,24 @@ module SugarCube
59
68
  attr_accessor :buttons
60
69
  attr_accessor :on_cancel
61
70
  attr_accessor :on_success
71
+ attr_accessor :on_default
62
72
 
63
73
  def alertView(alert, didDismissWithButtonIndex:index)
64
- if index == alert.cancelButtonIndex && on_cancel
65
- on_cancel.call
66
- elsif on_success
67
- if on_success.arity == 0
68
- on_success.call
69
- else
70
- button = buttons[index]
71
- on_success.call(button)
72
- end
74
+ cancel_handler = on_cancel || on_default
75
+ success_handler = on_success || on_default
76
+ handler = nil
77
+
78
+ if index == alert.cancelButtonIndex
79
+ handler = cancel_handler
80
+ else
81
+ handler = success_handler
82
+ end
83
+
84
+ if handler.arity == 0
85
+ handler.call
86
+ else
87
+ button = buttons[index]
88
+ handler.call(button)
73
89
  end
74
90
 
75
91
  self.send(:autorelease)
@@ -1,3 +1,3 @@
1
1
  module SugarCube
2
- Version = '0.20.7'
2
+ Version = '0.20.8'
3
3
  end
@@ -0,0 +1,97 @@
1
+ class Object
2
+
3
+ private
4
+ def adjust(*args)
5
+ SugarCube::Adjust.adjust(*args)
6
+ end
7
+ def a(*args)
8
+ SugarCube::Adjust.adjust(*args)
9
+ end
10
+ def frame(*args)
11
+ SugarCube::Adjust.frame(*args)
12
+ end
13
+ def f(*args)
14
+ SugarCube::Adjust.frame(*args)
15
+ end
16
+ def left(*args)
17
+ SugarCube::Adjust.left(*args)
18
+ end
19
+ def l(*args)
20
+ SugarCube::Adjust.left(*args)
21
+ end
22
+ def right(*args)
23
+ SugarCube::Adjust.right(*args)
24
+ end
25
+ def r(*args)
26
+ SugarCube::Adjust.right(*args)
27
+ end
28
+ def up(*args)
29
+ SugarCube::Adjust.up(*args)
30
+ end
31
+ def u(*args)
32
+ SugarCube::Adjust.up(*args)
33
+ end
34
+ def down(*args)
35
+ SugarCube::Adjust.down(*args)
36
+ end
37
+ def d(*args)
38
+ SugarCube::Adjust.down(*args)
39
+ end
40
+ def origin(*args)
41
+ SugarCube::Adjust.origin(*args)
42
+ end
43
+ def o(*args)
44
+ SugarCube::Adjust.origin(*args)
45
+ end
46
+ def thinner(*args)
47
+ SugarCube::Adjust.thinner(*args)
48
+ end
49
+ def n(*args)
50
+ SugarCube::Adjust.thinner(*args)
51
+ end
52
+ def wider(*args)
53
+ SugarCube::Adjust.wider(*args)
54
+ end
55
+ def w(*args)
56
+ SugarCube::Adjust.wider(*args)
57
+ end
58
+ def shorter(*args)
59
+ SugarCube::Adjust.shorter(*args)
60
+ end
61
+ def s(*args)
62
+ SugarCube::Adjust.shorter(*args)
63
+ end
64
+ def taller(*args)
65
+ SugarCube::Adjust.taller(*args)
66
+ end
67
+ def t(*args)
68
+ SugarCube::Adjust.taller(*args)
69
+ end
70
+ def size(*args)
71
+ SugarCube::Adjust.size(*args)
72
+ end
73
+ def z(*args)
74
+ SugarCube::Adjust.size(*args)
75
+ end
76
+ def center(*args)
77
+ SugarCube::Adjust.center(*args)
78
+ end
79
+ def c(*args)
80
+ SugarCube::Adjust.center(*args)
81
+ end
82
+ def shadow(*args)
83
+ SugarCube::Adjust.shadow(*args)
84
+ end
85
+ def h(*args)
86
+ SugarCube::Adjust.shadow(*args)
87
+ end
88
+ def tree(*args)
89
+ SugarCube::Adjust.tree(*args)
90
+ end
91
+ def root(*args)
92
+ SugarCube::Adjust.root(*args)
93
+ end
94
+ def restore(*args)
95
+ SugarCube::Adjust.restore(*args)
96
+ end
97
+ end
@@ -0,0 +1,23 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "The sugarcube gem must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+
6
+ Motion::Project::App.setup do |app|
7
+ # scans app.files until it finds app/ (the default)
8
+ # if found, it inserts just before those files, otherwise it will insert to
9
+ # the end of the list
10
+ insert_point = 0
11
+ app.files.each_index do |index|
12
+ file = app.files[index]
13
+ if file =~ /^(?:\.\/)?app\//
14
+ # found app/, so stop looking
15
+ break
16
+ end
17
+ insert_point = index + 1
18
+ end
19
+
20
+ Dir.glob(File.join(File.dirname(__FILE__), 'sugarcube-repl/**/*.rb')).reverse.each do |file|
21
+ app.files.insert(insert_point, file)
22
+ end
23
+ end
data/spec/nsdata_spec.rb CHANGED
@@ -26,13 +26,13 @@ describe "NSData" do
26
26
  it "should be able to create a turkey string from data" do
27
27
  "\u00ab\u03c4\u03b1\u0411\u042c\u2113\u03c3\u00bb".nsdata.nsstring.should == "\u00ab\u03c4\u03b1\u0411\u042c\u2113\u03c3\u00bb"
28
28
  end
29
-
29
+
30
30
  describe "write_to" do
31
-
31
+
32
32
  after do
33
33
  "a-z".document.remove!
34
34
  end
35
-
35
+
36
36
  it "should write data to spcified path" do
37
37
  path = "a-z".document
38
38
  contents = (:a..:z).to_a.join
@@ -40,7 +40,7 @@ describe "NSData" do
40
40
  path.exists?.should == true
41
41
  path.fileurl.nsdata.nsstring.should == contents
42
42
  end
43
-
43
+
44
44
  it "should write data to spcified url" do
45
45
  url = NSURL.alloc.initFileURLWithPath "a-z".document
46
46
  contents = (:a..:z).to_a.join
@@ -49,7 +49,19 @@ describe "NSData" do
49
49
  path.exists?.should == true
50
50
  url.nsdata.nsstring.should == contents
51
51
  end
52
-
52
+
53
+ end
54
+
55
+ describe 'base64' do
56
+
57
+ it 'should convert nsstring data to base64' do
58
+ 'much longer test'.nsdata.base64.should == 'bXVjaCBsb25nZXIgdGVzdA=='
59
+ end
60
+
61
+ it 'should convert image data (PNG) to base64' do
62
+ 'test'.uiimage.nsdata.base64.should == 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAHgCAIAAADrGJBNAAAAHElEQVQ4jWO4a8TAxMAwikfxKB7Fo3gUj2I6YgAAiQTO+8u2PwAAAABJRU5ErkJggg=='
63
+ end
64
+
53
65
  end
54
66
 
55
67
  end
data/spec/nsdate_spec.rb CHANGED
@@ -118,6 +118,14 @@ describe "NSDate" do
118
118
  @date.end_of_day.datetime_array.should == [2013, 1, 3, 0, 0, 0]
119
119
  end
120
120
 
121
+ it "should have an NSDate#start_of_week method" do
122
+ @date.start_of_week(:sunday).datetime_array.should == [2012, 12, 30, 0, 0, 0]
123
+ end
124
+
125
+ it "should have an NSDate#end_of_week method" do
126
+ @date.end_of_week(:sunday).datetime_array.should == [2013, 1, 6, 0, 0, 0]
127
+ end
128
+
121
129
  it "should have an NSDate#start_of_month method" do
122
130
  @date.start_of_month.datetime_array.should == [2013, 1, 1, 0, 0, 0]
123
131
  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.20.7
4
+ version: 0.20.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2013-04-13 00:00:00.000000000 Z
16
+ date: 2013-04-19 00:00:00.000000000 Z
17
17
  dependencies: []
18
18
  description: ! '== Description
19
19
 
@@ -62,6 +62,8 @@ files:
62
62
  - lib/sugarcube-attributedstring/nsattributedstring.rb
63
63
  - lib/sugarcube-gestures.rb
64
64
  - lib/sugarcube-gestures/gestures.rb
65
+ - lib/sugarcube-repl.rb
66
+ - lib/sugarcube-repl/object.rb
65
67
  - lib/sugarcube-unholy.rb
66
68
  - lib/sugarcube-unholy/ivar.rb
67
69
  - lib/sugarcube.rb