Class: Hanami::View::Scope Abstract

Inherits:
Object show all
Defined in:
lib/hanami/view/scope.rb

Overview

This class is abstract.

Subclass this and provide your own methods adding view-specific behavior. You should not override #initialize

Evaluation context for templates (including layouts and partials) and provides a place to encapsulate view-specific behaviour alongside a template and its locals.

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 locals].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, locals: Dry::Core::Constants::EMPTY_HASH, rendering: RenderingMissing.new) ⇒ Scope

Returns a new Scope instance.

Parameters:

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

    scope name

  • locals (Hash<Symbol, Object>) (defaults to: Dry::Core::Constants::EMPTY_HASH)

    template locals

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

    the current rendering

Since:

  • 2.1.0



65
66
67
68
69
70
71
72
73
# File 'lib/hanami/view/scope.rb', line 65

def initialize(
  name: nil,
  locals: Dry::Core::Constants::EMPTY_HASH,
  rendering: RenderingMissing.new
)
  @_name = name
  @_locals = locals
  @_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, according to the following rules:

  1. If there is a local with a name matching the method, it returns the local.
  2. If the context responds to the method, then it will be sent the method and all its arguments.

Since:

  • 2.1.0



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/hanami/view/scope.rb', line 158

def method_missing(name, *args, &block)
  if _locals.key?(name)
    _locals[name]
  elsif _context.respond_to?(name)
    _context.public_send(name, *args, &block)
  elsif CONVENIENCE_METHODS.include?(name)
    __send__(:"_#{name}", *args, &block)
  else
    super
  end
end

Instance Attribute Details

#_localsHash[<Symbol, Object>] (readonly) #localsHash[<Symbol, Object>] (readonly)

Returns the scope's locals

Overloads:

  • #_localsHash[<Symbol, Object>]

    Returns the locals.

  • #localsHash[<Symbol, Object>]

    A convenience alias for #_locals. Is available unless there is a local named locals

Returns:

  • (Hash[<Symbol, Object>])

    Hash[]

Since:

  • 2.1.0



45
46
47
# File 'lib/hanami/view/scope.rb', line 45

def _locals
  @_locals
end

#_nameSymbol (readonly)

Returns the scope's name.

Returns:

  • (Symbol)

Since:

  • 2.1.0



32
33
34
# File 'lib/hanami/view/scope.rb', line 32

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.

Returns the current rendering.

Returns:

Since:

  • 2.1.0



53
54
55
# File 'lib/hanami/view/scope.rb', line 53

def _rendering
  @_rendering
end

Instance Method Details

#_contextContext #contextContext

Returns the context object for the current render environment.

Overloads:

  • #_contextContext

    Returns the context.

  • #contextContext

    A convenience alias for #_context. Is available unless there is a local named context.

Returns:

Since:

  • 2.1.0



147
148
149
# File 'lib/hanami/view/scope.rb', line 147

def _context
  _rendering.context
end

#_formatSymbol #formatSymbol

Returns the template format for the current render environment.

Overloads:

  • #_formatSymbol

    Returns the format.

  • #formatSymbol

    A convenience alias for #_format. Is available unless there is a local named format

Returns:

  • (Symbol)

    format

Since:

  • 2.1.0



131
132
133
# File 'lib/hanami/view/scope.rb', line 131

def _format
  _rendering.format
end

#render(partial_name, **locals, &block) ⇒ String #render(**locals, &block) ⇒ String

Returns the rendered partial output.

Overloads:

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

    Renders a partial using the scope.

    Parameters:

    • partial_name (Symbol, String)

      partial name

    • locals (Hash<Symbol, Object>)

      partial locals

    Yield Returns:

    • (String)

      string content to include where the partial calls yield

  • #render(**locals, &block) ⇒ String

    Renders a partial (named after the scope's own name) using the scope.

    Parameters:

    • locals (Hash<Symbol, Object>)

      partial locals

    Yield Returns:

    • (String)

      string content to include where the partial calls yield

Returns:

  • (String)

    the rendered partial output

Since:

  • 2.1.0



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hanami/view/scope.rb', line 92

def render(partial_name = nil, **locals, &block)
  partial_name ||= _name

  unless partial_name
    raise ArgumentError, "+partial_name+ must be provided for unnamed scopes"
  end

  if partial_name.is_a?(Class)
    partial_name = _inflector.underscore(_inflector.demodulize(partial_name.to_s))
  end

  _rendering.partial(partial_name, _render_scope(**locals), &block)
end

#scope(name = nil, **locals) ⇒ Scope

Builds a new scope using a scope class matching the provided name.

Parameters:

  • name (Symbol, Class) (defaults to: nil)

    scope name (or class)

  • locals (Hash<Symbol, Object>)

    scope locals

Returns:

Since:

  • 2.1.0



115
116
117
# File 'lib/hanami/view/scope.rb', line 115

def scope(name = nil, **locals)
  _rendering.scope(name, locals)
end