Class: BeerRecipe::Reader
- Inherits:
-
Object
- Object
- BeerRecipe::Reader
- Defined in:
- lib/beer_recipe/reader.rb
Instance Attribute Summary collapse
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#recipe ⇒ Object
Returns the value of attribute recipe.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Reader
constructor
A new instance of Reader.
- #parse ⇒ Object
- #read ⇒ Object
- #setup_translation ⇒ Object
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[:recipe_wrapper] ||= BeerRecipe::RecipeWrapper @options[:formatter] ||= BeerRecipe::HtmlFormatter setup_translation end |
Instance Attribute Details
#parser ⇒ Object
Returns the value of attribute parser.
2 3 4 |
# File 'lib/beer_recipe/reader.rb', line 2 def parser @parser end |
#recipe ⇒ Object
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.() opts = {} opts[:formatter] = if .fetch('--format', 'html').downcase.start_with? 't' BeerRecipe::TextFormatter else BeerRecipe::HtmlFormatter end opts[:template] = ['--template'] if ['--template'] opts[:file] = ['--file'] if ['--file'] opts[:language] = ['--language'] || 'locales/en.yml' opts end |
Instance Method Details
#parse ⇒ Object
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 |
#read ⇒ Object
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_translation ⇒ Object
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 |