/
template_details.rb
66 lines (55 loc) · 1.64 KB
/
template_details.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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