Class: Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/yuslow/operation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object:, method:, parent: nil, started_at: Time.now, completed_at: nil, children: []) ⇒ Operation

Returns a new instance of Operation.



10
11
12
13
14
15
16
17
# File 'lib/yuslow/operation.rb', line 10

def initialize(object:, method:, parent: nil, started_at: Time.now, completed_at: nil, children: [])
  @object = object
  @method = method
  @started_at = started_at
  @completed_at = completed_at
  @parent = parent
  @children = children
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



2
3
4
# File 'lib/yuslow/operation.rb', line 2

def children
  @children
end

#parentObject (readonly)

Returns the value of attribute parent.



2
3
4
# File 'lib/yuslow/operation.rb', line 2

def parent
  @parent
end

Class Method Details

.start(object, method) ⇒ Object



5
6
7
# File 'lib/yuslow/operation.rb', line 5

def start(object, method)
  new(object, method)
end

Instance Method Details

#completeObject



30
31
32
33
34
# File 'lib/yuslow/operation.rb', line 30

def complete
  raise 'This operation is already completed' if @completed_at

  @completed_at = Time.now
end

#elapsedObject



36
37
38
# File 'lib/yuslow/operation.rb', line 36

def elapsed
  ((@completed_at - @started_at) * 1000).round if @completed_at
end

#fork(object:, method:) ⇒ Object



19
20
21
22
23
24
# File 'lib/yuslow/operation.rb', line 19

def fork(object:, method:)
  operation = self.class.new object: object, method: method, parent: self
  @children << operation

  operation
end

#identifierObject



26
27
28
# File 'lib/yuslow/operation.rb', line 26

def identifier
  "#{@object}##{@method}"
end