fidgit 0.2.4 → 0.2.5

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.
Files changed (85) hide show
  1. data/.gitignore +7 -7
  2. data/.rspec +2 -2
  3. data/CHANGELOG.md +30 -30
  4. data/Gemfile +3 -3
  5. data/LICENSE.txt +19 -19
  6. data/README.textile +139 -139
  7. data/Rakefile +37 -37
  8. data/config/default_schema.yml +216 -216
  9. data/examples/_all_examples.rb +9 -9
  10. data/examples/align_example.rb +55 -55
  11. data/examples/button_and_toggle_button_example.rb +37 -37
  12. data/examples/color_picker_example.rb +16 -16
  13. data/examples/color_well_example.rb +24 -24
  14. data/examples/combo_box_example.rb +23 -23
  15. data/examples/file_dialog_example.rb +41 -41
  16. data/examples/grid_packer_example.rb +28 -28
  17. data/examples/helpers/example_window.rb +16 -16
  18. data/examples/label_example.rb +22 -22
  19. data/examples/list_example.rb +22 -22
  20. data/examples/menu_pane_example.rb +26 -26
  21. data/examples/message_dialog_example.rb +64 -64
  22. data/examples/radio_button_example.rb +36 -36
  23. data/examples/readme_example.rb +31 -31
  24. data/examples/scroll_window_example.rb +48 -48
  25. data/examples/slider_example.rb +33 -33
  26. data/examples/splash_example.rb +41 -41
  27. data/examples/text_area_example.rb +32 -32
  28. data/fidgit.gemspec +35 -35
  29. data/lib/fidgit.rb +50 -50
  30. data/lib/fidgit/chingu_ext/window.rb +5 -5
  31. data/lib/fidgit/cursor.rb +37 -37
  32. data/lib/fidgit/elements/button.rb +112 -112
  33. data/lib/fidgit/elements/color_picker.rb +62 -62
  34. data/lib/fidgit/elements/color_well.rb +38 -38
  35. data/lib/fidgit/elements/combo_box.rb +113 -113
  36. data/lib/fidgit/elements/composite.rb +16 -16
  37. data/lib/fidgit/elements/container.rb +208 -208
  38. data/lib/fidgit/elements/element.rb +297 -297
  39. data/lib/fidgit/elements/file_browser.rb +151 -151
  40. data/lib/fidgit/elements/grid.rb +226 -226
  41. data/lib/fidgit/elements/group.rb +64 -64
  42. data/lib/fidgit/elements/horizontal.rb +11 -11
  43. data/lib/fidgit/elements/image_frame.rb +64 -64
  44. data/lib/fidgit/elements/label.rb +84 -84
  45. data/lib/fidgit/elements/list.rb +46 -46
  46. data/lib/fidgit/elements/main_packer.rb +24 -24
  47. data/lib/fidgit/elements/menu_pane.rb +160 -160
  48. data/lib/fidgit/elements/packer.rb +41 -41
  49. data/lib/fidgit/elements/radio_button.rb +85 -85
  50. data/lib/fidgit/elements/scroll_area.rb +67 -67
  51. data/lib/fidgit/elements/scroll_bar.rb +127 -127
  52. data/lib/fidgit/elements/scroll_window.rb +82 -82
  53. data/lib/fidgit/elements/slider.rb +124 -124
  54. data/lib/fidgit/elements/text_area.rb +493 -493
  55. data/lib/fidgit/elements/text_line.rb +91 -91
  56. data/lib/fidgit/elements/toggle_button.rb +66 -66
  57. data/lib/fidgit/elements/tool_tip.rb +34 -34
  58. data/lib/fidgit/elements/vertical.rb +11 -11
  59. data/lib/fidgit/event.rb +158 -158
  60. data/lib/fidgit/gosu_ext/color.rb +135 -135
  61. data/lib/fidgit/gosu_ext/gosu_module.rb +24 -24
  62. data/lib/fidgit/history.rb +90 -90
  63. data/lib/fidgit/redirector.rb +82 -82
  64. data/lib/fidgit/schema.rb +123 -123
  65. data/lib/fidgit/selection.rb +105 -105
  66. data/lib/fidgit/standard_ext/hash.rb +20 -20
  67. data/lib/fidgit/states/dialog_state.rb +51 -51
  68. data/lib/fidgit/states/file_dialog.rb +24 -24
  69. data/lib/fidgit/states/gui_state.rb +329 -329
  70. data/lib/fidgit/states/message_dialog.rb +60 -60
  71. data/lib/fidgit/version.rb +4 -4
  72. data/lib/fidgit/window.rb +19 -19
  73. data/spec/fidgit/elements/helpers/helper.rb +2 -2
  74. data/spec/fidgit/elements/helpers/tex_play_helper.rb +8 -8
  75. data/spec/fidgit/elements/image_frame_spec.rb +68 -68
  76. data/spec/fidgit/elements/label_spec.rb +36 -36
  77. data/spec/fidgit/event_spec.rb +209 -209
  78. data/spec/fidgit/gosu_ext/color_spec.rb +129 -129
  79. data/spec/fidgit/gosu_ext/helpers/helper.rb +2 -2
  80. data/spec/fidgit/helpers/helper.rb +3 -3
  81. data/spec/fidgit/history_spec.rb +153 -153
  82. data/spec/fidgit/redirector_spec.rb +77 -77
  83. data/spec/fidgit/schema_spec.rb +66 -66
  84. data/spec/fidgit/schema_test.yml +32 -32
  85. metadata +67 -22
