Class: Hanami::View::Rendering::LayoutScope
- Inherits:
- Utils::BasicObject
- Defined in:
- lib/hanami/view/rendering/layout_scope.rb
Overview
Scope for layout rendering
Direct Known Subclasses
Instance Method Summary collapse
-
#format ⇒ Symbol
Returns the requested format.
-
#initialize(layout, scope) ⇒ LayoutScope
constructor
private
Initialize the scope.
-
#local(key) ⇒ Object, Hanami::View::Rendering::NullLocal
It tries to invoke a method for the view or a local for the given key.
-
#locals ⇒ Hash
The current locals.
-
#render(options, &block) ⇒ String
Render a partial or a template within a layout template.
-
#respond_to?(m, include_all = false) ⇒ TrueClass, FalseClass
private
Implements “respond to” logic.
-
#respond_to_missing?(m, include_all) ⇒ TrueClass, FalseClass
private
Implements “respond to” logic.
-
#view ⇒ Hanami::View
The current view.
Constructor Details
#initialize(layout, scope) ⇒ LayoutScope
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.
Initialize the scope
29 30 31 32 33 34 |
# File 'lib/hanami/view/rendering/layout_scope.rb', line 29 def initialize(layout, scope) @layout = layout @scope = scope @view = nil @locals = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &blk) ⇒ Object (protected)
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.
Forward all the missing methods to the view scope or to the layout.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/hanami/view/rendering/layout_scope.rb', line 222 def method_missing(m, *args, &blk) # FIXME: this isn't compatible with Hanami 2.0, as it extends a view # that we want to be frozen in the future # # See https://github.com/hanami/view/issues/130#issuecomment-319326236 if @scope.respond_to?(m, true) && @scope.locals.has_key?(m) && layout.respond_to?(m, true) layout.__send__(m, *args, &blk) elsif @scope.respond_to?(m, true) @scope.__send__(m, *args, &blk) elsif layout.respond_to?(m, true) layout.__send__(m, *args, &blk) else ::Hanami::View::Escape.html(super) end end |
Instance Method Details
#format ⇒ Symbol
Returns the requested format.
104 105 106 |
# File 'lib/hanami/view/rendering/layout_scope.rb', line 104 def format @scope.format end |
#local(key) ⇒ Object, Hanami::View::Rendering::NullLocal
It tries to invoke a method for the view or a local for the given key. If the lookup fails, it returns a null object.
170 171 172 173 174 175 176 |
# File 'lib/hanami/view/rendering/layout_scope.rb', line 170 def local(key) if respond_to?(key) __send__(key) else locals.fetch(key) { NullLocal.new(key) } end end |
#locals ⇒ Hash
The current locals.
122 123 124 |
# File 'lib/hanami/view/rendering/layout_scope.rb', line 122 def locals (@locals || @scope.locals).dup end |
#render(options, &block) ⇒ String
Render a partial or a template within a layout template.
95 96 97 |
# File 'lib/hanami/view/rendering/layout_scope.rb', line 95 def render(, &block) renderer().render(&block) end |
#respond_to?(m, include_all = false) ⇒ TrueClass, FalseClass
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.
Implements “respond to” logic
186 187 188 |
# File 'lib/hanami/view/rendering/layout_scope.rb', line 186 def respond_to?(m, include_all = false) respond_to_missing?(m, include_all) end |
#respond_to_missing?(m, include_all) ⇒ TrueClass, FalseClass
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.
Implements “respond to” logic
198 199 200 201 |
# File 'lib/hanami/view/rendering/layout_scope.rb', line 198 def respond_to_missing?(m, include_all) @layout.respond_to?(m, include_all) || @scope.respond_to?(m, include_all) end |
#view ⇒ Hanami::View
The current view.
113 114 115 |
# File 'lib/hanami/view/rendering/layout_scope.rb', line 113 def view @view || @scope.view end |