Class: BreadCalculator::Parser
- Inherits:
-
Object
- Object
- BreadCalculator::Parser
- Defined in:
- lib/bread_calculator.rb
Overview
This class converts a nearly free-form text file to a Recipe
Instance Method Summary collapse
-
#initialize ⇒ Parser
constructor
Create a new parser for Recipe.
-
#parse(input) ⇒ Object
Parse text from IO object
input
.
Constructor Details
#initialize ⇒ Parser
Create a new parser for Recipe
375 376 377 378 379 380 381 382 383 |
# File 'lib/bread_calculator.rb', line 375 def initialize @i = 0 @args = @steps = [] @steps[0] = BreadCalculator::Step.new @in_prelude = true @prelude = '' @metadata = Hash.new(nil) end |
Instance Method Details
#parse(input) ⇒ Object
Parse text from IO object input
. It is the caller’s responsibility to open and close the input
correctly.
text recipes consist of a metadata prelude followed by steps.
In prelude lines, anything before a colon is considered to be the name of a metadata field; anything after the colon is a value to populate. Lines without colons are continuations of the ‘notes’ field. I suggest having at least a ‘name’ field.
A line starting with a hyphen ends the prelude and starts the first step.
Each step is delimited by one or more blank lines.
Any line in a step starting with a space or a blank is an ingredient, consisting of quantity, units, and the ingredient itself.
A brief sample:
name: imaginary bread
notes: This is a silly fake bread recipe
makes: 1 bad loaf
This line will become part of the notes
---------------------
Mix:
500 g flour
300 g water
Bake at 375°
417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/bread_calculator.rb', line 417 def parse input while line = input.gets new_step && next if line =~ /(^-)|(^\s*$)/ (line) && next if @in_prelude @args << preprocess_step(line.chomp) end close_step # because we made a spurious one to begin with @steps.shift Recipe.new @metadata, @steps end |