Class: SpeedGun::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/speed_gun/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#childrenObject (readonly)

Returns the value of attribute children.



16
17
18
# File 'lib/speed_gun/event.rb', line 16

def children
  @children
end

#finished_atTime? (readonly)

Returns Finished time of event.

Returns:

  • (Time, nil)

    Finished time of event



15
16
17
# File 'lib/speed_gun/event.rb', line 15

def finished_at
  @finished_at
end

#idString (readonly)

Returns Event ID.

Returns:

  • (String)

    Event ID



7
8
9
# File 'lib/speed_gun/event.rb', line 7

def id
  @id
end

#nameString (readonly)

Returns Event name.

Returns:

  • (String)

    Event name



9
10
11
# File 'lib/speed_gun/event.rb', line 9

def name
  @name
end

#payloadHash (readonly)

Returns Event payload.

Returns:

  • (Hash)

    Event payload



11
12
13
# File 'lib/speed_gun/event.rb', line 11

def payload
  @payload
end

#started_atTime (readonly)

Returns Started time of event.

Returns:

  • (Time)

    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

#durationObject



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

Returns:

  • (Boolean)


42
43
44
# File 'lib/speed_gun/event.rb', line 42

def finished?
    @finished_at
end

#roughly_finished_atObject



46
47
48
# File 'lib/speed_gun/event.rb', line 46

def roughly_finished_at
  finished_at || started_at
end

#to_hashObject



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