Class: Cooklang::Recipe

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**components) ⇒ Recipe

Returns a new instance of Recipe.



7
8
9
10
11
12
13
14
15
# File 'lib/cooklang/recipe.rb', line 7

def initialize(**components)
  @ingredients = freeze_component(components[:ingredients])
  @cookware = freeze_component(components[:cookware])
  @timers = freeze_component(components[:timers])
  @steps = freeze_component(components[:steps])
  @metadata = components[:metadata] || Metadata.new
  @sections = freeze_component(components[:sections])
  @notes = freeze_component(components[:notes])
end

Instance Attribute Details

#cookwareObject (readonly)

Returns the value of attribute cookware.



5
6
7
# File 'lib/cooklang/recipe.rb', line 5

def cookware
  @cookware
end

#ingredientsObject (readonly)

Returns the value of attribute ingredients.



5
6
7
# File 'lib/cooklang/recipe.rb', line 5

def ingredients
  @ingredients
end

#metadataObject (readonly)

Returns the value of attribute metadata.



5
6
7
# File 'lib/cooklang/recipe.rb', line 5

def 
  @metadata
end

#notesObject (readonly)

Returns the value of attribute notes.



5
6
7
# File 'lib/cooklang/recipe.rb', line 5

def notes
  @notes
end

#sectionsObject (readonly)

Returns the value of attribute sections.



5
6
7
# File 'lib/cooklang/recipe.rb', line 5

def sections
  @sections
end

#stepsObject (readonly)

Returns the value of attribute steps.



5
6
7
# File 'lib/cooklang/recipe.rb', line 5

def steps
  @steps
end

#timersObject (readonly)

Returns the value of attribute timers.



5
6
7
# File 'lib/cooklang/recipe.rb', line 5

def timers
  @timers
end

Instance Method Details

#==(other) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/cooklang/recipe.rb', line 46

def ==(other)
  return false unless other.is_a?(Recipe)

  comparable_attributes.all? do |attr|
    send(attr) == other.send(attr)
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/cooklang/recipe.rb', line 61

def eql?(other)
  self == other
end

#hashObject



65
66
67
# File 'lib/cooklang/recipe.rb', line 65

def hash
  [ingredients, cookware, timers, steps, ].hash
end

#ingredients_hashObject



25
26
27
28
29
30
31
32
# File 'lib/cooklang/recipe.rb', line 25

def ingredients_hash
  @ingredients.each_with_object({}) do |ingredient, hash|
    hash[ingredient.name] = {
      quantity: ingredient.quantity,
      unit: ingredient.unit
    }.compact
  end
end

#steps_textObject



34
35
36
# File 'lib/cooklang/recipe.rb', line 34

def steps_text
  @steps.map(&:to_text)
end

#to_hObject



38
39
40
# File 'lib/cooklang/recipe.rb', line 38

def to_h
  Formatters::Hash.new(self).generate
end

#to_json(*args) ⇒ Object



42
43
44
# File 'lib/cooklang/recipe.rb', line 42

def to_json(*args)
  Formatters::Json.new(self).generate(*args)
end