Map has* predicates to Connector attributes

This commit is contained in:
Maikel Linke
2023-05-25 12:35:29 +10:00
parent bfd084d9b6
commit 949019a277
2 changed files with 15 additions and 5 deletions

View File

@@ -75,13 +75,11 @@ module DataFoodConsortium
property = subject.__send__(:findSemanticProperty, property_id)
# Some properties have a one-to-one match to the method name.
setter_name = "#{statement.predicate.fragment}="
if property.value.is_a?(Enumerable)
property.value << value
elsif subject.respond_to?(setter_name)
subject.public_send(setter_name, value)
else
setter = guess_setter_name(statement.predicate)
subject.try(setter, value) if setter
end
end
@@ -92,6 +90,16 @@ module DataFoodConsortium
def resolve_object(object)
@subjects[object] || object.object
end
def guess_setter_name(predicate)
fragment = predicate.fragment
# Some predicates are named like `hasQuantity`
# but the attribute name would be `quantity`.
name = fragment.sub(/^has/, "").camelize(:lower)
"#{name}="
end
end
end
end

View File

@@ -77,7 +77,9 @@ describe DataFoodConsortium::Connector::Importer, vcr: true do
tomato, items = import(product)
expect(tomato.name).to eq "Tomato"
expect(tomato.quantity).to eq items
expect(items.value).to eq 5
expect(items.unit[:fragment]).to eq "Piece"
# Pending matching concepts:
#expect(items.unit).to eq piece