Class: HTTP::Accept::Languages::Locales

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(names) ⇒ Locales

Returns a new instance of Locales.



54
55
56
57
58
59
60
61
# File 'lib/http/accept/languages.rb', line 54

def initialize(names)
	@names = names
	@patterns = {}
	
	@names.each{|name| self.class.expand(name, @patterns)}
	
	self.freeze
end

Instance Attribute Details

#namesObject (readonly)

Returns the value of attribute names.



76
77
78
# File 'lib/http/accept/languages.rb', line 76

def names
  @names
end

#patternsObject (readonly)

Returns the value of attribute patterns.



77
78
79
# File 'lib/http/accept/languages.rb', line 77

def patterns
  @patterns
end

Class Method Details

.expand(locale, into) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/http/accept/languages.rb', line 42

def self.expand(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.



80
81
82
# File 'lib/http/accept/languages.rb', line 80

def & languages
	languages.collect{|language_range| @patterns[language_range.locale]}.compact
end

#+(others) ⇒ Object



92
93
94
# File 'lib/http/accept/languages.rb', line 92

def + others
	self.class.new(@names + others.to_a)
end

#each(&block) ⇒ Object



70
71
72
73
74
# File 'lib/http/accept/languages.rb', line 70

def each(&block)
	return to_enum unless block_given?
	
	@names.each(&block)
end

#freezeObject



63
64
65
66
67
68
# File 'lib/http/accept/languages.rb', line 63

def freeze
	@names.freeze
	@patterns.freeze
	
	super
end

#include?(locale_name) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/http/accept/languages.rb', line 84

def include? locale_name
	@patterns.include? locale_name
end

#join(*args) ⇒ Object



88
89
90
# File 'lib/http/accept/languages.rb', line 88

def join(*args)
	@names.join(*args)
end

#to_aObject



96
97
98
# File 'lib/http/accept/languages.rb', line 96

def to_a
	@names
end