Class: Dry::View::Scope Abstract
- Inherits:
-
Object
- Object
- Dry::View::Scope
- Defined in:
- lib/dry/view/scope.rb
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
The scope’s locals.
-
#_name ⇒ Symbol
readonly
The scope’s name.
-
#_render_env ⇒ RenderEnvironment
readonly
private
The current render environment.
Instance Method Summary collapse
-
#_context ⇒ Context
The context object for the current render environment.
-
#_format ⇒ Symbol
The template format for the current render environment.
-
#initialize(name: nil, locals: Dry::Core::Constants::EMPTY_HASH, render_env: RenderEnvironmentMissing.new) ⇒ Scope
constructor
Returns a new Scope instance.
-
#render(partial_name = nil, **locals, &block) ⇒ String
The rendered partial output.
-
#scope(name = nil, **locals) ⇒ Scope
Build a new scope using a scope class matching the provided name.
Constructor Details
#initialize(name: nil, locals: Dry::Core::Constants::EMPTY_HASH, render_env: RenderEnvironmentMissing.new) ⇒ Scope
Returns a new Scope instance
62 63 64 65 66 67 68 69 70 |
# File 'lib/dry/view/scope.rb', line 62 def initialize( name: nil, locals: Dry::Core::Constants::EMPTY_HASH, render_env: RenderEnvironmentMissing.new ) @_name = name @_locals = locals @_render_env = render_env 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.
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/dry/view/scope.rb', line 152 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)
The scope’s locals
44 45 46 |
# File 'lib/dry/view/scope.rb', line 44 def _locals @_locals end |
#_name ⇒ Symbol (readonly)
The scope’s name
31 32 33 |
# File 'lib/dry/view/scope.rb', line 31 def _name @_name end |
#_render_env ⇒ RenderEnvironment (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.
The current render environment
51 52 53 |
# File 'lib/dry/view/scope.rb', line 51 def _render_env @_render_env end |
Instance Method Details
#_context ⇒ Context #context ⇒ Context
The context object for the current render environment
140 141 142 |
# File 'lib/dry/view/scope.rb', line 140 def _context _render_env.context end |
#_format ⇒ Symbol #format ⇒ Symbol
The template format for the current render environment.
125 126 127 |
# File 'lib/dry/view/scope.rb', line 125 def _format _render_env.format end |
#render(partial_name, **locals, &block) ⇒ String #render(**locals, &block) ⇒ String
Returns the rendered partial output.
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/dry/view/scope.rb', line 88 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 _render_env.partial(partial_name, _render_scope(**locals), &block) end |
#scope(name = nil, **locals) ⇒ Scope
Build a new scope using a scope class matching the provided name
110 111 112 |
# File 'lib/dry/view/scope.rb', line 110 def scope(name = nil, **locals) _render_env.scope(name, locals) end |