sugarcube 0.4.2 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -140,6 +140,30 @@ image = "my_image".uiimage
140
140
  image.uicolor # => UIColor.colorWithPatternImage(image)
141
141
  ```
142
142
 
143
+ UIAlertView
144
+ --------
145
+
146
+ Accepts multiple buttons and success and cancel handlers. In its simplest
147
+ form, you can pass just a title and block.
148
+
149
+ ```ruby
150
+ # simple
151
+ UIAlertView.alert "This is happening, OK?" { self.happened! }
152
+ # a little more complex
153
+ UIAlertView.alert("This is happening, OK?", buttons: ["Nevermind", "OK"],
154
+ message: "don't worry, it'll be fine.") {
155
+ self.happened!
156
+ }
157
+
158
+ # Full on whiz bangery. Note the success block takes the pressed button, but as
159
+ # a string instead of an index. The cancel button should be the first entry in
160
+ # `buttons:`
161
+ UIAlertView.alert "I mean, is this cool?", buttons: %w[No! Sure! Hmmmm]
162
+ message: "No going back now",
163
+ cancel: { self.cancel },
164
+ success: { |pressed| self.proceed if pressed == "Sure!" }
165
+ ```
166
+
143
167
  UIView
144
168
  --------
145
169
 
@@ -0,0 +1,70 @@
1
+ class UIAlertView
2
+
3
+ # UIAlertView.alert("title",
4
+ # message: "help!",
5
+ # buttons: %w"OK Cancel Go Away",
6
+ # cancel: proc{ puts "nevermind" },
7
+ # success: proc{ |pressed| puts "pressed: #{pressed}" },
8
+ # )
9
+ def self.alert(title, options={}, &block)
10
+ puts options
11
+ puts block
12
+ # create the delegate
13
+ delegate = SugarCube::AlertViewDelegate.new
14
+ delegate.on_success = block || options[:success]
15
+ delegate.on_cancel = options[:cancel]
16
+ delegate.send(:retain)
17
+
18
+ args = [title] # initWithTitle:
19
+ args << options[:message] # message:
20
+ args << delegate # delegate:
21
+
22
+ buttons = options[:buttons] || []
23
+ if buttons.empty?
24
+ # cancelButtonTitle: is first, so check for cancel
25
+ buttons << "Cancel" if options[:cancel]
26
+ # otherButtonTitles:
27
+ buttons << "OK" if options[:success] or buttons.empty?
28
+ elsif buttons.length == 1 and options[:cancel]
29
+ raise "If you only have one button, use a :success handler, not :cancel (and definitely not BOTH)"
30
+ end
31
+
32
+ # the button titles. These are passed to the success handler.
33
+ delegate.buttons = buttons
34
+
35
+ # uses localized buttons in the actual alert
36
+ args.concat(buttons.map{ |s| s.localized })
37
+ args << nil # otherButtonTitles:..., nil
38
+
39
+ alert = self.alloc
40
+ alert.send(:"initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:", *args)
41
+ alert.show
42
+ alert
43
+ end
44
+
45
+ end
46
+
47
+
48
+ module SugarCube
49
+ class AlertViewDelegate
50
+ attr_accessor :buttons
51
+ attr_accessor :on_cancel
52
+ attr_accessor :on_success
53
+
54
+ def alertView(alert, didDismissWithButtonIndex:index)
55
+ if on_cancel
56
+ on_cancel.call
57
+ elsif on_success
58
+ if on_success.arity == 0
59
+ on_success.call
60
+ else
61
+ button = buttons[index]
62
+ on_success.call(button)
63
+ end
64
+ end
65
+
66
+ self.send(:autorelease)
67
+ end
68
+
69
+ end
70
+ end
@@ -1,3 +1,3 @@
1
1
  module SugarCube
2
- Version = '0.4.2'
2
+ Version = '0.5'
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.4.2
4
+ version: '0.5'
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-07-25 00:00:00.000000000 Z
13
+ date: 2012-07-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
17
- requirement: &70184501303980 !ruby/object:Gem::Requirement
17
+ requirement: &70125432971500 !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: *70184501303980
25
+ version_requirements: *70125432971500
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &70184501319460 !ruby/object:Gem::Requirement
28
+ requirement: &70125432986920 !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: *70184501319460
36
+ version_requirements: *70125432986920
37
37
  description: ! 'CocoaTouch/iOS is a *verbose* framework. These extensions hope to
38
38
  make
39
39
 
@@ -76,6 +76,7 @@ files:
76
76
  - lib/sugarcube/numeric.rb
77
77
  - lib/sugarcube/symbol.rb
78
78
  - lib/sugarcube/symbol/symbol_uicolor.rb
79
+ - lib/sugarcube/uialertview.rb
79
80
  - lib/sugarcube/uibutton.rb
80
81
  - lib/sugarcube/uicolor.rb
81
82
  - lib/sugarcube/uicontrol.rb