-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.rb
45 lines (38 loc) · 1.08 KB
/
model.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require "io/console"
require_relative "controller.rb"
class Model
attr_reader :possible_cocktails
def initialize
@recipe_array_of_hashes = populate_recipe_hash
end
def populate_recipe_hash
text = IO.read("cocktail_list.txt")
separate_cocktails = text.split("\n\n")
separate_cocktails_from_ingredients = separate_cocktails.map { |line| line.split("\n") }
separate_cocktails_from_ingredients.map! do |cocktail|
cocktail.map!.with_index do |item, index|
if index == 1
item.split(",")
else
item
end
end
end
separate_cocktails_from_ingredients.map! do |cocktail|
Hash[[cocktail]]
end
end
def check_hash_for_possible_recipes(ingredient)
@possible_cocktails = []
@recipe_array_of_hashes.each do |hash|
hash.values[0].each do |item|
if item =~ /\b#{ingredient}\b/
@possible_cocktails.push(hash.key(hash.values[0]))
end
end
end
end
end
# model = Model.new
# p model.populate_recipe_hash.is_a?(Array) == true
# p model.check_hash_for_possible_recipes("rum")