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(rendering: RenderingMissing.new, name: self.class.part_name(rendering.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(rendering: RenderingMissing.new, name: self.class.part_name(rendering.inflector), value:) ⇒ 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( rendering: RenderingMissing.new, name: self.class.part_name(rendering.inflector), value: ) @_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.
216 217 218 219 220 221 222 223 224 |
# File 'lib/hanami/view/part.rb', line 216 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.
141 142 143 |
# File 'lib/hanami/view/part.rb', line 141 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.
162 163 164 |
# File 'lib/hanami/view/part.rb', line 162 def _scope(scope_name = nil, **locals) _rendering.scope(scope_name, {_name => self}.merge(locals)) end |
#inspect ⇒ String
Returns a string representation of the part.
208 209 210 |
# File 'lib/hanami/view/part.rb', line 208 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.
193 194 195 196 197 198 199 200 |
# File 'lib/hanami/view/part.rb', line 193 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.
172 173 174 |
# File 'lib/hanami/view/part.rb', line 172 def to_s _value.to_s end |