@@ -1,61 +1,61 @@
1
-
2
- module Fidgit
3
- # A simple dialog that manages a message with a set of buttons beneath it.
4
- class MessageDialog < DialogState
5
- VALID_TYPES = [:ok, :ok_cancel, :yes_no, :yes_no_cancel, :quit_cancel, :quit_save_cancel]
6
-
7
- attr_reader :type
8
-
9
- # @param [String] message
10
- #
11
- # @option options [Symbol] :type (:ok) One from :ok, :ok_cancel, :yes_no, :yes_no_cancel, :quit_cancel or :quit_save_cancel
12
- # @option options [String] :ok_text ("OK")
13
- # @option options [String] :yes_text ("Yes")
14
- # @option options [String] :no_text ("No")
15
- # @option options [String] :cancel_text ("Cancel")
16
- # @option options [String] :save_text ("Save")
17
- # @option options [String] :quit_text ("Quit")
18
- # @option options [Boolean] :show (true) Whether to show the message immediately (otherwise need to use #show later).
19
- #
20
- # @yield when the dialog is closed.
21
- # @yieldparam [Symbol] result :ok, :yes, :no, :quit, :save or :cancel, depending on the button pressed.
22
- def initialize(message, options = {}, &block)
23
- options = {
24
- type: :ok,
25
- ok_text: "OK",
26
- yes_text: "Yes",
27
- no_text: "No",
28
- quit_text: "Quit",
29
- save_text: "Save",
30
- cancel_text: "Cancel",
31
- show: true,
32
- background_color: DEFAULT_BACKGROUND_COLOR,
33
- border_color: DEFAULT_BORDER_COLOR,
34
- width: $window.width / 2
35
- }.merge! options
36
-
37
- @type = options[:type]
38
- raise ArgumentError, ":type must be one of #{VALID_TYPES}, not #{@type}" unless VALID_TYPES.include? @type
39
-
40
- super(options)
41
-
42
- # Dialog is forced to the centre.
43
- options[:align_h] = options[:align_v] = :center
44
-
45
- vertical options do
46
- text_area(text: message, enabled: false, width: options[:width] - padding_left - padding_right)
47
-
48
- horizontal align_h: :center do
49
- @type.to_s.split('_').each do |type|
50
- button(options[:"#{type}_text"]) do
51
- hide
52
- block.call type.to_sym if block
53
- end
54
- end
55
- end
56
- end
57
-
58
- show if options[:show]
59
- end
60
- end
1
+
2
+ module Fidgit
3
+ # A simple dialog that manages a message with a set of buttons beneath it.
4
+ class MessageDialog < DialogState
5
+ VALID_TYPES = [:ok, :ok_cancel, :yes_no, :yes_no_cancel, :quit_cancel, :quit_save_cancel]
6
+
7
+ attr_reader :type
8
+
9
+ # @param [String] message
10
+ #
11
+ # @option options [Symbol] :type (:ok) One from :ok, :ok_cancel, :yes_no, :yes_no_cancel, :quit_cancel or :quit_save_cancel
12
+ # @option options [String] :ok_text ("OK")
13
+ # @option options [String] :yes_text ("Yes")
14
+ # @option options [String] :no_text ("No")
15
+ # @option options [String] :cancel_text ("Cancel")
16
+ # @option options [String] :save_text ("Save")
17
+ # @option options [String] :quit_text ("Quit")
18
+ # @option options [Boolean] :show (true) Whether to show the message immediately (otherwise need to use #show later).
19
+ #
20
+ # @yield when the dialog is closed.
21
+ # @yieldparam [Symbol] result :ok, :yes, :no, :quit, :save or :cancel, depending on the button pressed.
22
+ def initialize(message, options = {}, &block)
23
+ options = {
24
+ type: :ok,
25
+ ok_text: "OK",
26
+ yes_text: "Yes",
27
+ no_text: "No",
28
+ quit_text: "Quit",
29
+ save_text: "Save",
30
+ cancel_text: "Cancel",
31
+ show: true,
32
+ background_color: DEFAULT_BACKGROUND_COLOR,
33
+ border_color: DEFAULT_BORDER_COLOR,
34
+ width: $window.width / 2
35
+ }.merge! options
36
+
37
+ @type = options[:type]
38
+ raise ArgumentError, ":type must be one of #{VALID_TYPES}, not #{@type}" unless VALID_TYPES.include? @type
39
+
40
+ super(options)
41
+
42
+ # Dialog is forced to the centre.
43
+ options[:align_h] = options[:align_v] = :center
44
+
45
+ vertical options do
46
+ text_area(text: message, enabled: false, width: options[:width] - padding_left - padding_right)
47
+
48
+ horizontal align_h: :center do
49
+ @type.to_s.split('_').each do |type|
50
+ button(options[:"#{type}_text"]) do
51
+ hide
52
+ block.call type.to_sym if block
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ show if options[:show]
59
+ end
60
+ end
61
61
  end
