Class: Dry::View::Context Abstract
- Inherits:
-
Object
- Object
- Dry::View::Context
- Includes:
- DecoratedAttributes
- Defined in:
- lib/dry/view/context.rb
Overview
Subclass this and add your own methods (along with a custom ‘#initialize` if you wish to inject dependencies)
Provides a baseline environment across all the templates, parts and scopes in a given rendering.
Instance Attribute Summary collapse
- #_options ⇒ Object readonly
- #_render_env ⇒ Object readonly
Instance Method Summary collapse
- #for_render_env(render_env) ⇒ Object private
-
#initialize(render_env: nil, **options) ⇒ Context
constructor
Returns a new instance of Context.
-
#with(**new_options) ⇒ Object
Returns a copy of the Context with new options merged in.
Methods included from DecoratedAttributes
Constructor Details
#initialize(render_env: nil, **options) ⇒ Context
Returns a new instance of Context
In subclasses, you should include an ‘**options` parameter and pass _all arguments_ to `super`. This allows Context to make copies of itself while preserving your dependencies.
39 40 41 42 |
# File 'lib/dry/view/context.rb', line 39 def initialize(render_env: nil, **) @_render_env = render_env @_options = end |
Instance Attribute Details
#_options ⇒ Object (readonly)
19 20 21 |
# File 'lib/dry/view/context.rb', line 19 def @_options end |
#_render_env ⇒ Object (readonly)
19 20 21 |
# File 'lib/dry/view/context.rb', line 19 def _render_env @_render_env end |
Instance Method Details
#for_render_env(render_env) ⇒ 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.
45 46 47 48 49 |
# File 'lib/dry/view/context.rb', line 45 def for_render_env(render_env) return self if render_env == _render_env self.class.new(**.merge(render_env: render_env)) end |
#with(**new_options) ⇒ Object
Returns a copy of the Context with new options merged in.
This may be useful to supply values for dependencies that are optional when initializing your custom Context subclass.
72 73 74 75 76 77 |
# File 'lib/dry/view/context.rb', line 72 def with(**) self.class.new( render_env: _render_env, **.merge() ) end |