mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Simplify traversing of Product Types
This commit is contained in:
@@ -23,39 +23,25 @@ class DfcProductTypeFactory
|
||||
|
||||
def populate_product_types
|
||||
DfcLoader.connector.PRODUCT_TYPES.topConcepts.each do |product_type|
|
||||
stack = []
|
||||
record_type(stack, product_type.to_s)
|
||||
record_type(DfcLoader.connector.PRODUCT_TYPES, product_type.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
def record_type(stack, product_type)
|
||||
name = product_type.to_s
|
||||
current_stack = stack.dup.push(name)
|
||||
def record_type(product_type_object, product_type)
|
||||
current_product_type = product_type_object.public_send(product_type.to_s)
|
||||
|
||||
type = call_dfc_product_type(current_stack)
|
||||
|
||||
id = type.semanticId
|
||||
@product_types[id] = type
|
||||
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 = type.methods(false).sort
|
||||
narrowers = current_product_type.methods(false).sort
|
||||
|
||||
# Leaf node
|
||||
return if narrowers.empty?
|
||||
|
||||
narrowers.each do |narrower|
|
||||
# recursive call
|
||||
record_type(current_stack, narrower)
|
||||
record_type(current_product_type, narrower)
|
||||
end
|
||||
end
|
||||
|
||||
# Callproduct type method ie: DfcLoader.connector.PRODUCT_TYPES.DRINK.SOFT_DRINK
|
||||
def call_dfc_product_type(product_type_path)
|
||||
type = DfcLoader.connector.PRODUCT_TYPES
|
||||
product_type_path.each do |pt|
|
||||
type = type.public_send(pt)
|
||||
end
|
||||
|
||||
type
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user