Sha256: 1a95016d26bf8b861086611be2930e5fe2ccd8f5cc4f6f5e819ed69560c55075

Contents?: true

Size: 1.64 KB

Versions: 74

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module ActionView
  class TemplateDetails # :nodoc:
    class Requested
      attr_reader :locale, :handlers, :formats, :variants
      attr_reader :locale_idx, :handlers_idx, :formats_idx, :variants_idx

      ANY_HASH = Hash.new(1).merge(nil => 0).freeze

      def initialize(locale:, handlers:, formats:, variants:)
        @locale = locale
        @handlers = handlers
        @formats = formats
        @variants = variants

        @locale_idx   = build_idx_hash(locale)
        @handlers_idx = build_idx_hash(handlers)
        @formats_idx  = build_idx_hash(formats)
        if variants == :any
          @variants_idx = ANY_HASH
        else
          @variants_idx = build_idx_hash(variants)
        end
      end

      private
        def build_idx_hash(arr)
          [*arr, nil].each_with_index.to_h.freeze
        end
    end

    attr_reader :locale, :handler, :format, :variant

    def initialize(locale, handler, format, variant)
      @locale = locale
      @handler = handler
      @format = format
      @variant = variant
    end

    def matches?(requested)
      requested.formats_idx[@format] &&
        requested.locale_idx[@locale] &&
        requested.variants_idx[@variant] &&
        requested.handlers_idx[@handler]
    end

    def sort_key_for(requested)
      [
        requested.formats_idx[@format],
        requested.locale_idx[@locale],
        requested.variants_idx[@variant],
        requested.handlers_idx[@handler]
      ]
    end

    def handler_class
      Template.handler_for_extension(handler)
    end

    def format_or_default
      format || handler_class.try(:default_format)
    end
  end
end

Version data entries

74 entries across 71 versions & 7 rubygems

Version Path
actionview-8.0.0 lib/action_view/template_details.rb
actionview-7.2.2 lib/action_view/template_details.rb
actionview-7.1.5 lib/action_view/template_details.rb
actionview-8.0.0.rc2 lib/action_view/template_details.rb
actionview-7.2.1.2 lib/action_view/template_details.rb
actionview-7.1.4.2 lib/action_view/template_details.rb
actionview-7.0.8.6 lib/action_view/template_details.rb
actionview-8.0.0.rc1 lib/action_view/template_details.rb
actionview-7.2.1.1 lib/action_view/template_details.rb
actionview-7.1.4.1 lib/action_view/template_details.rb
actionview-7.0.8.5 lib/action_view/template_details.rb
actionview-8.0.0.beta1 lib/action_view/template_details.rb
omg-actionview-8.0.0.alpha9 lib/action_view/template_details.rb
omg-actionview-8.0.0.alpha8 lib/action_view/template_details.rb
omg-actionview-8.0.0.alpha7 lib/action_view/template_details.rb
omg-actionview-8.0.0.alpha4 lib/action_view/template_details.rb
omg-actionview-8.0.0.alpha3 lib/action_view/template_details.rb
omg-actionview-8.0.0.alpha2 lib/action_view/template_details.rb
omg-actionview-8.0.0.alpha1 lib/action_view/template_details.rb
actionview-7.1.4 lib/action_view/template_details.rb