Sha256: 839218b570795dd9516868cce3bd5860da3af865536cc4ebce66d60a7c41e609

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require "dry/equalizer"

module Dry
  class View
    # @api private
    class RenderEnvironment
      def self.prepare(renderer, config, context)
        new(
          renderer: renderer,
          inflector: config.inflector,
          context: context,
          scope_builder: config.scope_builder.new(namespace: config.scope_namespace),
          part_builder: config.part_builder.new(namespace: config.part_namespace),
        )
      end

      include Dry::Equalizer(:renderer, :inflector, :context, :scope_builder, :part_builder)

      attr_reader :renderer, :inflector, :context, :scope_builder, :part_builder

      def initialize(renderer:, inflector:, context:, scope_builder:, part_builder:)
        @renderer = renderer
        @inflector = inflector
        @context = context.for_render_env(self)
        @scope_builder = scope_builder.for_render_env(self)
        @part_builder = part_builder.for_render_env(self)
      end

      def format
        renderer.format
      end

      def part(name, value, **options)
        part_builder.(name, value, **options)
      end

      def scope(name = nil, locals)
        scope_builder.(name, locals)
      end

      def template(name, scope, &block)
        renderer.template(name, scope, &block)
      end

      def partial(name, scope, &block)
        renderer.partial(name, scope, &block)
      end

      def chdir(dirname)
        self.class.new(
          renderer: renderer.chdir(dirname),
          inflector: inflector,
          context: context,
          scope_builder: scope_builder,
          part_builder: part_builder,
        )
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-view-0.7.0 lib/dry/view/render_environment.rb
dry-view-0.6.0 lib/dry/view/render_environment.rb