Sha256: a597f20dfec86f0701c2c9d8f9f0048ec17daa678ea75d73aa44a79b9491797e

Contents?: true

Size: 966 Bytes

Versions: 5

Compression:

Stored size: 966 Bytes

Contents

require 'dry-equalizer'

module Dry
  module View
    class Scope
      include Dry::Equalizer(:_locals, :_context, :_renderer)

      attr_reader :_locals
      attr_reader :_context
      attr_reader :_renderer

      def initialize(renderer:, context: nil, locals: {})
        @_locals = locals
        @_context = context
        @_renderer = renderer
      end

      def render(partial_name, **locals, &block)
        _renderer.partial(partial_name, __render_scope(locals), &block)
      end

      private

      def method_missing(name, *args, &block)
        if _locals.key?(name)
          _locals[name]
        elsif _context.respond_to?(name)
          _context.public_send(name, *args, &block)
        else
          super
        end
      end

      def __render_scope(**locals)
        if locals.any?
          self.class.new(renderer: _renderer, context: _context, locals: locals)
        else
          self
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dry-view-0.5.3 lib/dry/view/scope.rb
dry-view-0.5.2 lib/dry/view/scope.rb
dry-view-0.5.1 lib/dry/view/scope.rb
dry-view-0.5.0 lib/dry/view/scope.rb
dry-view-0.4.0 lib/dry/view/scope.rb