Class: JustTheRecipe::Cookbook
- Inherits:
-
Object
- Object
- JustTheRecipe::Cookbook
- Defined in:
- lib/just-the-recipe/cookbook.rb
Constant Summary collapse
- @@all =
[]
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
- .all ⇒ Object
- .create_from_files ⇒ Object
- .find_by_name(name) ⇒ Object
- .find_or_create_by_name(name) ⇒ Object
- .list_cookbooks ⇒ Object
Instance Method Summary collapse
- #delete_cookbook ⇒ Object
- #get_recipes ⇒ Object
-
#initialize(name) ⇒ Cookbook
constructor
A new instance of Cookbook.
- #return_cookbook ⇒ Object
- #write_recipe_to_cookbook(recipe) ⇒ Object
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
#name ⇒ Object
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
.all ⇒ Object
13 14 15 |
# File 'lib/just-the-recipe/cookbook.rb', line 13 def self.all @@all end |
.create_from_files ⇒ Object
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_cookbooks ⇒ Object
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_cookbook ⇒ Object
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_recipes ⇒ Object
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_cookbook ⇒ Object
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 |