sugarcube 1.5.8 → 1.5.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +4 -1
- data/Gemfile.lock +1 -1
- data/README.md +83 -18
- data/lib/sugarcube/version.rb +1 -1
- data/lib/sugarcube-color/symbol.rb +13 -13
- data/lib/sugarcube-factories/uiactionsheet.rb +44 -16
- data/lib/sugarcube-factories/uialertview.rb +53 -18
- data/runtests +3 -1
- data/spec/symbol_uicolor_spec.rb +748 -69
- data/spec/uiactionsheet_spec.rb +103 -2
- data/spec/uialertview_spec.rb +160 -14
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 095bfec455df3479380c6c660fc39317b97129a8
|
4
|
+
data.tar.gz: 14ec967daf20d6b0ea2090abd8098cab5c8c67d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 718f2a93953af262d767e716c69ac2f824a639c9a5835590666b023082573fb0a4df6a2f02cbfe30055db44f573b7af79734fae1b6ef5ac8b7e37d14e799cc96
|
7
|
+
data.tar.gz: 65c9bab6c3b42c9de6c2ec3b977a5a685b7787080591e2883b1d754278bd228654ca73c8056d5f51c252eab2eaafda99e56c0667ed19b9586af6b9ab2d3e2f09
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -607,28 +607,62 @@ Factories
|
|
607
607
|
###### UIAlertView
|
608
608
|
|
609
609
|
Accepts multiple buttons and handlers. In its simplest form, you can pass just
|
610
|
-
a title and block.
|
610
|
+
a title and block. An optional `:message` can either be passed in as an option,
|
611
|
+
or as the 2nd positional arg.
|
612
|
+
|
613
|
+
Options:
|
614
|
+
|
615
|
+
UIAlertView.alert(options)
|
616
|
+
UIAlertView.alert(title, options)
|
617
|
+
UIAlertView.alert(title, message, options)
|
618
|
+
0 => title => String - title of the alert. optional positional arg.
|
619
|
+
1 => message => String - message of the alert. optional positional arg.
|
620
|
+
:title => String - title of the alert.
|
621
|
+
:message => String - message of the alert.
|
622
|
+
:success => Proc - the success handler
|
623
|
+
:cancel => Proc - the cancel handler
|
624
|
+
:buttons => [] - List of buttons ([cancel, others...])
|
625
|
+
:buttons => {} - Hash of buttons ({cancel:, others: ...}) in any order of course
|
626
|
+
:style => Symbol | Fixnum - A symbol (uialertstyle) or constant (UIAlertViewStyle*)
|
627
|
+
:show => Boolean - Whether to show the action sheet (default: true)
|
611
628
|
|
612
629
|
```ruby
|
613
|
-
# simple
|
614
|
-
UIAlertView.alert
|
630
|
+
# simple title/message alert
|
631
|
+
UIAlertView.alert('This is happening, OK?', 'An optional message') do
|
632
|
+
self.it_happened!
|
633
|
+
end
|
615
634
|
|
616
635
|
# a little more complex - the cancel button should be first, and the block will
|
617
|
-
# receive a string
|
618
|
-
UIAlertView.alert(
|
619
|
-
message:
|
620
|
-
|
636
|
+
# receive a string and an index
|
637
|
+
UIAlertView.alert('This is happening, OK?',
|
638
|
+
message: 'Don't worry, it'll be fine.',
|
639
|
+
buttons: ['Nevermind', 'OK'],
|
640
|
+
) do |button, button_index|
|
641
|
+
if button == 'OK' # or: button_index == 1
|
621
642
|
self.happened!
|
622
643
|
end
|
623
|
-
|
644
|
+
end
|
624
645
|
|
625
646
|
# Full on whiz-bangery. The cancel button should be the first entry in
|
626
647
|
# `buttons:`. When you specify the success and cancel button handlers this way,
|
627
648
|
# you need not assign both.
|
628
|
-
UIAlertView.alert
|
629
|
-
|
649
|
+
UIAlertView.alert('I mean, is this cool?',
|
650
|
+
buttons: ['No!', 'Sure!', 'Hmmmm'],
|
651
|
+
message: 'No going back now',
|
630
652
|
cancel: proc { self.cancel },
|
631
|
-
success: proc { |pressed| self.proceed if pressed ==
|
653
|
+
success: proc { |pressed| self.proceed if pressed == 'Sure!' }
|
654
|
+
)
|
655
|
+
|
656
|
+
# To keep up with BubbleWrap's awesome BW::ActionSheet and BW::AlertView
|
657
|
+
# helpers, SugarCube provides a similar interface.
|
658
|
+
UIAlertView.alert('Confirm action!', 'Are you sure you want to do this?',
|
659
|
+
buttons: {
|
660
|
+
cancel: 'No!',
|
661
|
+
success: 'Sure!',
|
662
|
+
unsure: 'Hmmm',
|
663
|
+
}) do |button|
|
664
|
+
# button will be :cancel, :success or :unsure
|
665
|
+
end
|
632
666
|
```
|
633
667
|
|
634
668
|
###### UIActionSheet
|
@@ -641,23 +675,54 @@ If you use an array of buttons (which you probably *should*), the order of
|
|
641
675
|
arguments is `[:cancel, :destructive, :others, ...]`. If you *dont* want a
|
642
676
|
cancel or destructive button, pass `nil` in place.
|
643
677
|
|
678
|
+
Options:
|
679
|
+
|
680
|
+
UIActionSheet.alert(options)
|
681
|
+
UIActionSheet.alert(title, options)
|
682
|
+
0 => title => String - title of the action sheet
|
683
|
+
:title => Proc - title of the action sheet
|
684
|
+
:success => Proc - the success handler
|
685
|
+
:cancel => Proc - the cancel handler
|
686
|
+
:destructive => Proc - the destructive handler
|
687
|
+
:buttons => [] - List of buttons ([cancel, destructive, others...])
|
688
|
+
:buttons => {} - Hash of buttons ({cancel:, destructive:, others: ...}) in any order of course
|
689
|
+
:style => Symbol | Fixnum - A symbol (uiactionstyle) or constant (UIActionSheetStyle*)
|
690
|
+
:show => Boolean - Whether to show the action sheet (default: true)
|
691
|
+
:from => CGRect | UIBarButtonItem | UIToolbar | UITabBar | UIView (default: first window)
|
692
|
+
Where to display the alert. Mostly relevant on iPad.
|
693
|
+
|
644
694
|
```ruby
|
645
695
|
# simple
|
646
|
-
UIActionSheet.alert 'This is happening, OK?'
|
647
|
-
# a little more complex, with cancel and destructive buttons
|
648
|
-
UIActionSheet.alert('This is happening, OK?', buttons: ['Sure!', 'OK']
|
649
|
-
) {
|
696
|
+
UIActionSheet.alert 'This is happening, OK?' do
|
650
697
|
self.happened!
|
651
|
-
|
698
|
+
end
|
699
|
+
|
700
|
+
# a little more complex, with cancel and destructive buttons
|
701
|
+
UIActionSheet.alert('This is happening, OK?', buttons: ['Cancel', 'Kill it!', 'Uh, what?']
|
702
|
+
) do |button|
|
703
|
+
# button is 'Cancel', 'Kill it!' or 'Uh, what?'
|
704
|
+
end
|
652
705
|
|
653
|
-
|
706
|
+
# skip cancel and destructive buttons:
|
707
|
+
UIActionSheet.alert('Should I?', buttons: [nil, nil, 'OK', 'Nevermind']) [ |pressed|
|
654
708
|
self.do_it if pressed == 'OK'
|
655
|
-
|
709
|
+
]
|
656
710
|
|
657
711
|
UIActionSheet.alert 'I mean, is this cool?', buttons: ['Nah', 'With fire!', 'Sure', 'whatever'],
|
658
712
|
cancel: proc { self.cancel },
|
659
713
|
destructive: proc { self.kill_it_with_fire }
|
660
714
|
success: proc { |pressed| self.proceed if pressed == 'Sure' }
|
715
|
+
|
716
|
+
# By passing a Hash to buttons you can get this improved interface, similar to
|
717
|
+
# BubbleWrap's awesome interface.
|
718
|
+
UIActionSheet.alert('Well, how bout it?',
|
719
|
+
buttons: {
|
720
|
+
cancel: 'Cancel',
|
721
|
+
destructive: 'Kill it with fire!',
|
722
|
+
help: 'Tell me more'
|
723
|
+
}) do |button|
|
724
|
+
# button is :cancel, :destructive or :help
|
725
|
+
end
|
661
726
|
```
|
662
727
|
|
663
728
|
###### UIButton
|
data/lib/sugarcube/version.rb
CHANGED
@@ -35,21 +35,21 @@ class Symbol
|
|
35
35
|
}
|
36
36
|
|
37
37
|
@uicolors = {
|
38
|
-
black:
|
39
|
-
blue:
|
40
|
-
brown:
|
41
|
-
cyan:
|
38
|
+
black: :blackColor,
|
39
|
+
blue: :blueColor,
|
40
|
+
brown: :brownColor,
|
41
|
+
cyan: :cyanColor,
|
42
42
|
dark_gray: :darkGrayColor,
|
43
|
-
gray:
|
44
|
-
green:
|
43
|
+
gray: :grayColor,
|
44
|
+
green: :greenColor,
|
45
45
|
light_gray: :lightGrayColor,
|
46
|
-
magenta:
|
47
|
-
orange:
|
48
|
-
purple:
|
49
|
-
red:
|
50
|
-
yellow:
|
51
|
-
white:
|
52
|
-
clear:
|
46
|
+
magenta: :magentaColor,
|
47
|
+
orange: :orangeColor,
|
48
|
+
purple: :purpleColor,
|
49
|
+
red: :redColor,
|
50
|
+
yellow: :yellowColor,
|
51
|
+
white: :whiteColor,
|
52
|
+
clear: :clearColor,
|
53
53
|
|
54
54
|
light_text: :lightTextColor,
|
55
55
|
dark_text: :darkTextColor,
|
@@ -17,6 +17,11 @@ class UIActionSheet
|
|
17
17
|
# # use one handler for all buttons
|
18
18
|
# UIActionSheet.alert("title", buttons: [...]) { |button| }
|
19
19
|
def self.alert(title, options={}, &block)
|
20
|
+
if title.is_a?(Hash)
|
21
|
+
options = title
|
22
|
+
title = options[:title]
|
23
|
+
end
|
24
|
+
|
20
25
|
# create the delegate
|
21
26
|
delegate = SugarCube::ActionSheetDelegate.new
|
22
27
|
delegate.on_default = block
|
@@ -28,10 +33,10 @@ class UIActionSheet
|
|
28
33
|
args = [title] # initWithTitle:
|
29
34
|
args << delegate # delegate:
|
30
35
|
|
31
|
-
buttons = []
|
32
|
-
buttons.concat(options[:buttons]) if options[:buttons]
|
33
|
-
|
36
|
+
buttons = (options[:buttons] || []).freeze
|
34
37
|
if buttons.empty?
|
38
|
+
buttons = [] # an empty Hash becomes an Array
|
39
|
+
|
35
40
|
# cancelButtonTitle:
|
36
41
|
buttons << nil
|
37
42
|
|
@@ -45,32 +50,53 @@ class UIActionSheet
|
|
45
50
|
end
|
46
51
|
|
47
52
|
# cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:
|
48
|
-
|
53
|
+
# uses localized buttons in the actual alert
|
54
|
+
if buttons.is_a?(Hash)
|
55
|
+
if buttons.key?(:cancel)
|
56
|
+
args << (buttons[:cancel] && buttons[:cancel].localized)
|
57
|
+
else
|
58
|
+
args << nil
|
59
|
+
end
|
60
|
+
if buttons.key?(:destructive)
|
61
|
+
args << (buttons[:destructive] && buttons[:destructive].localized)
|
62
|
+
else
|
63
|
+
args << nil
|
64
|
+
end
|
65
|
+
args.concat(buttons.select { |k, m| k != :cancel && k != :destructive }.map { |k, m| m && m.localized })
|
66
|
+
else
|
67
|
+
args.concat(buttons.map { |m| m && m.localized })
|
68
|
+
end
|
49
69
|
args << nil # otherButtonTitles:..., nil
|
50
70
|
|
51
71
|
# the button titles, mapped to how UIActionSheet orders them. These are
|
52
72
|
# passed to the success handler.
|
53
73
|
buttons_mapped = {}
|
74
|
+
if buttons.is_a?(Hash)
|
75
|
+
button_titles = buttons.keys
|
76
|
+
else
|
77
|
+
button_titles = buttons
|
78
|
+
end
|
79
|
+
|
54
80
|
if args[2] && args[3] # cancel && destructive buttons
|
55
|
-
buttons_mapped[0] =
|
56
|
-
buttons_mapped[
|
81
|
+
buttons_mapped[0] = button_titles[1] # destructiveIndex == 0, button == 1
|
82
|
+
buttons_mapped[button_titles.length - 1] = button_titles[0] # cancelIndex == last, button == 0
|
57
83
|
# from first+1 to last-1
|
58
|
-
|
84
|
+
button_titles[2..-1].each_with_index do |button,index|
|
59
85
|
buttons_mapped[index + 1] = button
|
60
86
|
end
|
61
87
|
elsif args[3] # destructive button
|
62
|
-
buttons_mapped[0] =
|
88
|
+
buttons_mapped[0] = button_titles[1] # destructiveIndex == 0, button == 1
|
63
89
|
# from first+1 to last-1
|
64
90
|
buttons[2..-1].each_with_index do |button,index|
|
65
91
|
buttons_mapped[index + 1] = button
|
66
92
|
end
|
67
93
|
elsif args[2] # cancel button
|
68
|
-
buttons_mapped[buttons.length - 2] =
|
69
|
-
|
94
|
+
buttons_mapped[buttons.length - 2] = button_titles[0] # cancelIndex == last, button == 0
|
95
|
+
button_titles[2..-1].each_with_index do |button,index|
|
70
96
|
buttons_mapped[index] = button
|
71
97
|
end
|
72
98
|
else
|
73
|
-
|
99
|
+
button_titles[2..-1].each_with_index do |button,index|
|
74
100
|
buttons_mapped[index] = button
|
75
101
|
end
|
76
102
|
end
|
@@ -134,7 +160,7 @@ module SugarCube
|
|
134
160
|
attr_accessor :on_destructive
|
135
161
|
attr_accessor :on_success
|
136
162
|
|
137
|
-
def actionSheet(alert, didDismissWithButtonIndex:index)
|
163
|
+
def actionSheet(alert, didDismissWithButtonIndex: index)
|
138
164
|
handler = nil
|
139
165
|
if index == alert.destructiveButtonIndex && on_destructive
|
140
166
|
handler = on_destructive
|
@@ -148,12 +174,14 @@ module SugarCube
|
|
148
174
|
if handler
|
149
175
|
if handler.arity == 0
|
150
176
|
handler.call
|
151
|
-
elsif handler.arity == 1
|
152
|
-
button = buttons[index]
|
153
|
-
handler.call(button)
|
154
177
|
else
|
155
178
|
button = buttons[index]
|
156
|
-
|
179
|
+
|
180
|
+
if handler.arity == 1
|
181
|
+
handler.call(button)
|
182
|
+
else
|
183
|
+
handler.call(button, index)
|
184
|
+
end
|
157
185
|
end
|
158
186
|
end
|
159
187
|
|
@@ -1,22 +1,38 @@
|
|
1
1
|
class UIAlertView
|
2
2
|
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
3
|
+
# @example
|
4
|
+
# UIAlertView.alert("title",
|
5
|
+
# message: "help!",
|
6
|
+
# # The first button is considered the 'cancel' button, for the purposes of
|
7
|
+
# # whether the cancel or success handler gets called
|
8
|
+
# buttons: %w"Cancel OK No-way",
|
9
|
+
# cancel: proc{ puts "nevermind" },
|
10
|
+
# success: proc{ |pressed| puts "pressed: #{pressed}" },
|
11
|
+
# )
|
12
|
+
# # you can explicitly set the cancel button by using a Hash for the :buttons option
|
13
|
+
# UIAlertView.alert("title",
|
14
|
+
# message: "help!",
|
15
|
+
# # The first button is considered the 'cancel' button, for the purposes of
|
16
|
+
# # whether the cancel or success handler gets called
|
17
|
+
# buttons: { cancel: 'Cancel', ok: 'OK', no_way: 'No-way' },
|
18
|
+
# cancel: proc{ puts "nevermind" },
|
19
|
+
# success: proc{ |pressed| puts "pressed: #{pressed.inspect}" },
|
20
|
+
# ) # pressed will be :ok or :no_way
|
13
21
|
def self.alert(title, options={}, more_options={}, &block)
|
14
|
-
if
|
15
|
-
|
22
|
+
if title.is_a?(Hash)
|
23
|
+
options = title
|
24
|
+
title = options[:title]
|
25
|
+
message = options[:message]
|
26
|
+
elsif options.is_a? String
|
27
|
+
message = options
|
16
28
|
options = more_options
|
29
|
+
else
|
30
|
+
message = options[:message]
|
17
31
|
end
|
18
32
|
|
19
|
-
#
|
33
|
+
# The delegate gets retained here because UIAlertView#delegate is a weak
|
34
|
+
# reference. It's released in the delegate method
|
35
|
+
# `#didDismissWithButtonIndex(index)`
|
20
36
|
delegate = SugarCube::AlertViewDelegate.new
|
21
37
|
delegate.on_success = options[:success]
|
22
38
|
delegate.on_cancel = options[:cancel]
|
@@ -24,11 +40,13 @@ class UIAlertView
|
|
24
40
|
delegate.send(:retain)
|
25
41
|
|
26
42
|
args = [title] # initWithTitle:
|
27
|
-
args <<
|
43
|
+
args << message # message:
|
28
44
|
args << delegate # delegate:
|
29
45
|
|
30
|
-
buttons = options[:buttons] || []
|
46
|
+
buttons = (options[:buttons] || []).freeze
|
31
47
|
if buttons.empty?
|
48
|
+
buttons = [] # an empty Hash becomes an Array
|
49
|
+
|
32
50
|
# cancelButtonTitle: is first, so check for cancel
|
33
51
|
if options[:cancel]
|
34
52
|
buttons << "Cancel"
|
@@ -41,19 +59,35 @@ class UIAlertView
|
|
41
59
|
elsif options[:success]
|
42
60
|
buttons << "OK"
|
43
61
|
end
|
62
|
+
elsif buttons.is_a?(Hash)
|
63
|
+
|
44
64
|
elsif buttons.length == 1 && options[:cancel]
|
45
65
|
raise "If you only have one button, use a :success handler, not :cancel (and definitely not BOTH)"
|
46
66
|
end
|
47
67
|
|
48
68
|
# the button titles. These are passed to the success handler.
|
49
|
-
|
69
|
+
if buttons.is_a?(Hash)
|
70
|
+
button_titles = buttons.keys
|
71
|
+
else
|
72
|
+
button_titles = buttons
|
73
|
+
end
|
74
|
+
delegate.buttons = button_titles
|
50
75
|
|
51
76
|
# uses localized buttons in the actual alert
|
52
|
-
|
77
|
+
if buttons.is_a?(Hash)
|
78
|
+
if buttons.key?(:cancel)
|
79
|
+
args << (buttons[:cancel] && buttons[:cancel].localized)
|
80
|
+
else
|
81
|
+
args << nil
|
82
|
+
end
|
83
|
+
args.concat(buttons.select { |k, m| k != :cancel }.map { |k, m| m && m.localized })
|
84
|
+
else
|
85
|
+
args.concat(buttons.map { |m| m && m.localized })
|
86
|
+
end
|
53
87
|
args << nil # otherButtonTitles:..., nil
|
54
88
|
|
55
89
|
alert = self.alloc
|
56
|
-
alert.send(
|
90
|
+
alert.send("initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:", *args)
|
57
91
|
if options.key?(:style)
|
58
92
|
style = options[:style]
|
59
93
|
style = style.uialertstyle if style.respond_to?(:uialertstyle)
|
@@ -115,6 +149,7 @@ module SugarCube
|
|
115
149
|
# but only send the ones they asked for
|
116
150
|
args = args[0...handler.arity]
|
117
151
|
end
|
152
|
+
|
118
153
|
handler.call(*args)
|
119
154
|
end
|
120
155
|
|
data/runtests
CHANGED