@@ -1,5 +1,5 @@
1
- # encoding: UTF-8
2
-
3
- module Fidgit
4
- VERSION = '0.2.4'
1
+ # encoding: UTF-8
2
+
3
+ module Fidgit
4
+ VERSION = '0.2.5'
5
5
  end
@@ -1,19 +1,19 @@
1
- # encoding: utf-8
2
-
3
- module Fidgit
4
- module Window
5
-
6
- def self.included(base)
7
- base.send :include, Methods
8
- end
9
-
10
- module Methods
11
-
12
- def close
13
- super
14
- GuiState.clear
15
- end
16
- end
17
-
18
- end
19
- end
1
+ # encoding: utf-8
2
+
3
+ module Fidgit
4
+ module Window
5
+
6
+ def self.included(base)
7
+ base.send :include, Methods
8
+ end
9
+
10
+ module Methods
11
+
12
+ def close
13
+ super
14
+ GuiState.clear
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
- require "rspec"
2
-
1
+ require "rspec"
2
+
3
3
  require_relative File.join(File.dirname(__FILE__), "..", "..", "..", "..", "lib", "fidgit")
@@ -1,9 +1,9 @@
1
- require 'texplay'
2
-
3
- module Gosu
4
- class Image
5
- def self.create(width, height, options = {})
6
- TexPlay.create_image($window, width, height, options)
7
- end
8
- end
1
+ require 'texplay'
2
+
3
+ module Gosu
4
+ class Image
5
+ def self.create(width, height, options = {})
6
+ TexPlay.create_image($window, width, height, options)
7
+ end
8
+ end
9
9
  end
