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.
41 42 43 44 45 |
# File 'lib/cura/event/base.rb', line 41 def initialize(attributes={}) @created_at = Time.now super end |
Instance Attribute Details
#created_at ⇒ Time (readonly)
Get the time this event was created.
50 51 52 |
# File 'lib/cura/event/base.rb', line 50 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?
58 59 60 |
# File 'lib/cura/event/base.rb', line 58 def target @target end |
Class Method Details
.inherited(subclass) ⇒ Object
Add the subclass to ‘Event.all`, when inherited.
33 34 35 |
# File 'lib/cura/event/base.rb', line 33 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`.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# 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 |
Instance Method Details
#==(other) ⇒ Boolean
Check if something is equivalent to this event.
81 82 83 84 85 86 87 88 89 |
# 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 ⇒ Event::Base
Dispatches this event directly to it’s target.
94 95 96 97 98 |
# File 'lib/cura/event/base.rb', line 94 def dispatch target.event_handler.handle(self) self end |
#to_h ⇒ Hash
Get this event as a Hash.
73 74 75 |
# File 'lib/cura/event/base.rb', line 73 def to_h { name: self.class.name } end |