lib/cosing/annex.rb in cosing-0.2.0 vs lib/cosing/annex.rb in cosing-0.2.1

- old
+ new

@@ -31,137 +31,29 @@ sccs_opinions = build_sccs_opinions(row) yield row.merge( cas_numbers: cas_numbers.compact, ec_numbers: ec_numbers.compact, - sccs_opinions: sccs_opinions.compact, + sccs_opinions: sccs_opinions.compact ) end end def load - annex_ii = Annex::II.new.tap do |annex| - parse("data/annex.II.csv") do |row| - identified_ingredients = transform_array!( - row, - key: :identified_ingredients, - split: ";" - ) + ii, iii, iv, v, vi = [ + Annex::II, + Annex::III, + Annex::IV, + Annex::V, + Annex::VI + ].map(&:load) - annex.add_rule( - row.merge( - identified_ingredients: identified_ingredients.compact - ) - ) - end - end - - annex_iii = Annex::III.new.tap do |annex| - parse("data/annex.III.csv") do |row| - common_ingredients = transform_array!( - row, - key: :common_ingredients, - split: ";" - ) - identified_ingredients = transform_array!( - row, - key: :identified_ingredients, - split: ";" - ) - - annex.add_rule( - row.merge( - common_ingredients: common_ingredients.compact, - identified_ingredients: identified_ingredients.compact, - other_restrictions: row[:other] - ) - ) - end - end - - annex_iv = Annex::IV.new.tap do |annex| - parse("data/annex.IV.csv") do |row| - identified_ingredients = transform_array!( - row, - key: :identified_ingredients, - split: ";" - ) - - annex.add_rule( - row.merge( - identified_ingredients: identified_ingredients.compact, - other_restrictions: row[:other] - ) - ) - end - end - - annex_v = Annex::V.new.tap do |annex| - parse("data/annex.V.csv") do |row| - common_ingredients = transform_array!( - row, - key: :common_ingredients, - split: ";" - ) - identified_ingredients = transform_array!( - row, - key: :identified_ingredients, - split: ";" - ) - - annex.add_rule( - row.merge( - common_ingredients: common_ingredients.compact, - identified_ingredients: identified_ingredients.compact, - other_restrictions: row[:other] - ) - ) - end - end - - annex_vi = Annex::VI.new.tap do |annex| - parse("data/annex.VI.csv") do |row| - common_ingredients = transform_array!( - row, - key: :common_ingredients, - split: ";" - ) - identified_ingredients = transform_array!( - row, - key: :identified_ingredients, - split: ";" - ) - - annex.add_rule( - row.merge( - common_ingredients: common_ingredients.compact, - identified_ingredients: identified_ingredients.compact, - other_restrictions: row[:other] - ) - ) - end - end - - { - ii: annex_ii, - iii: annex_iii, - iv: annex_iv, - v: annex_v, - vi: annex_vi - } + { ii:, iii:, iv:, v:, vi: } end - def transform_array!(params, key:, split:) - params - .delete(key) - .split(split) - .map(&:strip) - .reject { |n| n == "-" } - end - def build_sccs_opinions(row) - transform_array!( + Cosing::Parser.transform_array!( row, key: :sccs_opinions, split: ";" ).map do |opinion| code, *description = opinion.split("-") @@ -172,26 +64,26 @@ ) end end def build_cas_numbers(row) - transform_array!( + Cosing::Parser.transform_array!( row, key: :cas_number, split: "/" ).map do |cas_number| - match = cas_number.match(/(?<cas_number>\d{2,7}-\d{2}-\d)/) + match = cas_number.match(Patterns::CAS_NUMBER) match[:cas_number] if match end end def build_ec_numbers(row) - transform_array!( + Cosing::Parser.transform_array!( row, key: :ec_number, split: "/" ).map do |ec_number| - match = ec_number.match(/(?<ec_number>\d{3}-\d{3}-\d)/) + match = ec_number.match(Patterns::EC_NUMBER) match[:ec_number] if match end end end end