Class: Dry::View::Part Abstract
- Inherits:
-
Object
- Object
- Dry::View::Part
- Includes:
- DecoratedAttributes
- Defined in:
- lib/dry/view/part.rb
Overview
Subclass this and provide your own methods adding view-specific behavior. You should not override ‘#initialize`.
Decorates an exposure value and provides a place to encapsulate view-specific behavior alongside your application’s domain objects.
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 render scope value ].freeze
Instance Attribute Summary collapse
-
#_name ⇒ Symbol
readonly
The part’s name.
-
#_render_env ⇒ RenderEnvironment
readonly
private
The current render environment.
-
#_value ⇒ Object
readonly
The decorated value.
Class Method Summary collapse
-
.part_name(inflector) ⇒ Object
private
Determins a part name (when initialized without one).
Instance Method Summary collapse
-
#_context ⇒ Context
The context object for the current render environment.
-
#_format ⇒ Symbol
The template format for the current render environment.
-
#_render(partial_name, as: _name, **locals, &block) ⇒ String
Renders a new partial with the part included in its locals.
-
#_scope(scope_name = nil, **locals) ⇒ Dry::View::Scope
Builds a new scope with the part included in its locals.
-
#initialize(render_env: RenderEnvironmentMissing.new, name: self.class.part_name(render_env.inflector), value:) ⇒ Part
constructor
Returns a new Part instance.
-
#inspect ⇒ String
Returns a string representation of the part.
-
#new(klass = self.class, name: _name, value: _value, **options) ⇒ Object
Builds a new a part with the given parameters.
-
#to_s ⇒ String
Returns a string representation of the value.
Methods included from DecoratedAttributes
Constructor Details
#initialize(render_env: RenderEnvironmentMissing.new, name: self.class.part_name(render_env.inflector), value:) ⇒ Part
Returns a new Part instance
73 74 75 76 77 78 79 80 81 |
# File 'lib/dry/view/part.rb', line 73 def initialize( render_env: RenderEnvironmentMissing.new, name: self.class.part_name(render_env.inflector), value: ) @_name = name @_value = value @_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. If the ‘_value` responds to the method, then the method will be sent to the value.
201 202 203 204 205 206 207 208 209 |
# File 'lib/dry/view/part.rb', line 201 def method_missing(name, *args, &block) if _value.respond_to?(name) _value.public_send(name, *args, &block) elsif CONVENIENCE_METHODS.include?(name) __send__(:"_#{name}", *args, &block) else super end end |
Instance Attribute Details
#_name ⇒ Symbol (readonly)
The part’s name. This comes from the exposure supplying the value.
36 37 38 |
# File 'lib/dry/view/part.rb', line 36 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
56 57 58 |
# File 'lib/dry/view/part.rb', line 56 def _render_env @_render_env end |
#_value ⇒ Object (readonly) #value ⇒ Object (readonly)
The decorated value. This is the value returned from the exposure.
49 50 51 |
# File 'lib/dry/view/part.rb', line 49 def _value @_value end |
Class Method Details
.part_name(inflector) ⇒ 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.
Determins a part name (when initialized without one). Intended for use only while unit testing Parts.
62 63 64 |
# File 'lib/dry/view/part.rb', line 62 def self.part_name(inflector) name ? inflector.underscore(inflector.demodulize(name)) : "part" end |
Instance Method Details
#_context ⇒ Context #context ⇒ Context
The context object for the current render environment
109 110 111 |
# File 'lib/dry/view/part.rb', line 109 def _context _render_env.context end |
#_format ⇒ Symbol #format ⇒ Symbol
The template format for the current render environment.
94 95 96 |
# File 'lib/dry/view/part.rb', line 94 def _format _render_env.format end |
#_render(partial_name, as: _name, **locals, &block) ⇒ String #render(partial_name, as: _name, **locals, &block) ⇒ String
Renders a new partial with the part included in its locals.
rubocop:disable Naming/UncommunicativeMethodParamName
130 131 132 |
# File 'lib/dry/view/part.rb', line 130 def _render(partial_name, as: _name, **locals, &block) _render_env.partial(partial_name, _render_env.scope({as => self}.merge(locals)), &block) end |
#_scope(scope_name = nil, **locals) ⇒ Dry::View::Scope #scope(scope_name = nil, **locals) ⇒ Dry::View::Scope
Builds a new scope with the part included in its locals.
150 151 152 |
# File 'lib/dry/view/part.rb', line 150 def _scope(scope_name = nil, **locals) _render_env.scope(scope_name, {_name => self}.merge(locals)) end |
#inspect ⇒ String
Returns a string representation of the part
193 194 195 |
# File 'lib/dry/view/part.rb', line 193 def inspect %(#<#{self.class.name} name=#{_name.inspect} value=#{_value.inspect}>) end |
#new(klass = self.class, name: _name, value: _value, **options) ⇒ Object
Builds a new a part with the given parameters
This is helpful for manually constructing a new part object that maintains the current render environment.
However, using ‘.decorate` is preferred for declaring attributes that should also be decorated as parts.
179 180 181 182 183 184 185 186 |
# File 'lib/dry/view/part.rb', line 179 def new(klass = self.class, name: _name, value: _value, **) klass.new( name: name, value: value, render_env: _render_env, ** ) end |
#to_s ⇒ String
Returns a string representation of the value
159 160 161 |
# File 'lib/dry/view/part.rb', line 159 def to_s _value.to_s end |