wires 0.1.12 → 0.2.0

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/lib/wires/events.rb DELETED
@@ -1,159 +0,0 @@
1
-
2
- # Store a list of all Event classes that get loaded.
3
- class EventRegistry
4
- @@registry = []
5
-
6
- def self.<<(cls)
7
- @@registry << cls
8
- @@registry.uniq!
9
- end
10
-
11
- def self.list
12
- @@registry
13
- end
14
- end
15
-
16
- # All Event classes should inherit from this one
17
- class Event < Object # explicit for the sake of Event.ancestry
18
-
19
- # Register with the EventRegistry and make subclasses do the same
20
- EventRegistry << self
21
-
22
- # Operate on the metaclass as a type of singleton pattern
23
- class << self
24
-
25
- def inherited(subcls)
26
-
27
- # Be sure codestring doesn't conflict
28
- existing = _from_codestring(subcls.codestring)
29
- if existing then raise NameError, \
30
- "New Event subclass '#{subcls}' conflicts with"\
31
- " existing Event subclass '#{existing}'."\
32
- " The generated codestring '#{subcls.codestring}'"\
33
- " must be unique for each Event subclass." end
34
-
35
- # Register, then call super
36
- EventRegistry << subcls
37
- super
38
- end
39
-
40
- # List of class inheritance lineage back to but excluding Object
41
- def ancestry(cls=self)
42
- _next = cls.superclass
43
- [cls==Object ? [] : [cls, ancestry(_next)]].flatten
44
- end
45
-
46
- # Convert class <ClassNameEvent> to string "class_name"
47
- def codestring(cls=self)
48
- cls.to_s
49
- .underscore
50
- .gsub(/_event$/, "")
51
- end
52
-
53
- # List of codestrings associated with this event and ancestors
54
- def codestrings
55
- x = ancestry
56
- .map {|cls| cls.codestring}
57
- end
58
-
59
- # Pull class from registry by codestring
60
- # (more reliable than crafting a reverse regexp)
61
- def _from_codestring(str)
62
- return EventRegistry.list
63
- .select{|e| e.codestring==str}[0]
64
- end; private :_from_codestring
65
-
66
- def from_codestring(str)
67
- cls = _from_codestring(str.to_s)
68
- if not cls then raise NameError,
69
- "No known Event subclass with codestring: '#{str}'" end
70
- cls
71
- end
72
-
73
- # Convert an event from 'array notation' to an Event subclass instance
74
- # TODO: List acceptable input forms here for documentation
75
- def new_from(input)
76
-
77
- # Standardize to array and pull out arguments if they exist
78
- input = [input] unless input.is_a? Array
79
- input, *args = input
80
-
81
- # Create event object from event as an object, class, or symbol/string
82
- event = case input
83
- when Event
84
- input
85
- when Class
86
- input.new(*args) if input < Event
87
- else
88
- Event.from_codestring(input.to_s).new(*args)
89
- end
90
- end
91
-
92
- # Create attributes and accessors for all arguments to the constructor.
93
- # This is done here rather than in initialize so that the functionality
94
- # will remain if the user developer overrides initialize in the subclass.
95
- def new(*args, &block)
96
- obj = super
97
-
98
- kwargs = args[-1].is_a?(Hash) ? args.pop : Hash.new
99
- kwargs[:args] = args
100
- kwargs[:codeblock] = block if block
101
- for key in kwargs.keys
102
- att = key.to_s
103
- obj.instance_variable_set("@#{att}", kwargs[key])
104
- class_eval("def #{att}; @#{att}; end")
105
- class_eval("def #{att}=(val); @#{att}=val; end")
106
- end
107
-
108
- obj
109
- end
110
-
111
- end
112
-
113
- # Calling super in new with *args will complain if this isn't here
114
- def initialize(*args, &block) end
115
- end
116
-
117
-
118
- #
119
- # Comparison support for Events and Symbols/Strings
120
- #
121
-
122
- # Reopen Event and add comparison functions
123
- class Event
124
- class << self
125
- def ==(other)
126
- other.is_a?(Class) ?
127
- super : codestring==other.to_s
128
- end
129
- def <=(other)
130
- other.is_a?(Class) ?
131
- super : codestrings.include?(other.to_s)
132
- end
133
- def <(other)
134
- other.is_a?(Class) ?
135
- super : (self<=other and not self==other)
136
- end
137
- def >=(other)
138
- other.is_a?(Class) ?
139
- super : Event.from_codestring(other.to_s)<=self
140
- end
141
- def >(other)
142
- other.is_a?(Class) ?
143
- super : Event.from_codestring(other.to_s)<self
144
- end
145
- end
146
- end
147
-
148
- # Autogenerate the inverse comparison functions for Symbol/String
149
- for cls in [Symbol, String]
150
- %w(== < > <= >=).zip(%w(== > < >= <=))
151
- .each do |ops|
152
- op, opinv = ops # unzip operator and inverse operator
153
- cls.class_eval(
154
- "def #{op}(other)\n"\
155
- " (other.is_a?(Class) and other<=Event) ? \n"\
156
- " (other#{opinv}self) : super\n"\
157
- "end\n")
158
- end
159
- end