Class: Cura::Event::Base

Inherits:
Object
  • Object
show all
Includes:
Attributes::HasAttributes, Attributes::HasInitialize
Defined in:
lib/cura/event/base.rb

Overview

The base class for all events.

Direct Known Subclasses

Click, Focus, KeyDown, Mouse, Resize, Selected, Unfocus

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes::HasAttributes

included, #update_attributes

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_atTime (readonly)

Get the time this event was created.

Returns:

  • (Time)


50
51
52
# File 'lib/cura/event/base.rb', line 50

def created_at
  @created_at
end

#targetCura::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

.nameSymbol

Get the name of this class as a symbol. For example, ‘SomeAction` would be `:some_action`.

Returns:

  • (Symbol)


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.

Parameters:

  • other (Object)

    The object to check equivalence with.

Returns:

  • (Boolean)


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

#dispatchEvent::Base

Dispatches this event directly to it’s target.

Returns:



94
95
96
97
98
# File 'lib/cura/event/base.rb', line 94

def dispatch
  target.event_handler.handle(self)

  self
end

#to_hHash

Get this event as a Hash.

Returns:

  • (Hash)


73
74
75
# File 'lib/cura/event/base.rb', line 73

def to_h
  { name: self.class.name }
end