@@ -1,69 +1,69 @@
1
- require_relative 'helpers/helper'
2
- require_relative 'helpers/tex_play_helper'
3
-
4
- require 'fidgit'
5
-
6
- def check_thumbnail_is_square
7
- {square: [10, 10], tall: [5, 12], wide: [6, 5]}.each_pair do |type, dimensions|
8
- context "with a #{type} image" do
9
- it "should be square and just large enough to contain the image" do
10
- subject = described_class.new(Gosu::Image.create(*dimensions), thumbnail: true)
11
- subject.width.should equal dimensions.max
12
- subject.height.should equal dimensions.max
13
- end
14
- end
15
- end
16
- end
17
-
18
- def check_image_dimensions
19
- {square: [10, 10], tall: [5, 12], wide: [6, 5]}.each_pair do |type, dimensions|
20
- context "with a #{type} image" do
21
- it "should be just large enough to contain the image" do
22
- subject = described_class.new(Gosu::Image.create(*dimensions))
23
- subject.width.should equal dimensions.first
24
- subject.height.should equal dimensions.last
25
- end
26
- end
27
- end
28
- end
29
-
30
- module Fidgit
31
- describe ImageFrame do
32
- before :all do
33
- $window = Chingu::Window.new(100, 100, false) unless $window
34
- end
35
-
36
- before :each do
37
- @image = Gosu::Image.create(10, 10)
38
- end
39
-
40
- subject { described_class.new(@image) }
41
-
42
- describe '#image' do
43
- it "should have the image set" do
44
- subject.image.should be @image
45
- end
46
- end
47
-
48
- describe '#width and #height' do
49
- check_image_dimensions
50
- end
51
-
52
- describe "Thumbnails" do
53
- subject { described_class.new(@image, thumbnail: true) }
54
-
55
- describe '#image=' do
56
- it "should update the height and width" do
57
- image = Gosu::Image.create(8, 2)
58
- subject.image = image
59
- subject.height.should equal 8
60
- subject.width.should equal 8
61
- end
62
- end
63
-
64
- describe '#width and #height' do
65
- check_thumbnail_is_square
66
- end
67
- end
68
- end
1
+ require_relative 'helpers/helper'
2
+ require_relative 'helpers/tex_play_helper'
3
+
4
+ require 'fidgit'
5
+
6
+ def check_thumbnail_is_square
7
+ {square: [10, 10], tall: [5, 12], wide: [6, 5]}.each_pair do |type, dimensions|
8
+ context "with a #{type} image" do
9
+ it "should be square and just large enough to contain the image" do
10
+ subject = described_class.new(Gosu::Image.create(*dimensions), thumbnail: true)
11
+ subject.width.should equal dimensions.max
12
+ subject.height.should equal dimensions.max
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ def check_image_dimensions
19
+ {square: [10, 10], tall: [5, 12], wide: [6, 5]}.each_pair do |type, dimensions|
20
+ context "with a #{type} image" do
21
+ it "should be just large enough to contain the image" do
22
+ subject = described_class.new(Gosu::Image.create(*dimensions))
23
+ subject.width.should equal dimensions.first
24
+ subject.height.should equal dimensions.last
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ module Fidgit
31
+ describe ImageFrame do
32
+ before :all do
33
+ $window = Chingu::Window.new(100, 100, false) unless $window
34
+ end
35
+
36
+ before :each do
37
+ @image = Gosu::Image.create(10, 10)
38
+ end
39
+
40
+ subject { described_class.new(@image) }
41
+
42
+ describe '#image' do
43
+ it "should have the image set" do
44
+ subject.image.should be @image
45
+ end
46
+ end
47
+
48
+ describe '#width and #height' do
49
+ check_image_dimensions
50
+ end
51
+
52
+ describe "Thumbnails" do
53
+ subject { described_class.new(@image, thumbnail: true) }
54
+
55
+ describe '#image=' do
56
+ it "should update the height and width" do
57
+ image = Gosu::Image.create(8, 2)
58
+ subject.image = image
59
+ subject.height.should equal 8
60
+ subject.width.should equal 8
61
+ end
62
+ end
63
+
64
+ describe '#width and #height' do
65
+ check_thumbnail_is_square
66
+ end
67
+ end
68
+ end
69
69
  end
