Class: Cosing::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/cosing/database.rb

Constant Summary collapse

ANNOTATION_PATTERN =
%r{([IVX]+)/([IVX]*/)?([\dabcd,]+)}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(annexes) ⇒ Database

Returns a new instance of Database.



9
10
11
12
# File 'lib/cosing/database.rb', line 9

def initialize(annexes)
  @annexes = annexes
  @ingredients = {}
end

Instance Attribute Details

#annexesObject (readonly)

Returns the value of attribute annexes.



5
6
7
# File 'lib/cosing/database.rb', line 5

def annexes
  @annexes
end

#ingredientsObject (readonly)

Returns the value of attribute ingredients.



5
6
7
# File 'lib/cosing/database.rb', line 5

def ingredients
  @ingredients
end

Instance Method Details

#add_ingredient(params) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cosing/database.rb', line 14

def add_ingredient(params)
  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

  @ingredients[params[:reference_number]] = Ingredient.new(
    functions:,
    restrictions:,
    regulations:,
    cas_numbers:,
    einecs_numbers:,
    **params
  )
end

#save(filepath, pretty: false) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/cosing/database.rb', line 34

def save(filepath, pretty: false)
  output = if pretty
    JSON.pretty_generate(@ingredients.to_h)
  else
    JSON.dump(@ingredients.to_h)
  end

  File.write(filepath, output)
end