Class: BeerDb::ReaderBase
- Inherits:
-
Object
- Object
- BeerDb::ReaderBase
show all
- Includes:
- Matcher, Models, LogUtils::Logging, WorldDb::Matcher
- Defined in:
- lib/beerdb/reader.rb
Instance Method Summary
collapse
-
#load(name) ⇒ Object
-
#load_beers_for_country(country_key, name, more_attribs = {}) ⇒ Object
-
#load_beers_for_country_n_region(country_key, region_key, name, more_attribs = {}) ⇒ Object
-
#load_beers_worker(name, more_attribs = {}) ⇒ Object
-
#load_breweries_for_country(country_key, name, more_attribs = {}) ⇒ Object
-
#load_breweries_for_country_n_region(country_key, region_key, name, more_attribs = {}) ⇒ Object
-
#load_breweries_worker(name, more_attribs = {}) ⇒ Object
-
#load_setup(name) ⇒ Object
Methods included from Matcher
#match_beers_for_country, #match_beers_for_country_n_region, #match_breweries_for_country, #match_breweries_for_country_n_region, #match_brewpubs_for_country, #match_brewpubs_for_country_n_region
Instance Method Details
#load(name) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/beerdb/reader.rb', line 56
def load( name )
if match_beers_for_country_n_region( name ) do |country_key, region_key|
load_beers_for_country_n_region( country_key, region_key, name )
end
elsif match_beers_for_country( name ) do |country_key|
load_beers_for_country( country_key, name )
end
elsif match_breweries_for_country_n_region( name ) do |country_key, region_key|
load_breweries_for_country_n_region( country_key, region_key, name )
end
elsif match_breweries_for_country( name ) do |country_key|
load_breweries_for_country( country_key, name )
end
elsif match_brewpubs_for_country_n_region( name ) do |country_key, region_key|
load_breweries_for_country_n_region( country_key, region_key, name, brewpub: true )
end
elsif match_brewpubs_for_country( name ) do |country_key|
load_breweries_for_country( country_key, name, brewpub: true )
end
else
logger.error "unknown beer.db fixture type >#{name}<"
end
end
|
#load_beers_for_country(country_key, name, more_attribs = {}) ⇒ Object
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/beerdb/reader.rb', line 103
def load_beers_for_country( country_key, name, more_attribs={} )
country = Country.find_by_key!( country_key )
logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"
more_attribs[ :country_id ] = country.id
more_attribs[ :txt ] = name
load_beers_worker( name, more_attribs )
end
|
#load_beers_for_country_n_region(country_key, region_key, name, more_attribs = {}) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/beerdb/reader.rb', line 83
def load_beers_for_country_n_region( country_key, region_key, name, more_attribs={} )
country = Country.find_by_key!( country_key )
logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"
more_attribs[ :country_id ] = country.id
region = Region.find_by_key_and_country_id( region_key, country.id )
if region.nil?
logger.warn "Region w/ key >#{region_key}< not found; skip adding region"
else
logger.debug "Region #{region.key} >#{region.title}<"
more_attribs[ :region_id ] = region.id
end
more_attribs[ :txt ] = name
load_beers_worker( name, more_attribs )
end
|
#load_beers_worker(name, more_attribs = {}) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/beerdb/reader.rb', line 115
def load_beers_worker( name, more_attribs={} )
reader = create_beers_reader( name, more_attribs )
if more_attribs[:region_id].present?
known_breweries_source = Brewery.where( region_id: more_attribs[:region_id] )
elsif more_attribs[:country_id].present?
known_breweries_source = Brewery.where( country_id: more_attribs[:country_id] )
else
logger.warn "no region or country specified; use empty brewery ary for header mapper"
known_breweries_source = []
end
known_breweries = TextUtils.build_title_table_for( known_breweries_source )
reader.each_line do |new_attributes, values|
if new_attributes[:header].present?
brewery_line = new_attributes[:header].dup new_attributes.delete(:header)
logger.debug " trying to find brewery in line >#{brewery_line}<"
TextUtils.map_titles_for!( 'brewery', brewery_line, known_breweries )
brewery_key = TextUtils.find_key_for!( 'brewery', brewery_line )
logger.debug " brewery_key = >#{brewery_key}<"
unless brewery_key.nil?
values = values.unshift "by:#{brewery_key}"
end
end
Beer.create_or_update_from_attribs( new_attributes, values )
end end
|
#load_breweries_for_country(country_key, name, more_attribs = {}) ⇒ Object
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/beerdb/reader.rb', line 180
def load_breweries_for_country( country_key, name, more_attribs={} )
country = Country.find_by_key!( country_key )
logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"
more_attribs[ :country_id ] = country.id
more_attribs[ :txt ] = name
load_breweries_worker( name, more_attribs )
end
|
#load_breweries_for_country_n_region(country_key, region_key, name, more_attribs = {}) ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/beerdb/reader.rb', line 159
def load_breweries_for_country_n_region( country_key, region_key, name, more_attribs={} )
country = Country.find_by_key!( country_key )
logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"
more_attribs[ :country_id ] = country.id
region = Region.find_by_key_and_country_id( region_key, country.id )
if region.nil?
logger.warn "Region w/ key >#{region_key}< not found; skip adding region"
else
logger.debug "Region #{region.key} >#{region.title}<"
more_attribs[ :region_id ] = region.id
end
more_attribs[ :txt ] = name
load_breweries_worker( name, more_attribs )
end
|
#load_breweries_worker(name, more_attribs = {}) ⇒ Object
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/beerdb/reader.rb', line 191
def load_breweries_worker( name, more_attribs={} )
if name =~ /\(m\)/ more_attribs[ :prod_m ] = true
elsif name =~ /\(l\)/ more_attribs[ :prod_l ] = true
else
end
reader = create_breweries_reader( name, more_attribs )
reader.each_line do |new_attributes, values|
if new_attributes[:header].present?
logger.warn "removing unused group header #{new_attributes[:header]}"
new_attributes.delete(:header) end
Brewery.create_or_update_from_attribs( new_attributes, values )
end end
|
#load_setup(name) ⇒ Object
46
47
48
49
50
51
52
|
# File 'lib/beerdb/reader.rb', line 46
def load_setup( name )
reader = create_fixture_reader( name )
reader.each do |fixture_name|
load( fixture_name )
end
end
|