beer_recipe 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 041d468f9dd867b90a3cb4578ea1a585e033d904
4
+ data.tar.gz: afc83564964bbe91301631ce585b41b202b565f0
5
+ SHA512:
6
+ metadata.gz: eeb35af41305b7daa1c1916e0478ebf8e1f9d8c9c3ad462e67a6b9688b623e5b50806f2d253f688ee23ea8c8980129d0aa03356726bdf2b90247392725ab1fb0
7
+ data.tar.gz: 8b3a4f55a1bef64ea1b8a02341605f7fce27825bd07ef541a30657f487d12160c87e32f750ec47e48db09c9c13aa677dcad1dbabfeca45b5b92546f4f58ad8cb
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ require 'beer_recipe'
6
+
7
+ require "docopt"
8
+ doc = <<DOCOPT
9
+ Beer Recipe
10
+
11
+ Usage:
12
+ #{__FILE__} <beer.xml> [--format=<format>]
13
+ #{__FILE__} -h | --help
14
+ #{__FILE__} --version
15
+
16
+ Options:
17
+ -h --help Show this screen.
18
+ --version Show version.
19
+ --format=<format> Output format [default: html].
20
+
21
+ DOCOPT
22
+
23
+ begin
24
+ opts = Docopt::docopt(doc, version: "BeerRecipe v#{BeerRecipe::VERSION}")
25
+ formatter = if opts['--format'].downcase.start_with? 't'
26
+ BeerRecipe::TextFormatter.new
27
+ else
28
+ BeerRecipe::HtmlFormatter.new
29
+ end
30
+ BeerRecipe::Reader.new(file: ARGV[0], formatter: formatter).read.output
31
+ rescue Docopt::Exit => e
32
+ puts e.message
33
+ end
@@ -0,0 +1,17 @@
1
+ require 'nrb/beerxml'
2
+ require 'erb'
3
+
4
+ module BeerRecipe
5
+ end
6
+
7
+ require 'beer_recipe/reader'
8
+ require 'beer_recipe/wrapper'
9
+ require 'beer_recipe/mash_wrapper'
10
+ require 'beer_recipe/mash_step_wrapper'
11
+ require 'beer_recipe/hop_wrapper'
12
+ require 'beer_recipe/yeast_wrapper'
13
+ require 'beer_recipe/fermentable_wrapper'
14
+ require 'beer_recipe/recipe_wrapper'
15
+ require 'beer_recipe/recipe_formatter'
16
+ require 'beer_recipe/html_formatter'
17
+ require 'beer_recipe/text_formatter'
@@ -0,0 +1,6 @@
1
+ class BeerRecipe::FermentableWrapper < BeerRecipe::Wrapper
2
+ def formatted_amount
3
+ "#{'%.2f' % @record.amount} kg"
4
+ end
5
+ end
6
+
@@ -0,0 +1,37 @@
1
+ class BeerRecipe::HopWrapper < BeerRecipe::Wrapper
2
+ attr_accessor :ibu
3
+
4
+ def amount
5
+ @record.amount * 1000
6
+ end
7
+
8
+ def ibu
9
+ # IBU = (Alfasyrahalt) X (Humlemängd) X (Koktid) X (3) / (Bryggvolym)
10
+ ( @record.alpha * amount * boil_time * 3 ) / @recipe.batch_size
11
+ end
12
+
13
+ def boil_time
14
+ if @record.use == 'Dry Hop'
15
+ 0
16
+ else
17
+ @record.time / 60
18
+ end
19
+ end
20
+
21
+ def formatted_time
22
+ if @record.use == 'Dry Hop'
23
+ "#{'%.0f' % (@record.time / 1440)} days"
24
+ else
25
+ "#{'%.0f' % @record.time} min"
26
+ end
27
+ end
28
+
29
+ def formatted_amount
30
+ "#{'%.0f' % amount} gr"
31
+ end
32
+
33
+ def formatted_ibu
34
+ "#{'%.2f' % ibu} IBU"
35
+ end
36
+ end
37
+
@@ -0,0 +1,11 @@
1
+ class BeerRecipe::HtmlFormatter < BeerRecipe::RecipeFormatter
2
+ def output
3
+ puts parse.read
4
+ end
5
+
6
+ def template_path
7
+ 'template/html.erb'
8
+ end
9
+
10
+ end
11
+
@@ -0,0 +1,10 @@
1
+ class BeerRecipe::Mash_stepWrapper < BeerRecipe::Wrapper
2
+ def formatted_step_temp
3
+ "#{'%.0f' % step_temp}°C"
4
+ end
5
+
6
+ def formatted_step_time
7
+ "#{'%.0f' % step_time} min"
8
+ end
9
+ end
10
+
@@ -0,0 +1,14 @@
1
+ class BeerRecipe::MashWrapper < BeerRecipe::Wrapper
2
+
3
+ def initialize(record, recipe=nil)
4
+ @record = record
5
+ @recipe = recipe
6
+ end
7
+
8
+ def steps
9
+ @steps ||= @record.mash_steps.map do |step|
10
+ BeerRecipe::Wrapper.wrap(step, @recipe)
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,18 @@
1
+ class BeerRecipe::Reader
2
+ attr_accessor :parser, :recipe
3
+
4
+ def initialize(options = {})
5
+ @options = options
6
+ end
7
+
8
+ def read
9
+ @parser ||= NRB::BeerXML::Parser.new
10
+ @beerxml = parser.parse @options[:file]
11
+ self
12
+ end
13
+
14
+ def output
15
+ @options[:formatter].format(BeerRecipe::RecipeWrapper.new(@beerxml.records.first)).output
16
+ end
17
+ end
18
+
@@ -0,0 +1,41 @@
1
+ class BeerRecipe::RecipeFormatter
2
+ def format(recipe)
3
+ @recipe = recipe
4
+ self
5
+ end
6
+
7
+ def template_path
8
+ ''
9
+ end
10
+
11
+ def template
12
+ IO.read(template_path)
13
+ end
14
+
15
+ def parse
16
+ erb = ERB.new(template).result(@recipe.get_binding)
17
+ StringIO.new(erb)
18
+ end
19
+
20
+ def output
21
+ format_recipe
22
+ BeerRecipe::RecipeWrapper::SETS.map do |set|
23
+ format_records(@recipe.send(set), set)
24
+ end
25
+ format_mash(@recipe.mash)
26
+ end
27
+
28
+ def format_records(records, set)
29
+ if records.size > 0
30
+ send("before_#{set}") if respond_to? "before_#{set}"
31
+ records.each do |record|
32
+ if respond_to? record.format_method
33
+ send(record.format_method, record)
34
+ end
35
+ end
36
+ send("after_#{set}") if respond_to? "after_#{set}"
37
+ end
38
+ end
39
+
40
+ end
41
+
@@ -0,0 +1,23 @@
1
+ class BeerRecipe::Reader
2
+ attr_accessor :parser, :recipe
3
+
4
+ def initialize(options = {})
5
+ @options = options
6
+ end
7
+
8
+ def read
9
+ @parser ||= NRB::BeerXML::Parser.new
10
+ @beerxml = parser.parse @options[:file]
11
+ self
12
+ end
13
+
14
+ def recipe
15
+ BeerRecipe::RecipeWrapper.new(@beerxml.records.first)
16
+ end
17
+
18
+ def output
19
+ @options[:formatter].format(recipe)
20
+ self
21
+ end
22
+ end
23
+
@@ -0,0 +1,54 @@
1
+ class BeerRecipe::RecipeWrapper < BeerRecipe::Wrapper
2
+ SETS = %i(fermentables hops miscs waters yeasts)
3
+
4
+ def initialize(record, recipe=nil)
5
+ super
6
+ @sets = {}
7
+ end
8
+
9
+ def recipe
10
+ @record
11
+ end
12
+
13
+ def get_binding
14
+ binding
15
+ end
16
+
17
+ def method_missing(method, *args, &block)
18
+ if SETS.include?(method)
19
+ @sets[method] ||= BeerRecipe::Wrapper.set(recipe, method)
20
+ else
21
+ @record.send(method, *args, &block)
22
+ end
23
+ end
24
+
25
+ def respond_to_missing?(name, flag = true)
26
+ SETS.include?(method) || @record.respond_to?(name) || super
27
+ end
28
+
29
+ def mash
30
+ @mash ||= BeerRecipe::MashWrapper.new(recipe.mash, self)
31
+ end
32
+
33
+ def abv
34
+ return @abv if @abv
35
+ og = recipe.og
36
+ fg = recipe.fg
37
+ @abv = if og > 0 && fg > 0
38
+ ( (76.08 * (og - fg) / (1.775 - og) ) * (fg / 0.794) )
39
+ else
40
+ 0
41
+ end
42
+ end
43
+
44
+ def ibu
45
+ return @ibu if @ibu
46
+ @ibu = 0
47
+ hops.each do |hop|
48
+ @ibu += hop.ibu
49
+ end
50
+ @ibu
51
+ end
52
+
53
+ end
54
+
@@ -0,0 +1,49 @@
1
+ class BeerRecipe::TextFormatter < BeerRecipe::RecipeFormatter
2
+ def format_recipe
3
+ puts "Name: #{@recipe.name}"
4
+ puts "Type: #{@recipe.type}"
5
+ puts "Style: #{@recipe.style.name}"
6
+ puts "Batch size: #{'%.0f' % @recipe.batch_size} L"
7
+ puts "Boil time: #{'%.0f' % @recipe.boil_time} min"
8
+ puts "OG: #{@recipe.og}"
9
+ puts "FG: #{@recipe.fg}"
10
+ puts "ABV: #{'%.2f' % @recipe.abv}%"
11
+ puts "IBU: #{@recipe.ibu}"
12
+ end
13
+
14
+ def format_mash(mash)
15
+ puts "\nMash:"
16
+ puts mash.name
17
+ puts
18
+ format_records(mash.steps, :mashstep)
19
+ end
20
+
21
+ def format_mash_step(m)
22
+ puts "#{m.name}\t#{m.type}\t#{'%.2f' % m.infuse_amount} L\t#{m.formatted_step_time}\t#{m.formatted_step_temp}"
23
+ end
24
+
25
+ def before_fermentables
26
+ puts "\nFermentables:"
27
+ end
28
+
29
+ def format_fermentable(f)
30
+ puts "#{f.formatted_amount}\t #{f.name}"
31
+ end
32
+
33
+ def before_hops
34
+ puts "\nHops:"
35
+ end
36
+
37
+ def format_hop(h)
38
+ puts "#{h.formatted_amount}\t#{h.name} (#{h.form})\t#{h.use}\t#{h.formatted_time}\t#{h.formatted_ibu}"
39
+ end
40
+
41
+ def before_yeasts
42
+ puts "\nYeasts:"
43
+ end
44
+
45
+ def format_yeast(y)
46
+ puts "#{y.name}\t#{y.product_id}\t#{y.laboratory}\t#{y.form}"
47
+ end
48
+
49
+ end
@@ -0,0 +1,3 @@
1
+ module BeerRecipe
2
+ VERSION = '0.2.0'
3
+ end
@@ -0,0 +1,32 @@
1
+ class BeerRecipe::Wrapper
2
+ def initialize(record, recipe=nil)
3
+ @record = record
4
+ @recipe = recipe
5
+ end
6
+
7
+ def method_missing(method, *args, &block)
8
+ @record.send(method, *args, &block)
9
+ end
10
+
11
+ def respond_to_missing?(name, flag = true)
12
+ @record.respond_to?(name) || super
13
+ end
14
+
15
+ def format_method
16
+ "format_#{record_type}".to_sym
17
+ end
18
+
19
+ def self.set(recipe, set)
20
+ recipe.send(set).map { |record| self.wrap(record, recipe) }
21
+ end
22
+
23
+ def self.wrap(record, recipe)
24
+ wrapper = "#{record.record_type.capitalize}Wrapper".to_sym
25
+ begin
26
+ return BeerRecipe.const_get(wrapper).new(record, recipe)
27
+ rescue NameError
28
+ return self.new(record, recipe)
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,11 @@
1
+ class BeerRecipe::YeastWrapper < BeerRecipe::Wrapper
2
+ def formatted_attenuation
3
+ "#{'%.0f' % attenuation}%"
4
+ end
5
+
6
+ def formatted_temperatures
7
+ "#{'%.0f' % min_temperature}°C - #{'%.0f' % max_temperature}°C"
8
+ end
9
+ end
10
+
11
+
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beer_recipe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Olle Johansson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nrb-beerxml
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: docopt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.4'
69
+ description:
70
+ email: Olle@Johansson.com
71
+ executables:
72
+ - beer_recipe
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - bin/beer_recipe
77
+ - lib/beer_recipe.rb
78
+ - lib/beer_recipe/fermentable_wrapper.rb
79
+ - lib/beer_recipe/hop_wrapper.rb
80
+ - lib/beer_recipe/html_formatter.rb
81
+ - lib/beer_recipe/mash_step_wrapper.rb
82
+ - lib/beer_recipe/mash_wrapper.rb
83
+ - lib/beer_recipe/reader.rb
84
+ - lib/beer_recipe/recipe_formatter.rb
85
+ - lib/beer_recipe/recipe_reader.rb
86
+ - lib/beer_recipe/recipe_wrapper.rb
87
+ - lib/beer_recipe/text_formatter.rb
88
+ - lib/beer_recipe/version.rb
89
+ - lib/beer_recipe/wrapper.rb
90
+ - lib/beer_recipe/yeast_wrapper.rb
91
+ homepage: https://github.com/ollej/beer_recipe
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Simple Beer XML recipe formatter.
115
+ test_files: []