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 |
# 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
contextresponds to the method, then it will be sent the method and all its arguments.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/dry/view/scope.rb', line 142 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
130 131 132 |
# File 'lib/dry/view/scope.rb', line 130 def _context _render_env.context end |
#_format ⇒ Symbol #format ⇒ Symbol
The template format for the current render environment.
115 116 117 |
# File 'lib/dry/view/scope.rb', line 115 def _format _render_env.format end |
#render(partial_name, **locals, &block) ⇒ String #render(**locals, &block) ⇒ String
Returns the rendered partial output.
84 85 86 87 88 89 90 |
# File 'lib/dry/view/scope.rb', line 84 def render(partial_name = nil, **locals, &block) partial_name ||= _name raise ArgumentError, "+partial_name+ must be provided for unnamed scopes" unless partial_name partial_name = _inflector.underscore(_inflector.demodulize(partial_name.to_s)) if partial_name.is_a?(Class) _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
100 101 102 |
# File 'lib/dry/view/scope.rb', line 100 def scope(name = nil, **locals) _render_env.scope(name, locals) end |