lib/cosing/database.rb in cosing-0.2.0 vs lib/cosing/database.rb in cosing-0.2.1
- old
+ new
@@ -2,82 +2,88 @@
module Cosing
class Database
attr_reader :annexes, :ingredients
+ ANNOTATION_PATTERN = %r{([IVX]+)/([IVX]*/)?([\dabcd,]+)}
+
def initialize(annexes)
@annexes = annexes
@ingredients = {}
end
def add_ingredient(params)
- restrictions = transform_array!(params, key: :restriction, split: "\n")
- annotation_pattern = %r{([IVX]+)/([IVX]*/)?([\dabcd,]+)}
+ restrictions = Parser
+ .transform_array!(params, key: :restriction, split: "\n")
+ regulations = extract_regulations(restrictions).compact
+ cas_numbers = extract_cas_numbers(params).compact
+ einecs_numbers = extract_einecs_numbers(params).compact
+ functions = Parser
+ .transform_array!(params, key: :functions, split: ",")
+ .compact
- regulations = restrictions
- .flat_map do |restriction|
- matches = restriction.scan(annotation_pattern)
- next unless matches.any?
+ @ingredients[params[:reference_number]] = Ingredient.new(
+ functions:,
+ restrictions:,
+ regulations:,
+ cas_numbers:,
+ einecs_numbers:,
+ **params
+ )
+ end
- hits = matches.flat_map do |match|
- numeral, _, reference_number = match
+ def save(filepath, pretty: false)
+ output = if pretty
+ JSON.pretty_generate(@ingredients.to_h)
+ else
+ JSON.dump(@ingredients.to_h)
+ end
- reference_number.split(",").flat_map do |number|
- #debugger if number == "41" && numeral == "VI"
- @annexes[numeral.downcase.to_sym].lookup(number)
- end
- end
+ File.write(filepath, output)
+ end
- hits.compact
- end
+ private
- cas_numbers = transform_array!(
+ def extract_cas_numbers(params)
+ Parser.transform_array!(
params,
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
- functions = transform_array!(params, key: :functions, split: ",")
-
- einecs_numbers = transform_array!(
+ def extract_einecs_numbers(params)
+ Parser.transform_array!(
params,
key: :einecs_number,
split: "/"
).map do |einecs_number|
- match = einecs_number.match(/(?<einecs_number>\d{3}-\d{3}-\d)/)
+ match = einecs_number.match(Patterns::EINECS_NUMBER)
match[:einecs_number] if match
end
-
- @ingredients[params[:reference_number]] = Ingredient.new(
- functions:,
- restrictions: restrictions.compact,
- regulations: regulations.compact,
- cas_numbers: cas_numbers.compact,
- einecs_numbers: einecs_numbers.compact,
- **params
- )
end
- def save(filepath, pretty: false)
- output = if pretty
- JSON.pretty_generate(@ingredients.to_h)
- else
- JSON.dump(@ingredients.to_h)
- end
+ def extract_regulations(restrictions)
+ restrictions
+ .flat_map do |restriction|
+ matches = restriction.scan(ANNOTATION_PATTERN)
+ next if matches.none?
- File.write(filepath, output)
+ extract_hits(matches).compact
+ end
end
- private
+ def extract_hits(matches)
+ matches.flat_map do |match|
+ numeral, _, reference_number = match
- def transform_array!(params, key:, split:)
- params
- .delete(key)
- .split(split)
- .map(&:strip)
- .reject { |n| n == "-" }
+ reference_number.split(",").flat_map do |number|
+ # debugger if number == "41" && numeral == "VI"
+ @annexes[numeral.downcase.to_sym].lookup(number)
+ end
+ end
end
end
end