Class: Hanami::View::Part Abstract
- Includes:
- DecoratedAttributes
- Defined in:
- lib/hanami/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.
-
#_rendering ⇒ Rendering
readonly
private
The current rendering.
-
#_value ⇒ Object
readonly
The decorated value.
Class Method Summary collapse
-
.part_name(inflector) ⇒ String
private
Determines a part name (when initialized without one).
Instance Method Summary collapse
-
#_context ⇒ Context
Returns the context object for the current rendering.
-
#_format ⇒ Symbol
Returns the template format for the current rendering.
-
#_render(partial_name, as: _name, **locals, &block) ⇒ String
Renders a new partial with the part included in its locals.
-
#_scope(scope_name = nil, **locals) ⇒ Hanami::View::Scope
Builds a new scope with the part included in its locals.
-
#initialize(value:, rendering: RenderingMissing.new, name: self.class.part_name(rendering.inflector)) ⇒ 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(value:, rendering: RenderingMissing.new, name: self.class.part_name(rendering.inflector)) ⇒ Part
Returns a new Part instance
80 81 82 83 84 85 86 87 88 |
# File 'lib/hanami/view/part.rb', line 80 def initialize( value:, rendering: RenderingMissing.new, name: self.class.part_name(rendering.inflector) ) @_name = name @_value = value @_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. If the ‘_value` responds to the method, then the method will be sent to the value.
213 214 215 216 217 218 219 220 221 |
# File 'lib/hanami/view/part.rb', line 213 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.
37 38 39 |
# File 'lib/hanami/view/part.rb', line 37 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.
The current rendering
59 60 61 |
# File 'lib/hanami/view/part.rb', line 59 def _rendering @_rendering end |
Class Method Details
.part_name(inflector) ⇒ String
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.
Determines a part name (when initialized without one). Intended for internal use only while unit testing Parts.
68 69 70 |
# File 'lib/hanami/view/part.rb', line 68 def self.part_name(inflector) name ? inflector.underscore(inflector.demodulize(name)) : "part" end |
Instance Method Details
#_context ⇒ Context #context ⇒ Context
Returns the context object for the current rendering.
118 119 120 |
# File 'lib/hanami/view/part.rb', line 118 def _context _rendering.context end |
#_format ⇒ Symbol #format ⇒ Symbol
Returns the template format for the current rendering.
102 103 104 |
# File 'lib/hanami/view/part.rb', line 102 def _format _rendering.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.
139 140 141 |
# File 'lib/hanami/view/part.rb', line 139 def _render(partial_name, as: _name, **locals, &block) _rendering.partial(partial_name, _rendering.scope({as => self}.merge(locals)), &block) end |
#_scope(scope_name = nil, **locals) ⇒ Hanami::View::Scope #scope(scope_name = nil, **locals) ⇒ Hanami::View::Scope
Builds a new scope with the part included in its locals.
159 160 161 |
# File 'lib/hanami/view/part.rb', line 159 def _scope(scope_name = nil, **locals) _rendering.scope(scope_name, {_name => self}.merge(locals)) end |
#inspect ⇒ String
Returns a string representation of the part.
205 206 207 |
# File 'lib/hanami/view/part.rb', line 205 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 rendering.
However, using ‘.decorate` is preferred for declaring attributes that should also be decorated as parts.
190 191 192 193 194 195 196 197 |
# File 'lib/hanami/view/part.rb', line 190 def new(klass = self.class, name: _name, value: _value, **) klass.new( name: name, value: value, rendering: _rendering, ** ) end |
#to_s ⇒ String
Returns a string representation of the value.
169 170 171 |
# File 'lib/hanami/view/part.rb', line 169 def to_s _value.to_s end |