Class: Regent::Session
- Inherits:
-
Object
- Object
- Regent::Session
- Includes:
- Concerns::Durationable, Concerns::Identifiable
- Defined in:
- lib/regent/session.rb
Defined Under Namespace
Classes: AlreadyStartedError, InactiveSessionError, SessionError
Instance Attribute Summary collapse
-
#end_time ⇒ Object
readonly
Returns the value of attribute end_time.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#spans ⇒ Object
readonly
Returns the value of attribute spans.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Instance Method Summary collapse
-
#active? ⇒ Boolean
Whether the session is currently active.
-
#add_message(message) ⇒ Object
Adds a message to the session.
-
#complete ⇒ Object, String
Completes the session and returns the result.
-
#current_span ⇒ Span?
The current span or nil if no spans exist.
-
#exec(type, options = {}, &block) ⇒ String
Executes a new span in the session.
-
#initialize ⇒ Session
constructor
A new instance of Session.
-
#replay ⇒ String
Replays the session.
-
#result ⇒ String?
The output of the current span or nil if no spans exist.
-
#start ⇒ void
Starts the session.
Methods included from Concerns::Durationable
Methods included from Concerns::Identifiable
Constructor Details
#initialize ⇒ Session
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_time ⇒ Object (readonly)
Returns the value of attribute end_time.
21 22 23 |
# File 'lib/regent/session.rb', line 21 def end_time @end_time end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
21 22 23 |
# File 'lib/regent/session.rb', line 21 def id @id end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
21 22 23 |
# File 'lib/regent/session.rb', line 21 def @messages end |
#spans ⇒ Object (readonly)
Returns the value of attribute spans.
21 22 23 |
# File 'lib/regent/session.rb', line 21 def spans @spans end |
#start_time ⇒ Object (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.
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
80 81 82 83 84 |
# File 'lib/regent/session.rb', line 80 def () raise ArgumentError, "Message cannot be nil or empty" if .nil? || .empty? @messages << end |
#complete ⇒ Object, String
Completes the session and returns the result
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_span ⇒ Span?
Returns 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
37 38 39 40 41 42 |
# File 'lib/regent/session.rb', line 37 def exec(type, = {}, &block) raise InactiveSessionError, "Cannot execute span in inactive session" unless active? @spans << Span.new(type: type, arguments: ) current_span.run(&block) end |
#replay ⇒ String
Replays the session
46 47 48 49 |
# File 'lib/regent/session.rb', line 46 def replay spans.each { |span| span.replay } result end |
#result ⇒ String?
Returns 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 |
#start ⇒ void
This method returns an undefined value.
Starts the session
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 |