Module: Charming::Controller::SessionState
- Included in:
- Charming::Controller
- Defined in:
- lib/charming/controller/session_state.rb
Overview
Session-state helpers mixed into Controller: accessing the application session hash, lazy state-object lookup by name/class, form builder invocation, and async task submission.
Instance Method Summary collapse
-
#form(name, &block) ⇒ Object
Builds a Form component scoped to the named form slot in βsession`.
-
#run_task(name, &block) ⇒ Object
Submits a background task with the given name.
-
#session ⇒ Object
Returns the application session hash for this controller.
-
#state(name, state_class, **attributes) ⇒ Object
Returns the named session-backed state object, creating it on first access.
Instance Method Details
#form(name, &block) ⇒ Object
Builds a Form component scoped to the named form slot in βsession`. The block is evaluated against a Form::Builder (or invoked with the builder as its argument for arity-1 blocks) and returns a Form component pre-bound to the per-form mutable state hash.
25 26 27 28 29 30 31 |
# File 'lib/charming/controller/session_state.rb', line 25 def form(name, &block) session[:forms] ||= {} form_state = session[:forms][name.to_sym] ||= {} builder = Presentation::Components::Form::Builder.new(theme: theme) block.arity.zero? ? builder.instance_eval(&block) : block.call(builder) builder.build(state: form_state, theme: theme) end |
#run_task(name, &block) ⇒ Object
Submits a background task with the given name. The block is executed by the configured task executor; its return value (or any raised exception) is delivered to the controller as a TaskEvent dispatched to the matching βon_task` handler.
36 37 38 |
# File 'lib/charming/controller/session_state.rb', line 36 def run_task(name, &block) application.task_executor.submit(name, &block) end |
#session ⇒ Object
Returns the application session hash for this controller. All persistent state (focus, sidebar index, command palette, user state objects) lives here.
10 11 12 |
# File 'lib/charming/controller/session_state.rb', line 10 def session application.session end |
#state(name, state_class, **attributes) ⇒ Object
Returns the named session-backed state object, creating it on first access. name is a symbol key under βsession`. state_class is an ApplicationState subclass whose constructor receives attributes on first creation. Subsequent calls return the same object.
17 18 19 20 |
# File 'lib/charming/controller/session_state.rb', line 17 def state(name, state_class, **attributes) session[:states] ||= {} session[:states][name.to_sym] ||= state_class.new(**attributes) end |