Class: BeerRecipe::Reader

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Reader

Returns a new instance of Reader.



4
5
6
7
8
9
# File 'lib/beer_recipe/reader.rb', line 4

def initialize(options = {})
  @options = options
  @options[:recipe_wrapper] ||= BeerRecipe::RecipeWrapper
  @options[:formatter] ||= BeerRecipe::HtmlFormatter
  setup_translation
end

Instance Attribute Details

#parserObject

Returns the value of attribute parser.



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

def parser
  @parser
end

#recipeObject

Returns the value of attribute recipe.



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

def recipe
  @recipe
end

Class Method Details

.parse_options(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/beer_recipe/reader.rb', line 15

def self.parse_options(options)
  opts = {}
  opts[:formatter] = if options.fetch('--format', 'html').downcase.start_with? 't'
    BeerRecipe::TextFormatter
  else
    BeerRecipe::HtmlFormatter
  end
  opts[:template] = options['--template'] if options['--template']
  opts[:file] = options['--file'] if options['--file']
  opts[:language] = options['--language'] || 'locales/en.yml'
  opts
end

Instance Method Details

#parseObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/beer_recipe/reader.rb', line 34

def parse
  raise BeerRecipe::ParseError if @beerxml.nil?
  @beerxml.records.each do |record|
    recipe = @options[:recipe_wrapper].new(record)
    begin
      @options[:formatter].new(@options).format(recipe).output
    rescue NoMethodError => e
      raise BeerRecipe::FormatError.new e
    end
  end
end

#readObject



28
29
30
31
32
# File 'lib/beer_recipe/reader.rb', line 28

def read
  @parser ||= NRB::BeerXML::Parser.new
  @beerxml = @parser.parse @options.fetch(:file, STDIN)
  self
end

#setup_translationObject



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

def setup_translation
  I18n.backend.store_translations(:en, YAML.load(File.read(@options[:language])))
end