Class: Regent::Session

Inherits:
Object
  • Object
show all
Includes:
Concerns::Durationable, Concerns::Identifiable
Defined in:
lib/regent/session.rb

Defined Under Namespace

Classes: AlreadyStartedError, InactiveSessionError, SessionError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Durationable

#duration

Methods included from Concerns::Identifiable

included

Constructor Details

#initializeSession

Returns a new instance of Session.



12
13
14
15
16
17
18
19
# File 'lib/regent/session.rb', line 12

def initialize
  super()

  @spans = []
  @messages = []
  @start_time = nil
  @end_time = nil
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



21
22
23
# File 'lib/regent/session.rb', line 21

def end_time
  @end_time
end

#idObject (readonly)

Returns the value of attribute id.



21
22
23
# File 'lib/regent/session.rb', line 21

def id
  @id
end

#messagesObject (readonly)

Returns the value of attribute messages.



21
22
23
# File 'lib/regent/session.rb', line 21

def messages
  @messages
end

#spansObject (readonly)

Returns the value of attribute spans.



21
22
23
# File 'lib/regent/session.rb', line 21

def spans
  @spans
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



21
22
23
# File 'lib/regent/session.rb', line 21

def start_time
  @start_time
end

Instance Method Details

#active?Boolean

Returns Whether the session is currently active.

Returns:

  • (Boolean)

    Whether the session is currently active



73
74
75
# File 'lib/regent/session.rb', line 73

def active?
  start_time && end_time.nil?
end

#add_message(message) ⇒ Object

Adds a message to the session

Parameters:

  • message (String)

    The message to add

Raises:

  • (ArgumentError)

    if message is nil or empty



80
81
82
83
84
# File 'lib/regent/session.rb', line 80

def add_message(message)
  raise ArgumentError, "Message cannot be nil or empty" if message.nil? || message.empty?

  @messages << message
end

#completeObject, String

Completes the session and returns the result

Returns:

  • (Object)

    The result of the last span

  • (String)

    The result of the last span

Raises:



55
56
57
58
59
60
# File 'lib/regent/session.rb', line 55

def complete
  raise InactiveSessionError, "Cannot complete inactive session" unless active?

  @end_time = Time.now.freeze
  result
end

#current_spanSpan?

Returns The current span or nil if no spans exist.

Returns:

  • (Span, nil)

    The current span or nil if no spans exist



63
64
65
# File 'lib/regent/session.rb', line 63

def current_span
  @spans.last
end

#exec(type, options = {}, &block) ⇒ String

Executes a new span in the session

Parameters:

  • type (Symbol, String)

    The type of span

  • options (Hash) (defaults to: {})

    Options for the span

Returns:

  • (String)

    The output of the span

Raises:



37
38
39
40
41
42
# File 'lib/regent/session.rb', line 37

def exec(type, options = {}, &block)
  raise InactiveSessionError, "Cannot execute span in inactive session" unless active?

  @spans << Span.new(type: type, arguments: options)
  current_span.run(&block)
end

#replayString

Replays the session

Returns:

  • (String)

    The result of the session



46
47
48
49
# File 'lib/regent/session.rb', line 46

def replay
  spans.each { |span| span.replay }
  result
end

#resultString?

Returns The output of the current span or nil if no spans exist.

Returns:

  • (String, nil)

    The output of the current span or nil if no spans exist



68
69
70
# File 'lib/regent/session.rb', line 68

def result
  current_span&.output
end

#startvoid

This method returns an undefined value.

Starts the session

Raises:



26
27
28
29
30
# File 'lib/regent/session.rb', line 26

def start
  raise AlreadyStartedError, "Session already started" if @start_time

  @start_time = Time.now.freeze
end