Class: BeerDb::ReaderBase

Inherits:
Object
  • Object
show all
Includes:
Matcher, Models, LogUtils::Logging, WorldDb::Matcher
Defined in:
lib/beerdb/reader.rb

Direct Known Subclasses

Reader, ZipReader

Instance Method Summary collapse

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}<"
    # todo/fix: exit w/ error
  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  # store source ref

  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

  # NB: region lookup requires country id (region key only unique for country)
  region  = Region.find_by_key_and_country_id( region_key, country.id )
  if region.nil?
    # note: allow unknown region keys; issue warning n skip region
    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  # store source ref

  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 )  ### "virtual" method - required by concrete class

  ### todo: cleanup - check if [] works for build_title...
  #     better cleaner way ???
  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|

    ## note: check for header attrib; if present remove
    ### todo: cleanup code later
    ## fix: add to new_attributes hash instead of values ary
    ##   - fix: match_brewery()   move region,city code out of values loop for reuse at the end
    if new_attributes[:header].present?
      brewery_line = new_attributes[:header].dup   # note: make sure we make a copy; will use in-place string ops
      new_attributes.delete(:header)   ## note: do NOT forget to remove from hash!

      logger.debug "  trying to find brewery in line >#{brewery_line}<"
      ## todo: check what map_titles_for! returns (nothing ???)
      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?
        ## bingo! add brewery_id upfront, that is, as first value in ary
        values = values.unshift "by:#{brewery_key}"
      end
    end

    Beer.create_or_update_from_attribs( new_attributes, values )
  end # each_line
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  # store source ref

  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

  # note: region lookup requires country id (region key only unique for country)
  region  = Region.find_by_key_and_country_id( region_key, country.id )
  if region.nil?
    # note: allow unknown region keys; issue warning n skip region
    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  # store source ref

  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\)/     # check for (m) mid-size/medium marker -todo- use $?? must be last?
     more_attribs[ :prod_m ] = true
  elsif name =~ /\(l\)/  # check for (l) large marker - todo - use $?? must be last?
     more_attribs[ :prod_l ] = true
  else
    ## no marker; do nothing
  end

  reader = create_breweries_reader( name, more_attribs ) ### "virtual" method - required by concrete class

  reader.each_line do |new_attributes, values|
    
    #######
    # fix: move to (inside)
    #    Brewery.create_or_update_from_attribs ||||
    ## note: group header not used for now; do NOT forget to remove from hash!
    if new_attributes[:header].present?
      logger.warn "removing unused group header #{new_attributes[:header]}"
      new_attributes.delete(:header)   ## note: do NOT forget to remove from hash!
    end
    
    Brewery.create_or_update_from_attribs( new_attributes, values )
  end # each_line
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 )   ### "virtual" method - required by concrete class

  reader.each do |fixture_name|
    load( fixture_name )
  end
end