class Cura::Event::Base
The base class for all events.
Attributes
created_at[R]
Get the time this event was created.
@return [Time]
target[R]
Get the target this event was dispatched to. TODO: Rename to source.
The source is the component the event was originally sent to. The target is the component the event is currently being handled by. <- Needed?
@return [Cura::Attributes::HasEvents]
Public Class Methods
inherited(subclass)
click to toggle source
Add the subclass to `Event.all`, when inherited.
# File lib/cura/event/base.rb, line 33 def inherited(subclass) Event.all << subclass end
name()
click to toggle source
Get the name of this class as a symbol. For example, `SomeAction` would be `:some_action`.
@return [Symbol]
# File lib/cura/event/base.rb, line 17 def name # Note: 1.3 times faster but relys on Regexp and is the only usage of regexp throughout cura. # Using non regexp version until multiple usages of regexp occur within cura. # to_s.split(/::/).last.gsub(/([a-z])([A-Z])/, '\1_\2').downcase.to_sym # Note: MRuby does not have a #chars method so this is now 2.6 times slower instead of 1.3 caps = ("A".."Z") index = 0 to_s.split("::").last.split("").each_with_object("") do |char, memo| memo << "_" if index > 0 && caps.include?(char) memo << char index += 1 end.downcase.to_sym end
new(attributes={})
click to toggle source
Calls superclass method
Cura::Attributes::HasAttributes::new
# File lib/cura/event/base.rb, line 41 def initialize(attributes={}) @created_at = Time.now super end
Public Instance Methods
==(other)
click to toggle source
Check if something is equivalent to this event.
@param [Object] other The object to check equivalence with. @return [Boolean]
# File lib/cura/event/base.rb, line 81 def ==(other) # TODO: Below is needed? # object_equivalence = super # return true if object_equivalence other = other.to_h if other.respond_to?(:to_h) other == to_h end
dispatch()
click to toggle source
Dispatches this event directly to it's target, if it has an application.
@return [Event::Base] This event.
# File lib/cura/event/base.rb, line 94 def dispatch target.event_handler.handle(self) self end
target=(value)
click to toggle source
Set the target this event was dispatched to.
@param [Cura::Attributes::HasEvents] value @return [Cura::Attributes::HasEvents]
# File lib/cura/event/base.rb, line 64 def target=(value) raise TypeError, "target must be a Cura::Attributes::HasEvents" unless value.is_a?(Cura::Attributes::HasEvents) # TODO: Error::Event::InvalidTarget @target = value end
to_h()
click to toggle source
Get this event as a Hash.
@return [Hash]
# File lib/cura/event/base.rb, line 73 def to_h { name: self.class.name } end