unobservable 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +3 -10
  2. data/VERSION +1 -1
  3. data/lib/unobservable.rb +106 -10
  4. metadata +2 -2
data/README.rdoc CHANGED
@@ -1,16 +1,9 @@
1
1
  = unobservable
2
2
 
3
- Description goes here.
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
5
 
5
- == Contributing to unobservable
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
- * Fork the project.
10
- * Start a feature/bugfix branch.
11
- * Commit and push until you are happy with your contribution.
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
6
+ Unobservable strives to be a general-purpose Event Handler library.
14
7
 
15
8
  == Copyright
16
9
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
data/lib/unobservable.rb CHANGED
@@ -1,9 +1,96 @@
1
+ require 'set'
1
2
 
2
3
  module Unobservable
3
-
4
+
5
+ def self.instance_events_for(mod, include_supers = true)
6
+ raise TypeError, "Only modules and classes can have instance_events" unless mod.is_a? Module
7
+
8
+ contributors = [mod]
9
+ if include_supers
10
+ contributors += mod.included_modules
11
+ contributors += mod.ancestors[1...-1] if mod.is_a? Class
12
+ end
13
+
14
+ retval = Set.new
15
+
16
+ contributors.each do |c|
17
+ if c.instance_variable_defined? :@unobservable_instance_events
18
+ c.instance_variable_get(:@unobservable_instance_events).each do |e|
19
+ retval.add(e)
20
+ end
21
+ end
22
+ end
23
+
24
+ return retval.to_a
25
+ end
26
+
27
+ module ModuleSupport
28
+ def instance_events(include_supers = true)
29
+ if include_supers == false
30
+ @unobservable_instance_events ||= Set.new
31
+ return @unobservable_instance_events.to_a
32
+ else
33
+ return Unobservable.instance_events_for(self, true)
34
+ end
35
+ end
36
+
37
+
38
+ private
39
+ def attr_event(*names)
40
+ @unobservable_instance_events ||= Set.new
41
+
42
+ names.each do |n|
43
+ @unobservable_instance_events.add(n.to_sym)
44
+ define_method n do
45
+ return event(n)
46
+ end
47
+ end
48
+
49
+ return @unobservable_instance_events.to_a
50
+ end
51
+ end
52
+
53
+
54
+
55
+
56
+ module Support
57
+
58
+ def self.included(other_mod)
59
+ other_mod.extend ModuleSupport
60
+ end
61
+
62
+ def events
63
+ unobservable_events_map.keys
64
+ end
65
+
66
+ def event(name)
67
+ unobservable_events_map[name]
68
+ end
69
+
70
+ private
71
+ def unobservable_events_map
72
+ @unobservable_events_map ||= initialize_unobservable_events_map(self.class)
73
+ end
74
+
75
+
76
+ def initialize_unobservable_events_map(clazz)
77
+ retval = {}
78
+ if clazz.respond_to? :instance_events
79
+ clazz.instance_events.each do |e|
80
+ retval[e] = Event.new
81
+ end
82
+ end
83
+
84
+ return retval
85
+ end
86
+
87
+ end
88
+
89
+
90
+
4
91
  class Event
5
92
  attr_reader :handlers
6
-
93
+
7
94
  def initialize
8
95
  @handlers = []
9
96
  end
@@ -21,10 +108,12 @@ module Unobservable
21
108
  elsif args.size == 2
22
109
  return args[0].method(args[1])
23
110
  end
24
-
111
+
25
112
  raise ArgumentError, "Unable to create an event handler using the given arguments"
26
113
  end
27
-
114
+
115
+ # Registers the given event handler so that it will be
116
+ # invoked when the event is raised.
28
117
  def register(*args, &block)
29
118
  h = handler_for(*args, &block)
30
119
  @handlers << h
@@ -42,8 +131,11 @@ module Unobservable
42
131
  return nil
43
132
  end
44
133
  end
45
-
46
-
134
+
135
+
136
+ # Pass the specific arguments / block to all of the
137
+ # event handlers. Return true if there was at least
138
+ # 1 event handler; return false otherwise.
47
139
  def call(*args, &block)
48
140
  if @handlers.empty?
49
141
  return false
@@ -52,12 +144,16 @@ module Unobservable
52
144
  @handlers.each do |h|
53
145
  h.call(*args, &block)
54
146
  end
55
-
147
+
56
148
  return true
57
149
  end
58
150
  end
59
-
60
-
151
+
152
+
61
153
  end
62
154
 
63
- end
155
+
156
+
157
+ end
158
+
159
+
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.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -107,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
107
  version: '0'
108
108
  segments:
109
109
  - 0
110
- hash: 4412780719784246563
110
+ hash: -507085936034972992
111
111
  required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  none: false
113
113
  requirements: