Class: Wurk::Web::Extension::Action
- Inherits:
-
Object
- Object
- Wurk::Web::Extension::Action
- Includes:
- Helpers
- Defined in:
- lib/wurk/web/extension.rb
Overview
Per-request render context: instance-evals a matched route block, then renders ERB with the block’s ivars + helpers in scope.
Defined Under Namespace
Classes: Redirect
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Instance Method Summary collapse
-
#erb(content, _options = {}) ⇒ Object
Render an ERB template.
-
#initialize(env:, route_params:, ext:, mount:, embed: true) ⇒ Action
constructor
A new instance of Action.
- #logger ⇒ Object
- #params ⇒ Object
- #redirect(location) ⇒ Object
- #route_params(key = nil) ⇒ Object
-
#run(block) ⇒ Object
Run the route block in this context, capturing redirects.
- #session ⇒ Object
- #url_params(key) ⇒ Object
Methods included from Helpers
#asset_path, #csp_nonce, #csrf_tag, #current_path, #h, #number_with_delimiter, #product_version, #redis, #relative_time, #root_path, #t, #to_display, #truncate
Constructor Details
#initialize(env:, route_params:, ext:, mount:, embed: true) ⇒ Action
Returns a new instance of Action.
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/wurk/web/extension.rb', line 164 def initialize(env:, route_params:, ext:, mount:, embed: true) @env = env @request = ::Rack::Request.new(env) @route_params = route_params @ext_name = ext[:name].to_s @root_dir = ext[:root_dir] @ext_strings = ext[:strings] @mount = mount.to_s @embed = @subpath = env['wurk.ext.subpath'] extend_helpers(ext[:helpers]) end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
177 178 179 |
# File 'lib/wurk/web/extension.rb', line 177 def env @env end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
177 178 179 |
# File 'lib/wurk/web/extension.rb', line 177 def request @request end |
Instance Method Details
#erb(content, _options = {}) ⇒ Object
Render an ERB template. ‘content` is either the template String (the common path — the ext’s own helper reads the file) or a Symbol naming a ‘*.erb` under the ext’s root_dir/views.
189 190 191 192 |
# File 'lib/wurk/web/extension.rb', line 189 def erb(content, = {}) template = content.is_a?(::Symbol) ? read_view(content) : content.to_s ::ERB.new(template, trim_mode: '-').result(binding) end |
#params ⇒ Object
179 |
# File 'lib/wurk/web/extension.rb', line 179 def params = @params ||= symbolize(@request.params).merge(@route_params) |
#redirect(location) ⇒ Object
184 |
# File 'lib/wurk/web/extension.rb', line 184 def redirect(location) = throw(:wurk_ext_halt, Redirect.new(location)) |
#route_params(key = nil) ⇒ Object
181 |
# File 'lib/wurk/web/extension.rb', line 181 def route_params(key = nil) = key ? @route_params[key.to_sym] : @route_params |
#run(block) ⇒ Object
Run the route block in this context, capturing redirects. Returns the rendered HTML String, or a Redirect.
196 197 198 |
# File 'lib/wurk/web/extension.rb', line 196 def run(block) catch(:wurk_ext_halt) { instance_exec(&block) } end |
#session ⇒ Object
182 |
# File 'lib/wurk/web/extension.rb', line 182 def session = (@env['rack.session'] ||= {}) |
#url_params(key) ⇒ Object
180 |
# File 'lib/wurk/web/extension.rb', line 180 def url_params(key) = @request.params[key.to_s] |