rubymotion/BubbleWrap — pull request #213 by ryansobol (2013-04-21 06:37:39+00:00)
Title: Wrap UIAlertView

Inspired by @rbeene and #201, I present `BW::UIAlertView` - a subclass of `UIAlertView` with an idiomatic Ruby syntax. Each method accepts an optional `Hash` of options and a block. When a button is tapped, the block is executed.

```ruby
alert = BW::UIAlertView.default(:title => "Are you ready?") do |index|
# ...
end

alert.show
```

![iOS Simulator Screen shot Apr 20 2013 11 07 51 PM](https://f.cloud.github.com/assets/5688/406043/2cb580bc-aa4d-11e2-8015-329f1c2c0258.png)

It's easy to create alerts with alternate styles too.

```ruby
BW::UIAlertView.plain_text_input(:title => "Are you ready?") do |index|
# ...
end

BW::UIAlertView.secure_text_input(:title => "Are you ready?") do |index|
# ...
end

BW::UIAlertView.login_and_password_input(:title => "Are you ready?") do |index|
# ...
end
```

![iOS Simulator Screen shot Apr 20 2013 11 10 50 PM](https://f.cloud.github.com/assets/5688/406044/3f56881a-aa4d-11e2-9717-4f53ea080464.png)

![iOS Simulator Screen shot Apr 20 2013 11 10 16 PM](https://f.cloud.github.com/assets/5688/406045/42959908-aa4d-11e2-8460-c821a07686c6.png)

![iOS Simulator Screen shot Apr 20 2013 11 11 39 PM](https://f.cloud.github.com/assets/5688/406046/456d3366-aa4d-11e2-966e-6c5b772e16f6.png)

Each alert has sane button titles. Yet the titles are fully customizable.

```ruby
BW::UIAlertView.default(:title => "BubbleWrap is", :buttons => "Awesome")

options = {
:title => "Dinner this Friday?",
:message => "My treat!",
:buttons => ["No", "Maybe", "Yes"]
}
BW::UIAlertView.default(options)
```

![iOS Simulator Screen shot Apr 20 2013 11 13 29 PM](https://f.cloud.github.com/assets/5688/406047/6035a674-aa4d-11e2-97df-f4c8ba08b5b4.png)

![iOS Simulator Screen shot Apr 20 2013 11 14 17 PM](https://f.cloud.github.com/assets/5688/406048/62adaadc-aa4d-11e2-810e-0e9716e42ab8.png)

Re-styling a `BW::UIAlertView` instance is a snap.

```ruby
alert = BW::UIAlertView.default
alert.style = :plain_text_input
alert.style = :secure_text_input
alert.style = :login_and_password_input
alert.style = :default
```

`BW::UIAlertView` instances are their own delegate. The complete `UIAlertViewDelegte` protocol is available via callbacks.

```ruby
alert = BW::UIAlertView.default

alert.will_present do
# ...
end

alert.did_present do
# ...
end

alert.on_click do |index|
# ...
end

alert.will_dismiss do |index|
# ...
end

alert.did_dismiss do |index|
# ...
end

alert.on_system_cancel do
# ...
end

alert.enable_first_other_button? do
# ...
end
```

Alternatively, `BW::UIAlertView` provides a flexible, builder-style syntax.

```ruby
options = {
:title => "title",
:message => "message",
:buttons => "button title", # NOTE: either a String or Array
:style => :plain_text_input,
:will_present => -> { :will_present },
:did_present => -> { :did_present },
:on_click => -> { :on_click }, # NOTE: takes priority over a block
:will_dismiss => -> { :will_dismiss },
:did_dismiss => -> { :did_dismiss },
:on_system_cancel => -> { :on_system_cancel },
:enable_first_other_button? => -> { :enable_first_other_button? }
}
BW::UIAlertView.new(options)
```

---

Creating this wrapper was a blast! Thanks again, @rbeene, for the inspiration.

I'm happy to discuss both the API and the implementation. Although it may seem complex, there's only a few moving parts under the hood. I recommend trying the sample app to see it in action.