unobservable 0.1.1 → 0.1.3
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.
- data/VERSION +1 -1
- data/lib/unobservable.rb +39 -12
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/lib/unobservable.rb
CHANGED
@@ -2,6 +2,10 @@ require 'set'
|
|
2
2
|
|
3
3
|
module Unobservable
|
4
4
|
|
5
|
+
# Produces a list of instance events for any module regardless of whether or
|
6
|
+
# not that module includes the Unobservable::ModuleSupport mixin. If
|
7
|
+
# include_supers = true, then the list will also contain instance events
|
8
|
+
# defined by superclasses and included modules. By default, include_supers = true
|
5
9
|
def self.instance_events_for(mod, include_supers = true)
|
6
10
|
raise TypeError, "Only modules and classes can have instance_events" unless mod.is_a? Module
|
7
11
|
|
@@ -24,18 +28,22 @@ module Unobservable
|
|
24
28
|
return retval.to_a
|
25
29
|
end
|
26
30
|
|
31
|
+
|
32
|
+
|
33
|
+
# This module is a mixin that provides support for "instance events".
|
27
34
|
module ModuleSupport
|
35
|
+
|
36
|
+
# This is just a shortcut for Unobservable#instance_events_for . It passes
|
37
|
+
# in self as the first argument.
|
28
38
|
def instance_events(include_supers = true)
|
29
|
-
|
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
|
39
|
+
Unobservable.instance_events_for(self, include_supers)
|
35
40
|
end
|
36
41
|
|
37
42
|
|
38
43
|
private
|
44
|
+
|
45
|
+
# This helper method is similar to attr_reader and attr_accessor. It allows
|
46
|
+
# for instance events to be declared inside the body of the class.
|
39
47
|
def attr_event(*names)
|
40
48
|
@unobservable_instance_events ||= Set.new
|
41
49
|
|
@@ -48,16 +56,12 @@ module Unobservable
|
|
48
56
|
|
49
57
|
return @unobservable_instance_events.to_a
|
50
58
|
end
|
51
|
-
end
|
52
|
-
|
53
59
|
|
60
|
+
end
|
54
61
|
|
55
62
|
|
56
|
-
module Support
|
57
63
|
|
58
|
-
|
59
|
-
other_mod.extend ModuleSupport
|
60
|
-
end
|
64
|
+
module ObjectSupport
|
61
65
|
|
62
66
|
def events
|
63
67
|
unobservable_events_map.keys
|
@@ -68,6 +72,11 @@ module Unobservable
|
|
68
72
|
end
|
69
73
|
|
70
74
|
private
|
75
|
+
def raise_event(name, *args, &block)
|
76
|
+
event(name).call(*args, &block)
|
77
|
+
end
|
78
|
+
|
79
|
+
|
71
80
|
def unobservable_events_map
|
72
81
|
@unobservable_events_map ||= initialize_unobservable_events_map(self.class)
|
73
82
|
end
|
@@ -85,6 +94,23 @@ module Unobservable
|
|
85
94
|
end
|
86
95
|
|
87
96
|
end
|
97
|
+
|
98
|
+
|
99
|
+
# Typically, when you add support for Events to a class, you also want
|
100
|
+
# support for the handy attr_event keyword. So, including this module
|
101
|
+
# is equivalent to the following:
|
102
|
+
#
|
103
|
+
# class MyClass
|
104
|
+
# extend Unobservable::ModuleSupport # Get support for the attr_event keyword
|
105
|
+
# include Unobservable::ObjectSupport # Get support for the instance methods
|
106
|
+
# end
|
107
|
+
module Support
|
108
|
+
include ObjectSupport
|
109
|
+
|
110
|
+
def self.included(other_mod)
|
111
|
+
other_mod.extend ModuleSupport
|
112
|
+
end
|
113
|
+
end
|
88
114
|
|
89
115
|
|
90
116
|
|
@@ -133,6 +159,7 @@ module Unobservable
|
|
133
159
|
end
|
134
160
|
|
135
161
|
|
162
|
+
|
136
163
|
# Pass the specific arguments / block to all of the
|
137
164
|
# event handlers. Return true if there was at least
|
138
165
|
# 1 event handler; return false otherwise.
|
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.
|
4
|
+
version: 0.1.3
|
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:
|
110
|
+
hash: 2472220124637666856
|
111
111
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
112
|
none: false
|
113
113
|
requirements:
|