Class: BeerRecipe::RecipeFormatter

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

Direct Known Subclasses

HtmlFormatter, TextFormatter

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RecipeFormatter

Returns a new instance of RecipeFormatter.



2
3
4
# File 'lib/beer_recipe/recipe_formatter.rb', line 2

def initialize(options = {})
  @options = options
end

Instance Method Details

#format(recipe) ⇒ Object



6
7
8
9
# File 'lib/beer_recipe/recipe_formatter.rb', line 6

def format(recipe)
  @recipe = recipe
  self
end

#format_records(records, set) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/beer_recipe/recipe_formatter.rb', line 36

def format_records(records, set)
  if records.size > 0
    send("before_#{set}") if respond_to? "before_#{set}"
    records.each do |record|
      if respond_to? record.format_method
        send(record.format_method, record)
      end
    end
    send("after_#{set}") if respond_to? "after_#{set}"
  end
end

#outputObject



28
29
30
31
32
33
34
# File 'lib/beer_recipe/recipe_formatter.rb', line 28

def output
  format_recipe
  BeerRecipe::RecipeWrapper::SETS.map do |set|
    format_records(@recipe.send(set), set)
  end
  format_mash(@recipe.mash)
end

#parseObject



23
24
25
26
# File 'lib/beer_recipe/recipe_formatter.rb', line 23

def parse
  erb = ERB.new(template).result(@recipe.get_binding)
  StringIO.new(erb)
end

#templateObject



19
20
21
# File 'lib/beer_recipe/recipe_formatter.rb', line 19

def template
  IO.read(template_file)
end

#template_fileObject

Raises:

  • (BeerRecipe::NotImplementedError)


15
16
17
# File 'lib/beer_recipe/recipe_formatter.rb', line 15

def template_file
  raise BeerRecipe::NotImplementedError.new
end

#template_path(file) ⇒ Object



11
12
13
# File 'lib/beer_recipe/recipe_formatter.rb', line 11

def template_path(file)
  Pathname.new(File.join(File.dirname(__FILE__), '..', '..', 'template', file)).realpath
end