Class: Dry::View::Scope Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/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.

%i[format context locals].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, locals: Dry::Core::Constants::EMPTY_HASH, render_env: RenderEnvironmentMissing.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

  • render_env (RenderEnvironment) (defaults to: RenderEnvironmentMissing.new)

    render environment



62
63
64
65
66
67
68
69
70
# File 'lib/dry/view/scope.rb', line 62

def initialize(
  name: nil,
  locals: Dry::Core::Constants::EMPTY_HASH,
  render_env: RenderEnvironmentMissing.new
)
  @_name = name
  @_locals = locals
  @_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, 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.



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/dry/view/scope.rb', line 152

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)

The scope’s locals

Overloads:

  • #_localsHash[<Symbol, Object>]

    Returns the locals

  • #localsHash[<Symbol, Object>]

    A convenience alias for ‘#_format.` Is available unless there is a local named `locals`

Returns:

  • (Hash[<Symbol, Object>])

    Hash[<Symbol, Object>]



44
45
46
# File 'lib/dry/view/scope.rb', line 44

def _locals
  @_locals
end

#_nameSymbol (readonly)

The scope’s name

Returns:

  • (Symbol)


31
32
33
# File 'lib/dry/view/scope.rb', line 31

def _name
  @_name
end

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

Returns:



51
52
53
# File 'lib/dry/view/scope.rb', line 51

def _render_env
  @_render_env
end

Instance Method Details

#_contextContext #contextContext

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:



140
141
142
# File 'lib/dry/view/scope.rb', line 140

def _context
  _render_env.context
end

#_formatSymbol #formatSymbol

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



125
126
127
# File 'lib/dry/view/scope.rb', line 125

def _format
  _render_env.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



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dry/view/scope.rb', line 88

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

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

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

Build 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:



110
111
112
# File 'lib/dry/view/scope.rb', line 110

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