Module: DeviceMap::Keyword

Defined in:
lib/device_map/keyword.rb

Class Method Summary collapse

Class Method Details

.join(keywords) ⇒ Object

Concatenate all keywords together and skip duplicates



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/device_map/keyword.rb', line 19

def self.join(keywords)
  # HACK: This function handles the case when we want to concatenate all
  # keywords without duplication.
  #     <device id="BlackBerry 9700">
  #         <list>
  #             <value>blackberry</value>
  #             <value>blackberry 9700</value>
  #         </list>
  #     </device>
  normalize(keywords).reduce('') do |result, keyword|
    if keyword.include?(result)
      keyword
    else
      result.concat(keyword)
    end
  end
end

.normalize(keywords) ⇒ Object

Deletes all non-alphanumeric characters from the given keywords



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/device_map/keyword.rb', line 4

def self.normalize(keywords)
  keywords.map do |keyword|
    # HACK: <tt>BUILDER_DATA_SOURCE</tt> contains keywords like:
    #     <device id="BlackBerry 9650">
    #         <list>
    #             <value>[Bb]lack.?[Bb]erry</value>
    #             <value>blackberry 9650</value>
    #         </list>
    #     </device>
    # In such cases we want to replace patterns with simple keywords.
    keyword.downcase.gsub('[bb]', 'b').gsub(/[\W_]+/, '')
  end
end