Class: SpeedGun::Event
- Inherits:
-
Object
- Object
- SpeedGun::Event
- Defined in:
- lib/speed_gun/event.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#finished_at ⇒ Time?
readonly
Finished time of event.
-
#id ⇒ String
readonly
Event ID.
-
#name ⇒ String
readonly
Event name.
-
#payload ⇒ Hash
readonly
Event payload.
-
#started_at ⇒ Time
readonly
Started time of event.
Class Method Summary collapse
Instance Method Summary collapse
- #duration ⇒ Object
- #finish! ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(name, payload = {}, started_at = Time.now, finished_at = nil) ⇒ Event
constructor
A new instance of Event.
- #roughly_finished_at ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(name, payload = {}, started_at = Time.now, finished_at = nil) ⇒ Event
Returns a new instance of Event.
29 30 31 32 33 34 35 36 |
# File 'lib/speed_gun/event.rb', line 29 def initialize(name, payload = {}, started_at = Time.now, finished_at = nil) @id = SecureRandom.uuid @name = name.to_s @payload = payload @started_at = started_at @finished_at = finished_at @children = [] end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
16 17 18 |
# File 'lib/speed_gun/event.rb', line 16 def children @children end |
#finished_at ⇒ Time? (readonly)
Returns Finished time of event.
15 16 17 |
# File 'lib/speed_gun/event.rb', line 15 def finished_at @finished_at end |
#id ⇒ String (readonly)
Returns Event ID.
7 8 9 |
# File 'lib/speed_gun/event.rb', line 7 def id @id end |
#name ⇒ String (readonly)
Returns Event name.
9 10 11 |
# File 'lib/speed_gun/event.rb', line 9 def name @name end |
#payload ⇒ Hash (readonly)
Returns Event payload.
11 12 13 |
# File 'lib/speed_gun/event.rb', line 11 def payload @payload end |
#started_at ⇒ Time (readonly)
Returns Started time of event.
13 14 15 |
# File 'lib/speed_gun/event.rb', line 13 def started_at @started_at end |
Class Method Details
.from_hash(hash, id = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/speed_gun/event.rb', line 18 def self.from_hash(hash, id = nil) new( hash['name'], hash['payload'], Time.at(hash['started_at'].to_f), hash['finished_at'] ? Time.at(hash['finished_at']) : nil ).tap do |event| event.instance_variable_set(:@id, id) if id end end |
Instance Method Details
#duration ⇒ Object
50 51 52 |
# File 'lib/speed_gun/event.rb', line 50 def duration finished_at ? finished_at.to_f - started_at.to_f : 0 end |
#finish! ⇒ Object
38 39 40 |
# File 'lib/speed_gun/event.rb', line 38 def finish! @finished_at = Time.now end |
#finished? ⇒ Boolean
42 43 44 |
# File 'lib/speed_gun/event.rb', line 42 def finished? @finished_at end |
#roughly_finished_at ⇒ Object
46 47 48 |
# File 'lib/speed_gun/event.rb', line 46 def roughly_finished_at finished_at || started_at end |
#to_hash ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/speed_gun/event.rb', line 54 def to_hash { 'name' => name, 'payload' => payload, 'started_at' => started_at.to_f, 'finished_at' => finished? ? finished_at.to_f : nil } end |