Sha256: 35916fc240065bafaedcf1659fcabdd21f2e4d885853e357dd54d574a2183c9a

Contents?: true

Size: 1.45 KB

Versions: 38

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require "concurrent/map"

module ActionView
  class UnboundTemplate
    attr_reader :virtual_path, :details
    delegate :locale, :format, :variant, :handler, to: :@details

    def initialize(source, identifier, details:, virtual_path:)
      @source = source
      @identifier = identifier
      @details = details
      @virtual_path = virtual_path

      @templates = Concurrent::Map.new(initial_capacity: 2)
      @write_lock = Mutex.new
    end

    def bind_locals(locals)
      if template = @templates[locals]
        template
      else
        @write_lock.synchronize do
          normalized_locals = normalize_locals(locals)

          # We need ||=, both to dedup on the normalized locals and to check
          # while holding the lock.
          @templates[normalized_locals] ||= build_template(normalized_locals)

          # This may have already been assigned, but we've already de-dup'd so
          # reassignment is fine.
          @templates[locals.dup] = @templates[normalized_locals]
        end
      end
    end

    private
      def build_template(locals)
        Template.new(
          @source,
          @identifier,
          details.handler_class,

          format: details.format_or_default,
          variant: variant&.to_s,
          virtual_path: @virtual_path,

          locals: locals.map(&:to_s)
        )
      end

      def normalize_locals(locals)
        locals.map(&:to_sym).sort!.freeze
      end
  end
end

Version data entries

38 entries across 36 versions & 4 rubygems

Version Path
actionview-7.0.4.3 lib/action_view/unbound_template.rb
actionview-7.0.4.2 lib/action_view/unbound_template.rb
actionview-7.0.4.1 lib/action_view/unbound_template.rb
actionview-7.0.4 lib/action_view/unbound_template.rb
actionview-7.0.3.1 lib/action_view/unbound_template.rb
actionview-7.0.3 lib/action_view/unbound_template.rb
actionview-7.0.2.4 lib/action_view/unbound_template.rb
actionview-7.0.2.3 lib/action_view/unbound_template.rb
actionview-7.0.2.2 lib/action_view/unbound_template.rb
actionview-7.0.2.1 lib/action_view/unbound_template.rb
actionview-7.0.2 lib/action_view/unbound_template.rb
actionview-7.0.1 lib/action_view/unbound_template.rb
actionview-7.0.0 lib/action_view/unbound_template.rb
actionview-7.0.0.rc3 lib/action_view/unbound_template.rb
actionview-7.0.0.rc2 lib/action_view/unbound_template.rb
actionview-7.0.0.rc1 lib/action_view/unbound_template.rb
actionview-7.0.0.alpha2 lib/action_view/unbound_template.rb
actionview-7.0.0.alpha1 lib/action_view/unbound_template.rb