unobservable 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/README.rdoc +85 -2
  2. data/Rakefile +1 -0
  3. data/TODO.rdoc +5 -0
  4. data/VERSION +1 -1
  5. data/lib/unobservable.rb +5 -0
  6. metadata +3 -2
data/README.rdoc CHANGED
@@ -1,9 +1,92 @@
1
1
  = unobservable
2
2
 
3
3
  Ruby's Observable mixin is often characterized as an Event Handler library. In reality, it only provides basic
4
- support for "Property Changed" notifications.
4
+ support for "Property Changed" notifications. If an object needs to raise several different types of events,
5
+ then the Observable mixin is the wrong tool for the job.
5
6
 
6
- Unobservable strives to be a general-purpose Event Handler library.
7
+ Unobservable overcomes the limitations of the Observable mixin by allowing objects to own one or more Event
8
+ objects.
9
+
10
+ == 2-Minute Tour
11
+
12
+ Instead of a lengthy introduction, here's a short program that demonstrates the main concepts:
13
+
14
+
15
+ require 'unobservable'
16
+
17
+
18
+ class Button
19
+ # This will add basic support for Events to this class. As a bonus, it will
20
+ # also make the attr_event keyword available to us.
21
+ include Unobservable::Support
22
+
23
+ # The attr_event keyword allows us to declare the Events that are available
24
+ # to the instances of the Button class.
25
+ attr_event :clicked, :double_clicked
26
+
27
+
28
+ # This method will raise the :clicked event when it is invoked.
29
+ def click(x, y)
30
+ raise_event(:clicked, x, y)
31
+ end
32
+
33
+
34
+ # This method will raise the :double_clicked event when it is invoked
35
+ def double_click(x, y)
36
+ raise_event(:double_clicked, x, y)
37
+ end
38
+ end
39
+
40
+
41
+ # This class does not publish any events, so it does not need to include
42
+ # the Unobserable::Support mixin.
43
+ class Textbox
44
+ attr_accessor :text
45
+ end
46
+
47
+
48
+ # Now let's create some instances of these classes
49
+ button = Button.new
50
+ textbox = Textbox.new
51
+
52
+
53
+ # We want to automatically update the textbox's text whenever we click
54
+ # the button. So, let's register an event handler:
55
+ button.clicked.register do |x, y|
56
+ textbox.text = "You just clicked: #{x} #{y}"
57
+ end
58
+
59
+
60
+ # We want to print the [x, y] coordinates to the console whenever the
61
+ # button is double-clicked. So, we can register an event handler that
62
+ # just calls the Kernel#puts method directly.
63
+ button.double_clicked.register Kernel, :puts
64
+
65
+
66
+ # Show time! First, let's print the textbox's text just to verify
67
+ # that it's currently null:
68
+ puts "Before Clicking: #{textbox.text}"
69
+
70
+ # Now click the button. This should raise the :clicked event, which
71
+ # will invoke its event handlers:
72
+ button.click(2, 3)
73
+
74
+
75
+ # As expected, the event handler that we registered to the :clicked
76
+ # event updated the textbox's text.
77
+ puts "After Clicking: #{textbox.text}"
78
+
79
+
80
+ # Now double-click the button. This should raise the :double_clicked
81
+ # event, which will invoke its event handlers. As a result, the
82
+ # coordinates will be printed to the console.
83
+ button.double_click(15, 2)
84
+
85
+
86
+ # We did not register any event handlers that would change the textbox
87
+ # when the button was double-clicked. Therefore, we should find that
88
+ # the textbox's text has remained unchanged.
89
+ puts "After Double-Clicking: #{textbox.text}"
7
90
 
8
91
 
9
92
  == Usage
data/Rakefile CHANGED
@@ -49,5 +49,6 @@ Rake::RDocTask.new do |rdoc|
49
49
  rdoc.rdoc_dir = 'rdoc'
50
50
  rdoc.title = "unobservable #{version}"
51
51
  rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('TODO*')
52
53
  rdoc.rdoc_files.include('lib/**/*.rb')
53
54
  end
data/TODO.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = TODO
2
+
3
+ * Add support for singleton events
4
+ * Figure out a decent way to restrict access to Unobservable::Event#call to encourage proper encapsulation.
5
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
data/lib/unobservable.rb CHANGED
@@ -114,6 +114,7 @@ module Unobservable
114
114
 
115
115
 
116
116
 
117
+ # Minimalistic Event implementation
117
118
  class Event
118
119
  attr_reader :handlers
119
120
 
@@ -147,6 +148,10 @@ module Unobservable
147
148
  end
148
149
 
149
150
 
151
+ # Removes a single instance of the specified event handler
152
+ # from the list of event handlers. Therefore, if you've
153
+ # registered the same event handler 3 times, then you will
154
+ # need to unregister it 3 times as well.
150
155
  def unregister(*args, &block)
151
156
  h = handler_for(*args, &block)
152
157
  index = @handlers.index(h)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unobservable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -88,6 +88,7 @@ files:
88
88
  - LICENSE.txt
89
89
  - README.rdoc
90
90
  - Rakefile
91
+ - TODO.rdoc
91
92
  - VERSION
92
93
  - lib/unobservable.rb
93
94
  - test/helper.rb
@@ -107,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
108
  version: '0'
108
109
  segments:
109
110
  - 0
110
- hash: 2472220124637666856
111
+ hash: 1737406497350544853
111
112
  required_rubygems_version: !ruby/object:Gem::Requirement
112
113
  none: false
113
114
  requirements: