Class: Ruact::ServerFunctions::StandaloneContext

Inherits:
Object
  • Object
show all
Defined in:
lib/ruact/server_functions/standalone_context.rb

Overview

Story 8.3 — per-dispatch execution context for a standalone server action. The dispatcher allocates a fresh instance per request, ‘instance_exec`s the action block against it, and discards the instance once the response is written.

Exposes:

- `params`   — the action-call args, as `ActionController::Parameters`
  (same shape as the controller-hosted path from Story 8.1).
- `request`  — the live `ActionDispatch::Request`.
- `session`  — the host middleware's session.
- `cookies`  — the live `ActionDispatch::Cookies::CookieJar`.
- `headers`  — `request.headers`.
- `current_user` — memoized; reads `request.env['ruact.current_user']`
  when present, otherwise invokes
  {Ruact::Configuration#current_user_resolver} (a lambda taking
  `request.env`). Raises {Ruact::CurrentUserNotConfiguredError} when
  neither path yields a value AND the block actually reads it.

Does NOT expose ‘render` / `redirect_to` / `head` — those are controller-context methods. The block’s return value IS the response; raise ActionError for non-2xx returns.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params:, request:) ⇒ StandaloneContext

Returns a new instance of StandaloneContext.

Parameters:

  • params (ActionController::Parameters)

    action-call args.

  • request (ActionDispatch::Request)

    the live request.



31
32
33
34
35
36
37
# File 'lib/ruact/server_functions/standalone_context.rb', line 31

def initialize(params:, request:)
  @params = params
  @request = request
  @current_user_read = false
  @current_user_memo = nil
  @current_user_resolved = false
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



27
28
29
# File 'lib/ruact/server_functions/standalone_context.rb', line 27

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



27
28
29
# File 'lib/ruact/server_functions/standalone_context.rb', line 27

def request
  @request
end

Instance Method Details

#__ruact_current_user_read?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/ruact/server_functions/standalone_context.rb', line 74

def __ruact_current_user_read?
  @current_user_read
end

#cookiesObject



43
44
45
# File 'lib/ruact/server_functions/standalone_context.rb', line 43

def cookies
  @request.cookie_jar
end

#current_userObject

Memoized current_user accessor. Sets a flag so the dispatcher can emit a dev-only warning when a block never reads ‘current_user` (Pitfall #4 in the story spec).



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruact/server_functions/standalone_context.rb', line 54

def current_user
  @current_user_read = true
  return @current_user_memo if @current_user_resolved

  env = @request.env
  if env.key?("ruact.current_user")
    @current_user_memo = env["ruact.current_user"]
    @current_user_resolved = true
    return @current_user_memo
  end

  resolver = Ruact.config.current_user_resolver
  raise Ruact::CurrentUserNotConfiguredError unless resolver

  @current_user_memo = resolver.call(env)
  @current_user_resolved = true
  @current_user_memo
end

#head(*_args, **_kwargs) ⇒ Object

Raises:

  • (NoMethodError)


95
96
97
98
99
100
# File 'lib/ruact/server_functions/standalone_context.rb', line 95

def head(*_args, **_kwargs)
  raise NoMethodError,
        "StandaloneContext does not expose `head` — return `nil` to render " \
        "204 No Content, or raise `Ruact::ActionError.new(status:, body:)` " \
        "for other non-2xx responses."
end

#headersObject



47
48
49
# File 'lib/ruact/server_functions/standalone_context.rb', line 47

def headers
  @request.headers
end

#redirect_to(*_args, **_kwargs) ⇒ Object

Raises:

  • (NoMethodError)


88
89
90
91
92
93
# File 'lib/ruact/server_functions/standalone_context.rb', line 88

def redirect_to(*_args, **_kwargs)
  raise NoMethodError,
        "StandaloneContext does not expose `redirect_to` — return a value " \
        "from the block (it becomes the JSON response) or raise " \
        "`Ruact::ActionError.new(status:, body:)` for non-2xx responses."
end

#render(*_args, **_kwargs) ⇒ Object

Inhibits accidental controller-context calls inside a standalone block. The error message names the supported alternatives so the developer can immediately fix the call.

Raises:

  • (NoMethodError)


81
82
83
84
85
86
# File 'lib/ruact/server_functions/standalone_context.rb', line 81

def render(*_args, **_kwargs)
  raise NoMethodError,
        "StandaloneContext does not expose `render` — return a value from " \
        "the block (it becomes the JSON response) or raise " \
        "`Ruact::ActionError.new(status:, body:)` for non-2xx responses."
end

#sessionObject



39
40
41
# File 'lib/ruact/server_functions/standalone_context.rb', line 39

def session
  @request.session
end