Class: JustTheRecipe::Cookbook

Inherits:
Object
  • Object
show all
Defined in:
lib/just-the-recipe/cookbook.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Cookbook

Returns a new instance of Cookbook.



7
8
9
10
11
# File 'lib/just-the-recipe/cookbook.rb', line 7

def initialize(name)
    @name = name
    File.write("#{name}.txt", "" , mode: "a")
    @@all << self
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/just-the-recipe/cookbook.rb', line 3

def name
  @name
end

Class Method Details

.allObject



13
14
15
# File 'lib/just-the-recipe/cookbook.rb', line 13

def self.all
    @@all
end

.create_from_filesObject



17
18
19
20
21
22
23
# File 'lib/just-the-recipe/cookbook.rb', line 17

def self.create_from_files
    files = Dir["*.txt"]
    files.each do |f|
        name = f.gsub(".txt","")
        find_or_create_by_name(name)
    end    
end

.find_by_name(name) ⇒ Object



25
26
27
# File 'lib/just-the-recipe/cookbook.rb', line 25

def self.find_by_name(name)
    @@all.find{|i| i.name == name}
end

.find_or_create_by_name(name) ⇒ Object



29
30
31
# File 'lib/just-the-recipe/cookbook.rb', line 29

def self.find_or_create_by_name(name)
    find_by_name(name) ? find_by_name(name) : self.new(name)
end

.list_cookbooksObject



41
42
43
# File 'lib/just-the-recipe/cookbook.rb', line 41

def self.list_cookbooks
    @@all.map{|i| i.name}
end

Instance Method Details

#delete_cookbookObject



49
50
51
52
# File 'lib/just-the-recipe/cookbook.rb', line 49

def delete_cookbook
    @@all.delete(self)
    File.delete("#{@name}.txt")
end

#get_recipesObject



33
34
35
# File 'lib/just-the-recipe/cookbook.rb', line 33

def get_recipes
    JustTheRecipe::Recipe.all.select {|i| i.cookbook == self} 
end

#return_cookbookObject



45
46
47
# File 'lib/just-the-recipe/cookbook.rb', line 45

def return_cookbook
    File.read("#{@name}.txt")
end

#write_recipe_to_cookbook(recipe) ⇒ Object



37
38
39
# File 'lib/just-the-recipe/cookbook.rb', line 37

def write_recipe_to_cookbook(recipe)
    File.write("#{self.name}.txt", recipe.return_recipe , mode: "a")
end