Module: Hanami::Extensions::View::Context::ClassExtension::InstanceMethods
- Defined in:
- lib/hanami/extensions/view/context.rb
Overview
Instance Attribute Summary collapse
-
#inflector ⇒ Dry::Inflector
readonly
Returns the app’s inflector.
-
#settings ⇒ Hanami::Settings
readonly
Returns the app’s settings.
Instance Method Summary collapse
-
#assets ⇒ Hanami::Assets
Returns the app’s assets.
- #content_for(key, value = nil) ⇒ Object
-
#csrf_token ⇒ String
Returns the current request’s CSRF token.
-
#flash ⇒ Object
Returns the flash hash for the current request.
- #initialize(inflector: nil, settings: nil, routes: nil, assets: nil, request: nil, **args) ⇒ Object private
- #initialize_copy(source) ⇒ Object private
-
#request ⇒ Hanami::Action::Request
Returns the current request, if the view is rendered from within an action.
-
#routes ⇒ Hanami::Slice::RoutesHelper
Returns the app’s routes helper.
-
#session ⇒ Rack::Session::Abstract::SessionHash
Returns the session for the current request.
Instance Attribute Details
#inflector ⇒ Dry::Inflector (readonly)
Returns the app’s inflector.
91 92 93 |
# File 'lib/hanami/extensions/view/context.rb', line 91 def inflector @inflector end |
#settings ⇒ Hanami::Settings (readonly)
Returns the app’s settings.
99 100 101 |
# File 'lib/hanami/extensions/view/context.rb', line 99 def settings @settings end |
Instance Method Details
#assets ⇒ Hanami::Assets
Returns the app’s assets.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/hanami/extensions/view/context.rb', line 144 def assets unless @assets msg = if Hanami.bundled?("hanami-assets") "Have you put files into your assets directory?" else "The hanami-assets gem is required to access assets." end raise Hanami::ComponentLoadError, "Assets not available. #{msg}" end @assets end |
#content_for(key, value = nil, &block) ⇒ String #content_for(key) ⇒ String?
218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/hanami/extensions/view/context.rb', line 218 def content_for(key, value = nil) if block_given? @content_for[key] = yield nil elsif value @content_for[key] = value nil else @content_for[key] end end |
#csrf_token ⇒ String
Returns the current request’s CSRF token.
239 240 241 |
# File 'lib/hanami/extensions/view/context.rb', line 239 def csrf_token request.session[Hanami::Action::CSRFProtection::CSRF_TOKEN] end |
#flash ⇒ Object
Returns the flash hash for the current request.
265 266 267 |
# File 'lib/hanami/extensions/view/context.rb', line 265 def flash request.flash end |
#initialize(inflector: nil, settings: nil, routes: nil, assets: nil, request: nil, **args) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/hanami/extensions/view/context.rb', line 105 def initialize( # rubocop:disable Metrics/ParameterLists inflector: nil, settings: nil, routes: nil, assets: nil, request: nil, **args ) @inflector = inflector @settings = settings @routes = routes @assets = assets @request = request @content_for = {} super(**args) end |
#initialize_copy(source) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
126 127 128 129 130 131 132 133 134 |
# File 'lib/hanami/extensions/view/context.rb', line 126 def initialize_copy(source) # The standard implementation of initialize_copy will make shallow copies of all # instance variables from the source. This is fine for most of our ivars. super # Dup any objects that will be mutated over a given rendering to ensure no leakage of # state across distinct view renderings. @content_for = source.instance_variable_get(:@content_for).dup end |
#request ⇒ Hanami::Action::Request
Returns the current request, if the view is rendered from within an action.
167 168 169 170 171 172 173 |
# File 'lib/hanami/extensions/view/context.rb', line 167 def request unless @request raise Hanami::ComponentLoadError, "Request not available. Only views rendered from Hanami::Action instances have a request." end @request end |
#routes ⇒ Hanami::Slice::RoutesHelper
Returns the app’s routes helper.
184 185 186 187 188 189 190 |
# File 'lib/hanami/extensions/view/context.rb', line 184 def routes unless @routes raise Hanami::ComponentLoadError, "the hanami-router gem is required to access routes" end @routes end |
#session ⇒ Rack::Session::Abstract::SessionHash
Returns the session for the current request.
252 253 254 |
# File 'lib/hanami/extensions/view/context.rb', line 252 def session request.session end |