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.



187
188
189
190
191
# File 'lib/bread_calculator.rb', line 187

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

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



177
178
179
# File 'lib/bread_calculator.rb', line 177

def 
  @metadata
end

#stepsObject (readonly)

Returns the value of attribute steps.



177
178
179
# File 'lib/bread_calculator.rb', line 177

def steps
  @steps
end

Instance Method Details

#bakers_percent(weight) ⇒ Object

Returns the baker’s percentage of a weight



230
231
232
# File 'lib/bread_calculator.rb', line 230

def bakers_percent weight
  weight / bakers_percent_100.to_f
end

#bakers_percent_formulaObject

Returns a Formula



237
238
239
240
# File 'lib/bread_calculator.rb', line 237

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

#ingredientsObject

Returns an array of all Ingredients in Recipe



196
197
198
199
200
201
202
203
204
# File 'lib/bread_calculator.rb', line 196

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

#recipe(args = 1) ⇒ Object

Return a new recipe of weight and units args



258
259
260
# File 'lib/bread_calculator.rb', line 258

def recipe args=1
  self.summary.recipe args
end

#scale_by(ratio) ⇒ Object

Returns new Recipe scaled by ratio



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

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



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/bread_calculator.rb', line 265

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.



296
297
298
299
300
301
302
# File 'lib/bread_calculator.rb', line 296

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



284
285
286
287
288
289
290
# File 'lib/bread_calculator.rb', line 284

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



209
210
211
# File 'lib/bread_calculator.rb', line 209

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