Class: BreadCalculator::Recipe

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

Overview

This class represents a recipe.

Runtime-generated methods:

total_flours 
total_liquids
total_additives

return totals of their respective types

Direct Known Subclasses

Summary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata, steps) ⇒ Recipe

Creates a new Recipe with hash metadata and array of Steps steps

metadata is freeform, but most likely should include :name. Other likely keys are: :prep_time, :total_time, :notes, :history, :serves, :makes, :attribution.



173
174
175
176
177
# File 'lib/bread_calculator.rb', line 173

def initialize , steps
  @metadata = 
  @steps    = steps
  @ingredients = self.ingredients
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



163
164
165
# File 'lib/bread_calculator.rb', line 163

def 
  @metadata
end

#stepsObject (readonly)

Returns the value of attribute steps.



163
164
165
# File 'lib/bread_calculator.rb', line 163

def steps
  @steps
end

Instance Method Details

#bakers_percent(weight) ⇒ Object

Returns the baker’s percentage of a weight



216
217
218
# File 'lib/bread_calculator.rb', line 216

def bakers_percent weight
  weight / bakers_percent_100.to_f
end

#bakers_percent_formulaObject

Returns a Formula



223
224
225
226
# File 'lib/bread_calculator.rb', line 223

def bakers_percent_formula
  ratio = 100.0 / self.total_flours
  self.scale_by ratio
end

#ingredientsObject

Returns an array of all Ingredients in Recipe



182
183
184
185
186
187
188
189
190
# File 'lib/bread_calculator.rb', line 182

def ingredients
  a = Array.new
  self.steps.each do |step|
    step.ingredients.each do |ing|
      a << ing
    end
  end
  a
end

#scale_by(ratio) ⇒ Object

Returns new Recipe scaled by ratio



231
232
233
234
235
236
237
238
239
240
# File 'lib/bread_calculator.rb', line 231

def scale_by ratio
  new_steps = self.steps.map do |s| 
    step_args = s.techniques.map do |t| 
      t.is_a?(Ingredient) ? t.scale_by(ratio) : t
    end
    Step.new step_args
  end

  Recipe.new self., new_steps
end

#summaryObject

Returns a Summary



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/bread_calculator.rb', line 245

def summary
  new_meta = self.
  [:flours, :liquids, :additives].each do |s|
    new_meta["total_#{s}"] = eval "self.bakers_percent self.total_#{s}"
  end
  
  new_steps = self.steps.map do |s| 
    step_args = s.techniques.map do |t| 
      t.is_a?(Ingredient) ? t.as_bp(self.bakers_percent_100) : t
    end
    Step.new step_args
  end

  Summary.new new_meta, new_steps
end

#to_htmlObject

Print recipe as html. It is the caller’s responsibility to provide appropriate headers, etc.



276
277
278
279
280
281
282
# File 'lib/bread_calculator.rb', line 276

def to_html
  out = ''
  self..each{|k,v| out << "<p>\n<b>#{k}</b>: #{v}\n</p>\n"}
  out << "--------------------\n"
  self.steps.each{|s| out << s.to_html }
  out
end

#to_sObject

Print a nice text version of Recipe



264
265
266
267
268
269
270
# File 'lib/bread_calculator.rb', line 264

def to_s
  out = ''
  self..each{|k,v| out << "#{k}: #{v}\n"}
  out << "--------------------\n"
  self.steps.each{|s| out << s.to_s }
  out
end

#weightObject

Returns the total weight of Ingredients in Recipe



195
196
197
# File 'lib/bread_calculator.rb', line 195

def weight
  self.ingredients.map{|i| i.quantity}.reduce(:+)
end