Class: HTTP::Accept::Languages::Locales
- Inherits:
-
Object
- Object
- HTTP::Accept::Languages::Locales
- Defined in:
- lib/http/accept/languages.rb
Overview
Provides an efficient data-structure for matching the Accept-Languages header to set of available locales according to tools.ietf.org/html/rfc7231#section-5.3.5 and tools.ietf.org/html/rfc4647#section-2.3
Instance Attribute Summary collapse
-
#names ⇒ Object
readonly
Returns the value of attribute names.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
Class Method Summary collapse
Instance Method Summary collapse
-
#&(languages) ⇒ Object
Returns the intersection of others retaining order.
- #+(others) ⇒ Object
- #each(&block) ⇒ Object
- #freeze ⇒ Object
- #include?(locale_name) ⇒ Boolean
-
#initialize(names) ⇒ Locales
constructor
A new instance of Locales.
- #join(*args) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(names) ⇒ Locales
Returns a new instance of Locales.
38 39 40 41 42 43 44 45 |
# File 'lib/http/accept/languages.rb', line 38 def initialize(names) @names = names @patterns = {} @names.each{|name| self.class.(name, @patterns)} self.freeze end |
Instance Attribute Details
#names ⇒ Object (readonly)
Returns the value of attribute names.
60 61 62 |
# File 'lib/http/accept/languages.rb', line 60 def names @names end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
61 62 63 |
# File 'lib/http/accept/languages.rb', line 61 def patterns @patterns end |
Class Method Details
.expand(locale, into) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/http/accept/languages.rb', line 26 def self.(locale, into) parts = locale.split('-') while parts.size > 0 key = parts.join('-') into[key] ||= locale parts.pop end end |
Instance Method Details
#&(languages) ⇒ Object
Returns the intersection of others retaining order.
64 65 66 |
# File 'lib/http/accept/languages.rb', line 64 def & languages languages.collect{|language_range| @patterns[language_range.locale]}.compact end |
#+(others) ⇒ Object
76 77 78 |
# File 'lib/http/accept/languages.rb', line 76 def + others self.class.new(@names + others.to_a) end |
#each(&block) ⇒ Object
54 55 56 57 58 |
# File 'lib/http/accept/languages.rb', line 54 def each(&block) return to_enum unless block_given? @names.each(&block) end |
#freeze ⇒ Object
47 48 49 50 51 52 |
# File 'lib/http/accept/languages.rb', line 47 def freeze @names.freeze @patterns.freeze super end |
#include?(locale_name) ⇒ Boolean
68 69 70 |
# File 'lib/http/accept/languages.rb', line 68 def include? locale_name @patterns.include? locale_name end |
#join(*args) ⇒ Object
72 73 74 |
# File 'lib/http/accept/languages.rb', line 72 def join(*args) @names.join(*args) end |
#to_a ⇒ Object
80 81 82 |
# File 'lib/http/accept/languages.rb', line 80 def to_a @names end |