Class: Cura::Event::Base
- Inherits:
-
Object
- Object
- Cura::Event::Base
- Defined in:
- lib/cura/event/base.rb
Overview
The base class for all events.
Instance Attribute Summary collapse
-
#created_at ⇒ Time
readonly
Get the time this event was created.
-
#target ⇒ Cura::Attributes::HasEvents
Get the target this event was dispatched to.
Class Method Summary collapse
-
.inherited(subclass) ⇒ Object
Add the subclass to ‘Event.all`, when inherited.
-
.name ⇒ Symbol
Get the name of this class as a symbol.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check if something is equivalent to this event.
-
#dispatch ⇒ Event::Base
Dispatches this event directly to it’s target.
-
#initialize(attributes = {}) ⇒ Base
constructor
A new instance of Base.
-
#to_h ⇒ Hash
Get this event as a Hash.
Methods included from Attributes::HasAttributes
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
46 47 48 49 50 |
# File 'lib/cura/event/base.rb', line 46 def initialize(attributes={}) @created_at = Time.now super end |
Instance Attribute Details
#created_at ⇒ Time (readonly)
Get the time this event was created.
55 56 57 |
# File 'lib/cura/event/base.rb', line 55 def created_at @created_at end |
#target ⇒ Cura::Attributes::HasEvents
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?
63 64 65 |
# File 'lib/cura/event/base.rb', line 63 def target @target end |
Class Method Details
.inherited(subclass) ⇒ Object
Add the subclass to ‘Event.all`, when inherited.
37 38 39 |
# File 'lib/cura/event/base.rb', line 37 def inherited(subclass) Event.all << subclass end |
.name ⇒ Symbol
Get the name of this class as a symbol. For example, ‘SomeAction` would be `:some_action`.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/cura/event/base.rb', line 21 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("") { |char, memo| memo << "_" if index > 0 && caps.include?(char) memo << char index += 1 }.downcase.to_sym end |
Instance Method Details
#==(other) ⇒ Boolean
Check if something is equivalent to this event.
86 87 88 89 90 91 92 93 94 |
# File 'lib/cura/event/base.rb', line 86 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 ⇒ Event::Base
Dispatches this event directly to it’s target.
99 100 101 102 103 |
# File 'lib/cura/event/base.rb', line 99 def dispatch target.event_handler.handle(self) self end |
#to_h ⇒ Hash
Get this event as a Hash.
78 79 80 |
# File 'lib/cura/event/base.rb', line 78 def to_h { name: self.class.name } end |