Class: TalkToYourApp::Tool::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/talk_to_your_app/tool.rb

Overview

The context handed to ‘#call`. Exposes the request principal and session, the audit logger, and role-switched database connections.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tool_class:, logger: nil) ⇒ Context

Returns a new instance of Context.



35
36
37
38
# File 'lib/talk_to_your_app/tool.rb', line 35

def initialize(tool_class:, logger: nil)
  @tool_class = tool_class
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



33
34
35
# File 'lib/talk_to_your_app/tool.rb', line 33

def logger
  @logger
end

Instance Method Details

#connection(name = nil, &block) ⇒ Object

Runs the block against a registry connection, switched to its declared role. With no name, uses the tool’s declared ‘connection`.



50
51
52
53
54
55
56
57
# File 'lib/talk_to_your_app/tool.rb', line 50

def connection(name = nil, &block)
  name ||= @tool_class.connection
  if name.nil?
    raise TalkToYourApp::ConfigurationError,
      "tool #{@tool_class.tool_name.inspect} called ctx.connection without a name and declares no `connection`."
  end
  TalkToYourApp::ConnectionRegistry.with(name, &block)
end

#principalObject



40
41
42
# File 'lib/talk_to_your_app/tool.rb', line 40

def principal
  TalkToYourApp::Current.principal
end

#session_idObject



44
45
46
# File 'lib/talk_to_your_app/tool.rb', line 44

def session_id
  TalkToYourApp::Current.session_id
end