Module: Menuconform::Rules::AllergenRules

Defined in:
lib/menuconform/rules/allergen_rules.rb

Overview

ALLERGEN-001..002. An explicit empty contains list is a declaration ("we state this has none of the big-9") and satisfies ALLERGEN-001; only a wholly absent allergens object is an enrichment gap.

Class Method Summary collapse

Class Method Details

.call(menu) ⇒ Object



11
12
13
# File 'lib/menuconform/rules/allergen_rules.rb', line 11

def call(menu)
  missing(menu) + contradictions(menu)
end

.contradictions(menu) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/menuconform/rules/allergen_rules.rb', line 22

def contradictions(menu)
  check = lambda do |entity, type|
    a = entity["allergens"]
    return nil unless a
    both = (a["contains"] || []) & (a["may_contain"] || [])
    return nil if both.empty?
    Finding.new("ALLERGEN-002", type, entity["id"],
                "#{both.join(', ')} declared in both contains and may_contain")
  end
  menu.items_by_id.each_value.filter_map { |it| check.call(it, "item") } +
    menu.modifiers_by_id.each_value.filter_map { |m| check.call(m, "modifier") }
end

.missing(menu) ⇒ Object



15
16
17
18
19
20
# File 'lib/menuconform/rules/allergen_rules.rb', line 15

def missing(menu)
  menu.items_by_id.each_value.filter_map do |it|
    next if it.key?("allergens")
    Finding.new("ALLERGEN-001", "item", it["id"], "no allergen information declared")
  end
end