Class: Hanami::View::Part Abstract

Inherits:
Object show all
Includes:
DecoratedAttributes
Defined in:
lib/hanami/view/part.rb

Overview

This class is abstract.

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.

Since:

  • 2.1.0

%i[
  format
  context
  render
  scope
  value
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DecoratedAttributes

included

Constructor Details

#initialize(rendering: RenderingMissing.new, name: self.class.part_name(rendering.inflector), value:) ⇒ Part

Returns a new Part instance

Parameters:

  • name (Symbol) (defaults to: self.class.part_name(rendering.inflector))

    part name

  • value (Object)

    the value to decorate

  • rendering (Rendering) (defaults to: RenderingMissing.new)

    the current rendering

Since:

  • 2.1.0



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.

Since:

  • 2.1.0



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

#_nameSymbol (readonly)

The part’s name. This comes from the exposure supplying the value.

Returns:

  • (Symbol)

    name

Since:

  • 2.1.0



37
38
39
# File 'lib/hanami/view/part.rb', line 37

def _name
  @_name
end

#_renderingRendering (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

Returns:

Since:

  • 2.1.0



59
60
61
# File 'lib/hanami/view/part.rb', line 59

def _rendering
  @_rendering
end

#_valueObject (readonly) #valueObject (readonly)

The decorated value. This is the value returned from the exposure.

Overloads:

  • #_valueObject

    Returns the value.

  • #valueObject

    A convenience alias for ‘_value`. Is available unless the value itself responds to `#value`.

Returns:

Since:

  • 2.1.0



51
52
53
# File 'lib/hanami/view/part.rb', line 51

def _value
  @_value
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.

Returns:

Since:

  • 2.1.0



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

#_contextContext #contextContext

Returns the context object for the current rendering.

Overloads:

  • #_contextContext

    Returns the context.

  • #contextContext

    A convenience alias for ‘#_context`. Is available unless the value itself responds to `#context`.

Returns:

Since:

  • 2.1.0



118
119
120
# File 'lib/hanami/view/part.rb', line 118

def _context
  _rendering.context
end

#_formatSymbol #formatSymbol

Returns the template format for the current rendering.

Overloads:

  • #_formatSymbol

    Returns the format.

  • #formatSymbol

    A convenience alias for ‘#_format.` Is available unless the value itself responds to `#format`.

Returns:

  • (Symbol)

    format

Since:

  • 2.1.0



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.

Overloads:

  • #_render(partial_name, as: _name, **locals, &block) ⇒ String

    Renders the partial.

  • #render(partial_name, as: _name, **locals, &block) ⇒ String

    A convenience alias for ‘#_render`. Is available unless the value itself responds to `#render`.

Parameters:

  • partial_name (Symbol, String)

    partial name

  • as (Symbol) (defaults to: _name)

    the name for the Part to assume in the partial’s locals. Defaults to the Part’s ‘_name`.

  • locals (Hash<Symbol, Object>)

    other locals to provide the partial

Returns:

  • (String)

    rendered partial

Since:

  • 2.1.0



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.

Overloads:

  • #_scope(scope_name = nil, **locals) ⇒ Hanami::View::Scope

    Builds the scope.

  • #scope(scope_name = nil, **locals) ⇒ Hanami::View::Scope

    A convenience alias for ‘#_scope`. Is available unless the value itself responds to `#scope`.

Parameters:

  • scope_name (Symbol, nil) (defaults to: nil)

    scope name, used by the scope builder to determine the scope class

  • locals (Hash<Symbol, Object>)

    other locals to provide the partial

Returns:

Since:

  • 2.1.0



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

#inspectString

Returns a string representation of the part.

Returns:

Since:

  • 2.1.0



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.

Parameters:

  • klass (Class) (defaults to: self.class)

    part class to use (defaults to the part’s class)

  • name (Symbol) (defaults to: _name)

    part name (defaults to the part’s name)

  • value (Object) (defaults to: _value)

    value to decorate (defaults to the part’s value)

  • options (Hash<Symbol, Object>)

    other options to provide when initializing the new part

See Also:

Since:

  • 2.1.0



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, **options)
  klass.new(
    name: name,
    value: value,
    rendering: _rendering,
    **options
  )
end

#to_sString

Returns a string representation of the value.

Returns:

Since:

  • 2.1.0



172
173
174
# File 'lib/hanami/view/part.rb', line 172

def to_s
  _value.to_s
end