beerdb 0.9.13 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,112 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- module BeerDb
4
-
5
-
6
- class ZipReader < ReaderBase
7
-
8
- def initialize( name, include_path, opts = {} )
9
-
10
- ## todo/fix: make include_path an opts (included in opts?) - why? why not??
11
-
12
- path = "#{include_path}/#{name}.zip"
13
- ## todo: check if zip exists
14
-
15
- @zip_file = Zip::File.open( path ) ## NOTE: do NOT create if file is missing; let it crash
16
-
17
- ### allow prefix (path) in name
18
- ### e.g. assume all files relative to setup manifest
19
- ## e.g. at-austria-master/setups/all.txt or
20
- ## be-belgium-master/setups/all.txt
21
- ## for
22
- ## setups/all.txt
23
- ###
24
- ## will get (re)set w/ fixture/setup reader
25
- ##
26
- ## todo/fix: change/rename to @relative_path ?? - why? why not?
27
- @zip_prefix = ''
28
- end
29
-
30
-
31
- def close
32
- ## todo/check: add a close method - why? why not ???
33
- @zip_file.close
34
- end
35
-
36
-
37
- def create_fixture_reader( name )
38
- ## e.g. pass in => setups/all or setups/test etc. e.g. w/o .txt extension
39
- query = "**/#{name}.txt"
40
-
41
- ## note: returns an array of Zip::Entry
42
- candidates = @zip_file.glob( query )
43
- pp candidates
44
-
45
- ## use first candidates entry as match
46
- ## todo/fix: issue warning if more than one entries/matches!!
47
-
48
- ## get fullpath e.g. at-austria-master/setups/all.txt
49
- path = candidates[0].name
50
- logger.debug " zip entry path >>#{path}<<"
51
-
52
- ## cut-off at-austria-master/ NOTE: includes trailing slash (if present)
53
- ## logger.debug " path.size #{path.size} >>#{path}<<"
54
- ## logger.debug " name.size #{name.size+4} >>#{name}<<"
55
-
56
- ## note: add +4 for extension (.txt)
57
- @zip_prefix = path[ 0...(path.size-(name.size+4)) ]
58
- logger.debug " zip entry prefix >>#{@zip_prefix}<<"
59
-
60
- logger.info "parsing data in zip '#{name}' (#{path})..."
61
-
62
- FixtureReader.from_zip( @zip_file, path )
63
- end
64
-
65
-
66
- def create_beers_reader( name, more_attribs={} )
67
- path = name_to_zip_entry_path( name )
68
-
69
- logger.debug "parsing data (beers) in zip '#{name}' (#{path})..."
70
-
71
- ValuesReader.from_zip( @zip_file, path, more_attribs )
72
- end
73
-
74
- def create_breweries_reader( name, more_attribs={} )
75
- path = name_to_zip_entry_path( name )
76
-
77
- logger.debug "parsing data (breweries) in zip '#{name}' (#{path})..."
78
-
79
- ValuesReader.from_zip( @zip_file, path, more_attribs )
80
- end
81
-
82
- private
83
-
84
- def path_to_real_path( path )
85
- # map name to name_real_path
86
- # name might include !/ for virtual path (gets cut off)
87
- # e.g. at-austria!/w-wien/beers becomse w-wien/beers
88
- pos = path.index( '!/')
89
- if pos.nil?
90
- path # not found; real path is the same as name
91
- else
92
- # cut off everything until !/ e.g.
93
- # at-austria!/w-wien/beers becomes
94
- # w-wien/beers
95
- path[ (pos+2)..-1 ]
96
- end
97
- end
98
-
99
- def name_to_zip_entry_path( name )
100
- path = "#{name}.txt"
101
-
102
- real_path = path_to_real_path( path )
103
-
104
- # NOTE: add possible zip entry prefix path
105
- # (if present includes trailing slash e.g. /)
106
- entry_path = "#{@zip_prefix}#{real_path}"
107
- entry_path
108
- end
109
-
110
-
111
- end # class ZipReader
112
- end # module BeerDb
data/lib/beerdb/schema.rb DELETED
@@ -1,195 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- module BeerDb
4
-
5
-
6
- class CreateDb < ActiveRecord::Migration
7
-
8
- def up
9
-
10
- create_table :beers do |t|
11
- t.string :key, :null => false # import/export key
12
- t.string :title, :null => false
13
- t.string :synonyms # comma separated list of synonyms
14
-
15
- t.string :web # optional url link (e.g. )
16
- t.integer :since # optional year (e.g. 1896)
17
-
18
- # t.boolean :bottle, :null => false, :default => false # Flaschenbier
19
- # t.boolean :draft, :null => false, :default => false # Fassbier
20
- ## todo: check seasonal is it proper english?
21
- t.boolean :seasonal, :null => false, :default => false # all year or just eg. Festbier/Oktoberfest Special
22
- t.boolean :limited, :null => false, :default => false # one year or season only
23
- ## todo: add microbrew/brewpub flag?
24
- #### t.boolean :brewpub, :null => false, :default => false
25
-
26
- ## add t.boolean :lite flag ??
27
- t.decimal :kcal # kcal/100ml e.g. 45.0 kcal/100ml
28
-
29
- ## check: why decimal and not float?
30
- t.decimal :abv # Alcohol by volume (abbreviated as ABV, abv, or alc/vol) e.g. 4.9 %
31
- t.decimal :og # malt extract (original gravity) in plato
32
- t.integer :srm # color in srm
33
- t.integer :ibu # bitterness in ibu
34
-
35
- ### fix/todo: add bitterness field
36
-
37
- # renamed - old field names
38
- ## t.decimal :plato # stammwuerze / gravity in plato scale (e.g. °P) e.g. 12.6° - todo: use a different field name e.g. just p or gravity?
39
- ## t.integer :color # beer color in Standard Reference Method (SRM)
40
-
41
- # see en.wikipedia.org/wiki/Plato_scale#Colour
42
-
43
- # SRM/Lovibond | Example | Beer color | EBC
44
- # ---------------------------------------------------------------
45
- # 2 | Pale lager, Witbier, Pilsener, Berliner Weisse | #F8F753 | 4
46
- # 3 | Maibock, Blonde Ale | #F6F513 | 6
47
- # 4 | Weissbier | #ECE61A | 8
48
- # 6 | American Pale Ale, India Pale Ale | #D5BC26 | 12
49
- # 8 | Weissbier, Saison | #BF923B | 16
50
- # 10 | English Bitter, ESB | #BF813A | 20
51
- # 13 | Biere de Garde, Double IPA | #BC6733 | 26
52
- # 17 | Dark lager, Vienna lager, Marzen, Amber Ale | #8D4C32 | 33
53
- # 20 | Brown Ale, Bock, Dunkel, Dunkelweizen | #5D341A | 39
54
- # 24 | Irish Dry Stout, Doppelbock, Porter | #261716 | 47
55
- # 29 | Stout | #0F0B0A | 57
56
- # 35 | Foreign Stout, Baltic Porter | #080707 | 69
57
- # 40+ | Imperial Stout | #030403 | 79
58
-
59
- t.references :brewery # optional (for now)
60
- t.references :brand # optional (for now)
61
-
62
-
63
- ## todo: add categories e.g. (A/B/C, 1/2/3, main/major/minor ??)
64
- # - A-grade /1st class/ tier1 / main beer brand/bestseller/flagship ?
65
- # - B-grade /2nd class/ tier2 / regular, major, - todo: find better names?
66
- # - C-grade /3nd class/ tier3/ / speciality, minor ?
67
-
68
- # use stars in .txt e.g. # ***/**/*/- => 1/2/3/4
69
- t.integer :grade, :null => false, :default => 4
70
-
71
- t.string :txt # source ref
72
- t.boolean :txt_auto, :null => false, :default => false # inline? got auto-added?
73
-
74
-
75
- t.references :country, :null => false
76
- t.references :region # optional
77
- t.references :city # optional
78
-
79
- t.timestamps
80
- end
81
-
82
-
83
- create_table :brands do |t| # beer families (sharing same name e.g. brand)
84
- t.string :key, :null => false # import/export key
85
- t.string :title, :null => false
86
- t.string :synonyms # comma separated list of synonyms
87
- t.string :web # optional web page (e.g. www.ottakringer.at)
88
- t.string :wiki # optional wiki(pedia page)
89
- t.integer :since
90
-
91
- ## scope of brand (global/intern'l/national/regional/local) ??
92
- t.boolean :global, :null => false, :default => false
93
- t.boolean :internl, :null => false, :default => false
94
- t.boolean :national, :null => false, :default => false
95
- t.boolean :regional, :null => false, :default => false
96
- t.boolean :local, :null => false, :default => false
97
-
98
- # t.integer :brand_grade # 1/2/3/4/5 (global/intern'l/national/regional/local)
99
-
100
- # use stars in .txt e.g. # ***/**/*/- => 1/2/3/4
101
- t.integer :grade, :null => false, :default => 4
102
- # -- todo: add plus 1 for brewery w/ *** ??
103
-
104
- t.string :txt # source ref
105
- t.boolean :txt_auto, :null => false, :default => false # inline? got auto-added?
106
-
107
-
108
- t.references :brewery # optional (for now)
109
-
110
- t.references :country, :null => false
111
- t.references :region # optional
112
- t.references :city # optional
113
-
114
- t.timestamps
115
- end
116
-
117
- create_table :breweries do |t|
118
- t.string :key, :null => false # import/export key
119
- t.string :title, :null => false
120
- t.string :synonyms # comma separated list of synonyms
121
- t.string :address
122
- t.integer :since
123
- ### fix: rename back to founded or use opened/closed
124
- ## fix: add flag for ca./about boolean opened_guess / opened_est / opened_??
125
- ## ca. / about 1010 marker e.g t.boolean : opened_est (for estimate) or similar!!!
126
- ## renamed to founded to since
127
- ## t.integer :founded # year founded/established - todo/fix: rename to since?
128
- t.integer :closed # optional; year brewery closed
129
-
130
- ## todo: add optional parent brewery (owned_by) ???
131
-
132
- t.boolean :brewpub, :null => false, :default => false
133
- t.boolean :prod_m, :null => false, :default => false # prod medium (mid-size/regional brewery)
134
- # e.g. > 15_000 barrels (us)
135
- t.boolean :prod_l, :null => false, :default => false # prod large
136
- # e.g. > 500_000 hl (at), > 6_000_000 barrels (us)
137
-
138
- t.integer :prod # (estimated) annual production/capacity in hl (1hl=100l) e.g. megabrewery 2_000_000, microbrewery 1_000 hl; brewbup 500 hl etc.
139
- t.integer :prod_grade # 1/2/3/4/5/6/7/8/9/10/11
140
-
141
- # grade - classified using annual production (capacity) in hl
142
- # < 1_000 hl => 11
143
- # < 3_000 hl => 10
144
- # < 5_000 hl => 9
145
- # < 10_000 hl => 8
146
- # < 50_000 hl => 7
147
- # < 100_000 hl => 6
148
- # < 200_000 hl => 5
149
- # < 500_000 hl => 4
150
- # < 1_000_000 hl => 3
151
- # < 2_000_000 hl => 2
152
- # > 2_000_000 hl => 1
153
-
154
-
155
- # use stars in .txt e.g. # ***/**/*/- => 1/2/3/4
156
- t.integer :grade, :null => false, :default => 4
157
-
158
-
159
- t.string :txt # source ref
160
- t.boolean :txt_auto, :null => false, :default => false # inline? got auto-added?
161
-
162
- t.string :web # optional web page (e.g. www.ottakringer.at)
163
- t.string :wikipedia # optional wiki(pedia page)
164
-
165
- t.boolean :indie # independent brewery (flag)
166
-
167
- # for convenience (easy queries) use flags for top beer multinationals (-- later use just tags? more flexible)
168
- t.boolean :abinbev # owned by AB InBev / Anheuser-Busch InBev (and Grupo Modelo)
169
- t.boolean :sabmiller # owned by SAB Miller (in US cooperates w/ Molson Coors using MillerCoors venture)
170
- t.boolean :heineken # owned by Heineken
171
- t.boolean :carlsberg # owned by Carlsberg
172
- t.boolean :molsoncoors # owned by Molson Coors
173
- t.boolean :diageo # owned by Diageo (e.g. Guiness, Kilkenny,...)
174
-
175
-
176
- # todo: add t.references :parent # for parent brewery
177
- # (or better use has many parents w/ percentage of ownership; might not be 100%)
178
-
179
- t.references :country, :null => false
180
- t.references :region # optional
181
- t.references :city # optional
182
-
183
- t.timestamps
184
- end
185
-
186
- end # method up
187
-
188
- def down
189
- raise ActiveRecord::IrreversibleMigration
190
- end
191
-
192
-
193
- end # class CreateDb
194
-
195
- end # module BeerDb
@@ -1,45 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- module BeerDb::Model
4
-
5
- class BeerSerializer
6
-
7
- def initialize( beer )
8
- @beer = beer
9
- end
10
-
11
- attr_reader :beer
12
-
13
- def as_json
14
- brewery = {}
15
- if beer.brewery.present?
16
- brewery = { key: beer.brewery.key,
17
- title: beer.brewery.title }
18
- end
19
-
20
- tags = []
21
- if beer.tags.present?
22
- beer.tags.each { |tag| tags << tag.key }
23
- end
24
-
25
- country = {
26
- key: beer.country.key,
27
- title: beer.country.title
28
- }
29
-
30
- data = { key: beer.key,
31
- title: beer.title,
32
- synonyms: beer.synonyms,
33
- abv: beer.abv,
34
- srm: beer.srm,
35
- og: beer.og,
36
- tags: tags,
37
- brewery: brewery,
38
- country: country }
39
-
40
- data.to_json
41
- end
42
-
43
- end # class BeerSerializer
44
-
45
- end # module BeerDb::Model
@@ -1,46 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- module BeerDb::Model
4
-
5
- class BrewerySerializer
6
-
7
- def initialize( brewery )
8
- @brewery = brewery
9
- end
10
-
11
- attr_reader :brewery
12
-
13
- def as_json
14
-
15
- beers = []
16
- brewery.beers.each do |b|
17
- beers << { key: b.key, title: b.title }
18
- end
19
-
20
- tags = []
21
- if brewery.tags.present?
22
- brewery.tags.each { |tag| tags << tag.key }
23
- end
24
-
25
- country = {
26
- key: brewery.country.key,
27
- title: brewery.country.title
28
- }
29
-
30
- data = { key: brewery.key,
31
- title: brewery.title,
32
- synonyms: brewery.synonyms,
33
- since: brewery.since,
34
- address: brewery.address,
35
- web: brewery.web,
36
- prod: brewery.prod, # (estimated) annual production in hl e.g. 2_000 hl
37
- tags: tags,
38
- beers: beers,
39
- country: country }
40
-
41
- data.to_json
42
- end
43
-
44
- end # class BrewerySerializer
45
-
46
- end # module BeerDb::Model
data/lib/beerdb/stats.rb DELETED
@@ -1,27 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module BeerDb
4
-
5
- class Stats
6
- include Models
7
-
8
- def tables
9
- puts "Stats:"
10
- puts " #{'%5d' % Beer.count} beers"
11
- puts " #{'%5d' % Brand.count} brands"
12
- puts " #{'%5d' % Brewery.count} breweries"
13
- puts
14
-
15
- ### fix: use if defined? BeerDbNote or similar or/and check if table exist ??
16
- ### or move to beerdb-note ??
17
-
18
- # puts " #{'%5d' % User.count} users" # db model extension - move to its own addon?
19
- # puts " #{'%5d' % Bookmark.count} bookmarks" # db model extension - move to its own addon?
20
- # puts " #{'%5d' % Drink.count} drinks" # db model extension - move to its own addon?
21
- # puts " #{'%5d' % Note.count} notes" # db model extension - move to its own addon?
22
- end
23
-
24
- end # class Stats
25
-
26
- end # module BeerDb
27
-
data/test/helper.rb DELETED
@@ -1,72 +0,0 @@
1
-
2
- ## $:.unshift(File.dirname(__FILE__))
3
-
4
- ## minitest setup
5
-
6
- # require 'minitest/unit'
7
- require 'minitest/autorun'
8
-
9
- # include MiniTest::Unit # lets us use TestCase instead of MiniTest::Unit::TestCase
10
-
11
-
12
- # our own code
13
-
14
- require 'beerdb'
15
- require 'logutils/db' # NOTE: explict require required for LogDb (not automatic)
16
-
17
-
18
- Country = WorldDb::Model::Country
19
- Region = WorldDb::Model::Region
20
-
21
- ## todo: get all models aliases (e.g. from console script)
22
-
23
- Beer = BeerDb::Model::Beer
24
- Brand = BeerDb::Model::Brand
25
- Brewery = BeerDb::Model::Brewery
26
-
27
-
28
- def setup_in_memory_db
29
- # Database Setup & Config
30
-
31
- db_config = {
32
- adapter: 'sqlite3',
33
- database: ':memory:'
34
- }
35
-
36
- pp db_config
37
-
38
- ActiveRecord::Base.logger = Logger.new( STDOUT )
39
- ## ActiveRecord::Base.colorize_logging = false - no longer exists - check new api/config setting?
40
-
41
- ## NB: every connect will create a new empty in memory db
42
- ActiveRecord::Base.establish_connection( db_config )
43
-
44
-
45
- ## build schema
46
-
47
- LogDb.create
48
- ConfDb.create
49
- TagDb.create
50
- WorldDb.create
51
- BeerDb.create
52
- end
53
-
54
- def fillup_in_memory_db
55
- ## add some counties
56
-
57
- at = Country.create!( key: 'at', title: 'Austria', code: 'AUT', pop: 0, area: 0 )
58
- Region.create!( key: 'w', title: 'Wien', country_id: at.id )
59
-
60
- de = Country.create!( key: 'de', title: 'Germany', code: 'DEU', pop: 0, area: 0 )
61
- Region.create!( key: 'by', title: 'Bayern', country_id: de.id )
62
-
63
- end
64
-
65
- setup_in_memory_db()
66
- fillup_in_memory_db()
67
-
68
- AT = Country.find_by_key!( 'at' )
69
- W = Region.find_by_key!( 'w' )
70
-
71
- DE = Country.find_by_key!( 'de' )
72
- BY = Region.find_by_key!( 'by' )
@@ -1,85 +0,0 @@
1
- # encoding: utf-8
2
-
3
-
4
- require 'helper'
5
-
6
-
7
- class TestFixtureMatchers < MiniTest::Unit::TestCase
8
-
9
- include WorldDb::Matcher
10
- include BeerDb::Matcher
11
-
12
-
13
- def test_country
14
-
15
- beers_at = [
16
- 'europe/at/beers',
17
- 'europe/at-austria/beers',
18
- 'at-austria/beers',
19
- 'at-austria!/beers',
20
- '1--at-austria--central/beers',
21
- 'europe/1--at-austria--central/beers'
22
- ]
23
-
24
- beers_at.each do |name|
25
- found = match_beers_for_country( name ) do |country_key|
26
- assert( country_key == 'at')
27
- end
28
- assert( found == true )
29
- end
30
-
31
- breweries_at = [
32
- 'europe/at/breweries',
33
- 'europe/at-austria/breweries',
34
- 'at-austria/breweries',
35
- 'at-austria!/breweries',
36
- '1--at-austria--central/breweries',
37
- 'europe/1--at-austria--central/breweries'
38
- ]
39
-
40
- breweries_at.each do |name|
41
- found = match_breweries_for_country( name ) do |country_key|
42
- assert( country_key == 'at')
43
- end
44
- assert( found == true )
45
- end
46
- end # method test_country
47
-
48
-
49
- def test_country_n_region
50
-
51
- beers_at = [
52
- 'europe/at-austria/w-wien/beers',
53
- 'at-austria/w-wien/beers',
54
- 'at-austria!/w-wien/beers',
55
- '1--at-austria--central/1--w-wien--eastern/beers',
56
- 'europe/1--at-austria--central/1--w-wien--eastern/beers'
57
- ]
58
-
59
- beers_at.each do |name|
60
- found = match_beers_for_country_n_region( name ) do |country_key,region_key|
61
- assert( country_key == 'at')
62
- assert( region_key == 'w' )
63
- end
64
- assert( found == true )
65
- end
66
-
67
- breweries_at = [
68
- 'europe/at-austria/w-wien/breweries',
69
- 'at-austria/w-wien/breweries',
70
- 'at-austria!/w-wien/breweries',
71
- '1--at-austria--central/1--w-wien--eastern/breweries',
72
- 'europe/1--at-austria--central/1--w-wien--eastern/breweries'
73
- ]
74
-
75
- breweries_at.each do |name|
76
- found = match_breweries_for_country_n_region( name ) do |country_key,region_key|
77
- assert( country_key == 'at')
78
- assert( region_key == 'w' )
79
- end
80
- assert( found == true )
81
- end
82
- end # method test_country_n_region
83
-
84
-
85
- end # class TestFixtureMatchers