Class: DeviceMap::DeviceData::Patterns

Inherits:
Object
  • Object
show all
Defined in:
lib/device_map/device_data/patterns.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(all_patterns) ⇒ Patterns

Returns a new instance of Patterns.



25
26
27
28
29
30
31
32
33
34
# File 'lib/device_map/device_data/patterns.rb', line 25

def initialize(all_patterns)
  @pattern_index = {}

  all_patterns.each do |pattern|
    pattern.keywords.each do |keyword|
      @pattern_index[keyword] ||= Set.new
      @pattern_index[keyword] << pattern
    end
  end
end

Class Method Details

.parse(openddr_builder_xml) ⇒ Object

rubocop:disable Metrics/MethodLength



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/device_map/device_data/patterns.rb', line 7

def self.parse(openddr_builder_xml)
  builders_doc = Nokogiri::XML(openddr_builder_xml)
  openddr_builders = builders_doc.xpath('//builder')

  all_patterns = openddr_builders.flat_map do |builder_node|
    builder_node_class = builder_node[:class]
    builder = Builder.find(builder_node_class)

    builder_node.xpath('device').flat_map do |device_node|
      device_id = device_node[:id]
      keywords = device_node.xpath('list/value').map(&:content)
      builder.patterns(device_id, keywords)
    end
  end

  new(all_patterns)
end

Instance Method Details

#find(keyword) ⇒ Object



36
37
38
39
# File 'lib/device_map/device_data/patterns.rb', line 36

def find(keyword)
  pattern_set = @pattern_index.fetch(keyword) { Set.new }
  pattern_set.freeze
end