@@ -1,37 +1,37 @@
1
- require_relative "helpers/helper"
2
-
3
- include Fidgit
4
-
5
- describe Label do
6
- before :all do
7
- $window = Chingu::Window.new(100, 100, false) unless $window
8
- end
9
-
10
- context "with default parameters" do
11
- subject { Label.new( "Hello world!") }
12
-
13
- it "should have text value set" do
14
- subject.text.should eq "Hello world!"
15
- end
16
-
17
- it "should have white text" do
18
- subject.color.should eq Gosu::Color.rgb(255, 255, 255)
19
- end
20
-
21
- it "should have a transparent background" do
22
- subject.background_color.should be_transparent
23
- end
24
-
25
- it "should have a transparent border" do
26
- subject.border_color.should be_transparent
27
- end
28
-
29
- it "should be enabled" do
30
- subject.should be_enabled
31
- end
32
-
33
- it "should not have an image" do
34
- subject.icon.should be_nil
35
- end
36
- end
1
+ require_relative "helpers/helper"
2
+
3
+ include Fidgit
4
+
5
+ describe Label do
6
+ before :all do
7
+ $window = Chingu::Window.new(100, 100, false) unless $window
8
+ end
9
+
10
+ context "with default parameters" do
11
+ subject { Label.new( "Hello world!") }
12
+
13
+ it "should have text value set" do
14
+ subject.text.should eq "Hello world!"
15
+ end
16
+
17
+ it "should have white text" do
18
+ subject.color.should eq Gosu::Color.rgb(255, 255, 255)
19
+ end
20
+
21
+ it "should have a transparent background" do
22
+ subject.background_color.should be_transparent
23
+ end
24
+
25
+ it "should have a transparent border" do
26
+ subject.border_color.should be_transparent
27
+ end
28
+
29
+ it "should be enabled" do
30
+ subject.should be_enabled
31
+ end
32
+
33
+ it "should not have an image" do
34
+ subject.icon.should be_nil
35
+ end
36
+ end
37
37
  end
