Class: Hanami::View::Scope Abstract
Overview
Subclass this and provide your own methods adding view-specific behavior. You should not override ‘#initialize`
Evaluation context for templates (including layouts and partials) and provides a place to encapsulate view-specific behaviour alongside a template and its locals.
Constant Summary collapse
- CONVENIENCE_METHODS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%i[format context locals].freeze
Instance Attribute Summary collapse
-
#_locals ⇒ Hash[<Symbol, Object>]
readonly
Returns the scope’s locals.
-
#_name ⇒ Symbol
readonly
Returns the scope’s name.
-
#_rendering ⇒ Rendering
readonly
private
Returns the current rendering.
Instance Method Summary collapse
-
#_context ⇒ Context
Returns the context object for the current render environment.
-
#_format ⇒ Symbol
Returns the template format for the current render environment.
-
#initialize(name: nil, locals: Dry::Core::Constants::EMPTY_HASH, rendering: RenderingMissing.new) ⇒ Scope
constructor
Returns a new Scope instance.
-
#render(partial_name = nil, **locals, &block) ⇒ String
The rendered partial output.
-
#scope(name = nil, **locals) ⇒ Scope
Builds a new scope using a scope class matching the provided name.
Constructor Details
#initialize(name: nil, locals: Dry::Core::Constants::EMPTY_HASH, rendering: RenderingMissing.new) ⇒ Scope
Returns a new Scope instance.
65 66 67 68 69 70 71 72 73 |
# File 'lib/hanami/view/scope.rb', line 65 def initialize( name: nil, locals: Dry::Core::Constants::EMPTY_HASH, rendering: RenderingMissing.new ) @_name = name @_locals = locals @_rendering = rendering end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
Handles missing methods, according to the following rules:
-
If there is a local with a name matching the method, it returns the local.
-
If the ‘context` responds to the method, then it will be sent the method and all its arguments.
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/hanami/view/scope.rb', line 158 def method_missing(name, *args, &block) if _locals.key?(name) _locals[name] elsif _context.respond_to?(name) _context.public_send(name, *args, &block) elsif CONVENIENCE_METHODS.include?(name) __send__(:"_#{name}", *args, &block) else super end end |
Instance Attribute Details
#_locals ⇒ Hash[<Symbol, Object>] (readonly) #locals ⇒ Hash[<Symbol, Object>] (readonly)
Returns the scope’s locals
45 46 47 |
# File 'lib/hanami/view/scope.rb', line 45 def _locals @_locals end |
#_name ⇒ Symbol (readonly)
Returns the scope’s name.
32 33 34 |
# File 'lib/hanami/view/scope.rb', line 32 def _name @_name end |
#_rendering ⇒ Rendering (readonly)
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.
Returns the current rendering.
53 54 55 |
# File 'lib/hanami/view/scope.rb', line 53 def _rendering @_rendering end |
Instance Method Details
#_context ⇒ Context #context ⇒ Context
Returns the context object for the current render environment.
147 148 149 |
# File 'lib/hanami/view/scope.rb', line 147 def _context _rendering.context end |
#_format ⇒ Symbol #format ⇒ Symbol
Returns the template format for the current render environment.
131 132 133 |
# File 'lib/hanami/view/scope.rb', line 131 def _format _rendering.format end |
#render(partial_name, **locals, &block) ⇒ String #render(**locals, &block) ⇒ String
Returns the rendered partial output.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/hanami/view/scope.rb', line 92 def render(partial_name = nil, **locals, &block) partial_name ||= _name unless partial_name raise ArgumentError, "+partial_name+ must be provided for unnamed scopes" end if partial_name.is_a?(Class) partial_name = _inflector.underscore(_inflector.demodulize(partial_name.to_s)) end _rendering.partial(partial_name, _render_scope(**locals), &block) end |
#scope(name = nil, **locals) ⇒ Scope
Builds a new scope using a scope class matching the provided name.
115 116 117 |
# File 'lib/hanami/view/scope.rb', line 115 def scope(name = nil, **locals) _rendering.scope(name, locals) end |