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.



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

Get the time this event was created.

Returns:

  • (Time)


55
56
57
# File 'lib/cura/event/base.rb', line 55

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?


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

.nameSymbol

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

Returns:

  • (Symbol)


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.

Parameters:

  • other (Object)

    The object to check equivalence with.

Returns:

  • (Boolean)


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

#dispatchEvent::Base

Dispatches this event directly to it’s target.

Returns:



99
100
101
102
103
# File 'lib/cura/event/base.rb', line 99

def dispatch
  target.event_handler.handle(self)
  
  self
end

#to_hHash

Get this event as a Hash.

Returns:

  • (Hash)


78
79
80
# File 'lib/cura/event/base.rb', line 78

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