forked from openfoodfoundation/openfoodnetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfc_product_type_factory.rb
47 lines (34 loc) · 1 KB
/
dfc_product_type_factory.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
# frozen_string_literal: true
require 'singleton'
class DfcProductTypeFactory
include Singleton
def self.for(dfc_id)
instance.for(dfc_id)
end
def initialize
@product_types = {}
populate_product_types
end
def for(dfc_id)
@product_types[dfc_id]
end
private
def populate_product_types
DfcLoader.connector.PRODUCT_TYPES.topConcepts.each do |product_type|
record_type(DfcLoader.connector.PRODUCT_TYPES, product_type.to_s)
end
end
def record_type(product_type_object, product_type)
current_product_type = product_type_object.public_send(product_type.to_s)
id = current_product_type.semanticId
@product_types[id] = current_product_type
# Narrower product types are defined as class method on the current product type object
narrowers = current_product_type.methods(false).sort
# Leaf node
return if narrowers.empty?
narrowers.each do |narrower|
# recursive call
record_type(current_product_type, narrower)
end
end
end