/
qualifier_mapper.rb
51 lines (42 loc) · 1.15 KB
/
qualifier_mapper.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
module Picky
# Collection class for qualifiers.
#
class QualifierMapper
attr_reader :mapping
#
#
def initialize categories
@mapping = {}
categories.each { |category| add category }
end
#
#
def add category
category.qualifiers.each do |qualifier|
sym_qualifier = qualifier.intern
Picky.logger.warn %Q{Warning: Qualifier "#{qualifier}" already mapped to category #{mapping[sym_qualifier].identifier} (ambiguous qualifier mapping).} if mapping.has_key? sym_qualifier
mapping[sym_qualifier] = category
end
end
# Normalizes the given qualifier.
#
# Returns nil if it is not allowed, the normalized qualifier if it is.
#
def map qualifier
return nil if qualifier.empty?
mapping[qualifier.intern]
end
# Restricts the given categories.
#
def restrict user_qualified
if @restricted
user_qualified ? @restricted & user_qualified : @restricted
else
user_qualified
end
end
def restrict_to *qualifiers
@restricted = qualifiers.map { |qualifier| map qualifier }.compact
end
end
end