Class: Wires::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/wires/event.rb,
lib/wires/event.rb

Overview

Reopen Event and add comparison functions

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Event

Calling super in new with *args will complain if this isn’t here



115
# File 'lib/wires/event.rb', line 115

def initialize(*args, &block) end

Class Method Details

.<(other) ⇒ Object



134
135
136
137
# File 'lib/wires/event.rb', line 134

def <(other)
  other.is_a?(Class) ? 
  super : (self<=other and not self==other)
end

.<=(other) ⇒ Object



130
131
132
133
# File 'lib/wires/event.rb', line 130

def <=(other)
  other.is_a?(Class) ? 
  super : codestrings.include?(other.to_s)
end

.==(other) ⇒ Object



126
127
128
129
# File 'lib/wires/event.rb', line 126

def ==(other)
  other.is_a?(Class) ? 
  super : codestring==other.to_s
end

.>(other) ⇒ Object



142
143
144
145
# File 'lib/wires/event.rb', line 142

def >(other)
  other.is_a?(Class) ? 
  super : Event.from_codestring(other.to_s)<self
end

.>=(other) ⇒ Object



138
139
140
141
# File 'lib/wires/event.rb', line 138

def >=(other)
  other.is_a?(Class) ? 
  super : Event.from_codestring(other.to_s)<=self
end

._from_codestring(str) ⇒ Object

Pull class from registry by codestring (more reliable than crafting a reverse regexp)



62
63
64
65
# File 'lib/wires/event.rb', line 62

def _from_codestring(str)
  return EventRegistry.list
  .select{|e| e.codestring==str}[0]
end

.ancestry(cls = self) ⇒ Object

List of class inheritance lineage back to but excluding Object



42
43
44
45
# File 'lib/wires/event.rb', line 42

def ancestry(cls=self)
  _next = cls.superclass
  [cls==Object ? [] : [cls, ancestry(_next)]].flatten
end

.codestring(cls = self) ⇒ Object

Convert class <ClassNameEvent> to string “class_name”



48
49
50
51
52
# File 'lib/wires/event.rb', line 48

def codestring(cls=self)
  File.basename cls.to_s
  .underscore
  .gsub(/_event$/, "")
end

.codestringsObject

List of codestrings associated with this event and ancestors



55
56
57
58
# File 'lib/wires/event.rb', line 55

def codestrings
  x = ancestry
  .map {|cls| cls.codestring}
end

.from_codestring(str) ⇒ Object



67
68
69
70
71
72
# File 'lib/wires/event.rb', line 67

def from_codestring(str)
cls = _from_codestring(str.to_s)
if not cls then raise NameError,
  "No known Event subclass with codestring: '#{str}'" end
  cls
end

.inherited(subcls) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wires/event.rb', line 26

def inherited(subcls)
  
  # Be sure codestring doesn't conflict
  existing = _from_codestring(subcls.codestring)
  if existing then raise NameError, \
    "New Event subclass '#{subcls}' conflicts with"\
    " existing Event subclass '#{existing}'."\
    " The generated codestring '#{subcls.codestring}'"\
    " must be unique for each Event subclass." end
    
  # Register, then call super
  EventRegistry << subcls
  super
end

.new(*args, &block) ⇒ Object

Create attributes and accessors for all arguments to the constructor. This is done here rather than in initialize so that the functionality will remain if the user developer overrides initialize in the subclass.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/wires/event.rb', line 96

def new(*args, &block)
  obj = super
  
  kwargs = args[-1].is_a?(Hash) ? args.pop : Hash.new
  kwargs[:args] = args
  kwargs[:codeblock] = block if block
  for key in kwargs.keys
    att = key.to_s
    obj.instance_variable_set("@#{att}", kwargs[key])
    class_eval("def #{att}; @#{att}; end")
    # class_eval("def #{att}=(val); @#{att}=val; end")
  end
  
  obj
end

.new_from(input) ⇒ Object

Convert an event from ‘array notation’ to an Event subclass instance TODO: List acceptable input forms here for documentation



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wires/event.rb', line 76

def new_from(input)
  
  # Standardize to array and pull out arguments if they exist
  input = [input] unless input.is_a? Array
  input, *args = input
  
  # Create event object from event as an object, class, or symbol/string
  event = case input
    when Event
      input
    when Class
      input.new(*args) if input < Event
    else
      Event.from_codestring(input.to_s).new(*args)
  end
end