@@ -1,210 +1,210 @@
1
- require_relative 'helpers/helper'
2
-
3
- require 'event'
4
-
5
- module Fidgit
6
- describe Event do
7
- before :each do
8
- class Test
9
- include Event
10
- end
11
- end
12
-
13
- after :each do
14
- Fidgit.send :remove_const, :Test
15
- end
16
-
17
- subject { Test }
18
-
19
- describe "events" do
20
- it "should initially be empty" do
21
- subject.events.should be_empty
22
- end
23
- end
24
-
25
- describe "event" do
26
- it "should add the event to the list of events handled" do
27
- subject.event :frog
28
- subject.events.should include :frog
29
- end
30
-
31
- it "should inherit parent's events and be able to add more" do
32
- Test.event :frog
33
- class Test2 < Test; end
34
- Test2.event :fish
35
- Test2.events.should include :frog
36
- Test2.events.should include :fish
37
- end
38
- end
39
-
40
- context "When included into a class that is instanced" do
41
- subject { Test.event :frog; Test.new }
42
-
43
- describe "#subscribe" do
44
- it "should add a handler as a block" do
45
- subject.subscribe(:frog) { puts "hello" }
46
- end
47
-
48
- it "should fail if the event name isn't handled by this object" do
49
- ->{ subject.subscribe(:unhandled_event) {} }.should raise_error ArgumentError
50
- end
51
-
52
- it "should add a handler as a method" do
53
- subject.stub! :handler
54
- subject.subscribe(:frog, subject.method(:handler))
55
- end
56
-
57
- it "should fail if neither a method or a block is passed" do
58
- ->{ subject.subscribe(:frog) }.should raise_error ArgumentError
59
- end
60
-
61
- it "should fail if both a method and a block is passed" do
62
- subject.stub! :handler
63
- ->{ subject.subscribe(:frog, subject.method(:handler)) { } }.should raise_error ArgumentError
64
- end
65
-
66
- it "should return a Subscription" do
67
- handler = proc {}
68
- result = subject.subscribe(:frog, &handler)
69
- result.should be_a Fidgit::Event::Subscription
70
- result.handler.should be handler
71
- result.publisher.should be subject
72
- result.event.should equal :frog
73
- end
74
-
75
- it "should return a Subscription that can use Subscription#unsubscribe" do
76
- handler_ran = false
77
- subscription = subject.subscribe(:frog) { handler_ran = true }
78
- subscription.unsubscribe
79
- subject.publish :frog
80
- handler_ran.should be_false
81
- end
82
- end
83
-
84
- describe "#unsubscribe" do
85
- it "should accept a Subscription" do
86
- handler_ran = false
87
- subscription = subject.subscribe(:frog) { handler_ran = true }
88
- subject.unsubscribe subscription
89
- subject.publish :frog
90
- handler_ran.should be_false
91
- end
92
-
93
- it "should accept a handler" do
94
- handler_ran = false
95
- handler = proc { handler_ran = true }
96
- subject.subscribe(:frog, &handler)
97
- subject.unsubscribe handler
98
- subject.publish :frog
99
- handler_ran.should be_false
100
- end
101
-
102
- it "should accept an event and handler" do
103
- handler_ran = false
104
- handler = proc { handler_ran = true }
105
- subject.subscribe(:frog, &handler)
106
- subject.unsubscribe :frog, handler
107
- subject.publish :frog
108
- handler_ran.should be_false
109
- end
110
-
111
- it "should require 1..2 arguments" do
112
- ->{ subject.unsubscribe }.should raise_error ArgumentError
113
- ->{ subject.unsubscribe 1, 2, 3 }.should raise_error ArgumentError
114
- end
115
-
116
- it "should fail with bad types" do
117
- ->{ subject.unsubscribe 1 }.should raise_error TypeError
118
- ->{ subject.unsubscribe 1, ->{} }.should raise_error TypeError
119
- ->{ subject.unsubscribe :event, 2 }.should raise_error TypeError
120
- end
121
-
122
- it "should fail if passed a Subscription to another object" do
123
- subscription = Fidgit::Event::Subscription.new(Object.new.extend(Fidgit::Event), :event, proc {})
124
- ->{ subject.unsubscribe subscription }.should raise_error ArgumentError
125
- end
126
- end
127
-
128
- describe "#publish" do
129
- it "should return nil if there are no handlers" do
130
- subject.publish(:frog).should be_nil
131
- end
132
-
133
- it "should return nil if there are no handlers that handle the event" do
134
- subject.should_receive(:frog).with(subject)
135
- subject.should_receive(:handler).with(subject)
136
- subject.subscribe(:frog, subject.method(:handler))
137
- subject.publish(:frog).should be_nil
138
- end
139
-
140
- it "should return :handled if a manual handler handled the event and not call other handlers" do
141
- subject.should_not_receive(:handler1)
142
- subject.should_receive(:handler2).with(subject).and_return(:handled)
143
- subject.subscribe(:frog, subject.method(:handler1))
144
- subject.subscribe(:frog, subject.method(:handler2))
145
- subject.publish(:frog).should == :handled
146
- end
147
-
148
- it "should return :handled if an automatic handler handled the event and not call other handlers" do
149
- subject.should_receive(:frog).with(subject).and_return(:handled)
150
- subject.should_not_receive(:handler2)
151
- subject.subscribe(:frog, subject.method(:handler2))
152
- subject.publish(:frog).should == :handled
153
- end
154
-
155
- it "should pass the object as the first parameter" do
156
- subject.should_receive(:handler).with(subject)
157
- subject.subscribe(:frog, subject.method(:handler))
158
- subject.publish :frog
159
- end
160
-
161
- it "should call all the handlers, once each" do
162
- subject.should_receive(:handler1).with(subject)
163
- subject.should_receive(:handler2).with(subject)
164
- subject.subscribe(:frog, subject.method(:handler1))
165
- subject.subscribe(:frog, subject.method(:handler2))
166
- subject.publish(:frog).should be_nil
167
- end
168
-
169
- it "should pass parameters passed to it" do
170
- subject.should_receive(:handler).with(subject, 1, 2)
171
- subject.subscribe(:frog, subject.method(:handler))
172
- subject.publish(:frog, 1, 2).should be_nil
173
- end
174
-
175
- it "should do nothing if the subject is disabled" do
176
- subject.should_receive(:enabled?).and_return(false)
177
- subject.should_not_receive(:handler)
178
- subject.subscribe(:frog, subject.method(:handler))
179
- subject.publish(:frog, 1, 2).should be_nil
180
- end
181
-
182
- it "should act normally if the subject is enabled" do
183
- subject.should_receive(:enabled?).and_return(true)
184
- subject.should_receive(:handler)
185
- subject.subscribe(:frog, subject.method(:handler))
186
- subject.publish(:frog, 1, 2).should be_nil
187
- end
188
-
189
- it "should only call the handlers requested" do
190
- Test.event :fish
191
-
192
- subject.should_receive(:handler1).with(subject)
193
- subject.should_not_receive(:handler2)
194
- subject.subscribe(:frog, subject.method(:handler1))
195
- subject.subscribe(:fish, subject.method(:handler2))
196
- subject.publish(:frog).should be_nil
197
- end
198
-
199
- it "should automatically call a method on the publisher if it exists" do
200
- subject.should_receive(:frog).with(subject)
201
- subject.publish(:frog).should be_nil
202
- end
203
-
204
- it "should fail if the event name isn't handled by this object" do
205
- ->{ subject.publish(:unhandled_event) }.should raise_error ArgumentError
206
- end
207
- end
208
- end
209
- end
1
+ require_relative 'helpers/helper'
2
+
3
+ require 'event'
4
+
5
+ module Fidgit
6
+ describe Event do
7
+ before :each do
8
+ class Test
9
+ include Event
10
+ end
11
+ end
12
+
13
+ after :each do
14
+ Fidgit.send :remove_const, :Test
15
+ end
16
+
17
+ subject { Test }
18
+
19
+ describe "events" do
20
+ it "should initially be empty" do
21
+ subject.events.should be_empty
22
+ end
23
+ end
24
+
25
+ describe "event" do
26
+ it "should add the event to the list of events handled" do
27
+ subject.event :frog
28
+ subject.events.should include :frog
29
+ end
30
+
31
+ it "should inherit parent's events and be able to add more" do
32
+ Test.event :frog
33
+ class Test2 < Test; end
34
+ Test2.event :fish
35
+ Test2.events.should include :frog
36
+ Test2.events.should include :fish
37
+ end
38
+ end
39
+
40
+ context "When included into a class that is instanced" do
41
+ subject { Test.event :frog; Test.new }
42
+
43
+ describe "#subscribe" do
44
+ it "should add a handler as a block" do
45
+ subject.subscribe(:frog) { puts "hello" }
46
+ end
47
+
48
+ it "should fail if the event name isn't handled by this object" do
49
+ ->{ subject.subscribe(:unhandled_event) {} }.should raise_error ArgumentError
50
+ end
51
+
52
+ it "should add a handler as a method" do
53
+ subject.stub! :handler
54
+ subject.subscribe(:frog, subject.method(:handler))
55
+ end
56
+
57
+ it "should fail if neither a method or a block is passed" do
58
+ ->{ subject.subscribe(:frog) }.should raise_error ArgumentError
59
+ end
60
+
61
+ it "should fail if both a method and a block is passed" do
62
+ subject.stub! :handler
63
+ ->{ subject.subscribe(:frog, subject.method(:handler)) { } }.should raise_error ArgumentError
64
+ end
65
+
66
+ it "should return a Subscription" do
67
+ handler = proc {}
68
+ result = subject.subscribe(:frog, &handler)
69
+ result.should be_a Fidgit::Event::Subscription
70
+ result.handler.should be handler
71
+ result.publisher.should be subject
72
+ result.event.should equal :frog
73
+ end
74
+
75
+ it "should return a Subscription that can use Subscription#unsubscribe" do
76
+ handler_ran = false
77
+ subscription = subject.subscribe(:frog) { handler_ran = true }
78
+ subscription.unsubscribe
79
+ subject.publish :frog
80
+ handler_ran.should be_false
81
+ end
82
+ end
83
+
84
+ describe "#unsubscribe" do
85
+ it "should accept a Subscription" do
86
+ handler_ran = false
87
+ subscription = subject.subscribe(:frog) { handler_ran = true }
88
+ subject.unsubscribe subscription
89
+ subject.publish :frog
90
+ handler_ran.should be_false
91
+ end
92
+
93
+ it "should accept a handler" do
94
+ handler_ran = false
95
+ handler = proc { handler_ran = true }
96
+ subject.subscribe(:frog, &handler)
97
+ subject.unsubscribe handler
98
+ subject.publish :frog
99
+ handler_ran.should be_false
100
+ end
101
+
102
+ it "should accept an event and handler" do
103
+ handler_ran = false
104
+ handler = proc { handler_ran = true }
105
+ subject.subscribe(:frog, &handler)
106
+ subject.unsubscribe :frog, handler
107
+ subject.publish :frog
108
+ handler_ran.should be_false
109
+ end
110
+
111
+ it "should require 1..2 arguments" do
112
+ ->{ subject.unsubscribe }.should raise_error ArgumentError
113
+ ->{ subject.unsubscribe 1, 2, 3 }.should raise_error ArgumentError
114
+ end
115
+
116
+ it "should fail with bad types" do
117
+ ->{ subject.unsubscribe 1 }.should raise_error TypeError
118
+ ->{ subject.unsubscribe 1, ->{} }.should raise_error TypeError
119
+ ->{ subject.unsubscribe :event, 2 }.should raise_error TypeError
120
+ end
121
+
122
+ it "should fail if passed a Subscription to another object" do
123
+ subscription = Fidgit::Event::Subscription.new(Object.new.extend(Fidgit::Event), :event, proc {})
124
+ ->{ subject.unsubscribe subscription }.should raise_error ArgumentError
125
+ end
126
+ end
127
+
128
+ describe "#publish" do
129
+ it "should return nil if there are no handlers" do
130
+ subject.publish(:frog).should be_nil
131
+ end
132
+
133
+ it "should return nil if there are no handlers that handle the event" do
134
+ subject.should_receive(:frog).with(subject)
135
+ subject.should_receive(:handler).with(subject)
136
+ subject.subscribe(:frog, subject.method(:handler))
137
+ subject.publish(:frog).should be_nil
138
+ end
139
+
140
+ it "should return :handled if a manual handler handled the event and not call other handlers" do
141
+ subject.should_not_receive(:handler1)
142
+ subject.should_receive(:handler2).with(subject).and_return(:handled)
143
+ subject.subscribe(:frog, subject.method(:handler1))
144
+ subject.subscribe(:frog, subject.method(:handler2))
145
+ subject.publish(:frog).should == :handled
146
+ end
147
+
148
+ it "should return :handled if an automatic handler handled the event and not call other handlers" do
149
+ subject.should_receive(:frog).with(subject).and_return(:handled)
150
+ subject.should_not_receive(:handler2)
151
+ subject.subscribe(:frog, subject.method(:handler2))
152
+ subject.publish(:frog).should == :handled
153
+ end
154
+
155
+ it "should pass the object as the first parameter" do
156
+ subject.should_receive(:handler).with(subject)
157
+ subject.subscribe(:frog, subject.method(:handler))
158
+ subject.publish :frog
159
+ end
160
+
161
+ it "should call all the handlers, once each" do
162
+ subject.should_receive(:handler1).with(subject)
163
+ subject.should_receive(:handler2).with(subject)
164
+ subject.subscribe(:frog, subject.method(:handler1))
165
+ subject.subscribe(:frog, subject.method(:handler2))
166
+ subject.publish(:frog).should be_nil
167
+ end
168
+
169
+ it "should pass parameters passed to it" do
170
+ subject.should_receive(:handler).with(subject, 1, 2)
171
+ subject.subscribe(:frog, subject.method(:handler))
172
+ subject.publish(:frog, 1, 2).should be_nil
173
+ end
174
+
175
+ it "should do nothing if the subject is disabled" do
176
+ subject.should_receive(:enabled?).and_return(false)
177
+ subject.should_not_receive(:handler)
178
+ subject.subscribe(:frog, subject.method(:handler))
179
+ subject.publish(:frog, 1, 2).should be_nil
180
+ end
181
+
182
+ it "should act normally if the subject is enabled" do
183
+ subject.should_receive(:enabled?).and_return(true)
184
+ subject.should_receive(:handler)
185
+ subject.subscribe(:frog, subject.method(:handler))
186
+ subject.publish(:frog, 1, 2).should be_nil
187
+ end
188
+
189
+ it "should only call the handlers requested" do
190
+ Test.event :fish
191
+
192
+ subject.should_receive(:handler1).with(subject)
193
+ subject.should_not_receive(:handler2)
194
+ subject.subscribe(:frog, subject.method(:handler1))
195
+ subject.subscribe(:fish, subject.method(:handler2))
196
+ subject.publish(:frog).should be_nil
197
+ end
198
+
199
+ it "should automatically call a method on the publisher if it exists" do
200
+ subject.should_receive(:frog).with(subject)
201
+ subject.publish(:frog).should be_nil
202
+ end
203
+
204
+ it "should fail if the event name isn't handled by this object" do
205
+ ->{ subject.publish(:unhandled_event) }.should raise_error ArgumentError
206
+ end
207
+ end
208
+ end
209
+ end
210
210
  end