Class: Recipe

Inherits:
Object
  • Object
show all
Defined in:
lib/practica8/recipe.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Recipe

Returns a new instance of Recipe.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/practica8/recipe.rb', line 4

def initialize(name, &block)
  self.name = name
  self.ingredients = []
  self.instructions = []

  if block_given?  
    if block.arity == 1
      yield self
    else
     instance_eval &block 
    end
  end
end

Instance Attribute Details

#ingredientsObject

Returns the value of attribute ingredients.



2
3
4
# File 'lib/practica8/recipe.rb', line 2

def ingredients
  @ingredients
end

#instructionsObject

Returns the value of attribute instructions.



2
3
4
# File 'lib/practica8/recipe.rb', line 2

def instructions
  @instructions
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/practica8/recipe.rb', line 2

def name
  @name
end

Instance Method Details

#ingredient(name, options = {}) ⇒ Object



30
31
32
33
34
35
# File 'lib/practica8/recipe.rb', line 30

def ingredient(name, options = {})
  ingredient = name
  ingredient << " (#{options[:amount]})" if options[:amount]

  ingredients << ingredient
end

#step(text, options = {}) ⇒ Object



37
38
39
40
41
42
# File 'lib/practica8/recipe.rb', line 37

def step(text, options = {})
  instruction = text
  instruction << " (#{options[:during]})" if options[:during]

  instructions << instruction
end

#to_sObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/practica8/recipe.rb', line 18

def to_s
  output = name
  output << "\n#{'=' * name.size}\n\n"
  output << "Ingredients: #{ingredients.join(', ')}\n\n"

  instructions.each_with_index do |instruction, index|
    output << "#{index + 1}) #{instruction}\n"
  end

  output
end