Class: WineDb::Reader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Matcher

#fix_match_xxx_for_country_n_region, #match_shops_for_country_n_region, #match_taverns_for_country_n_region, #match_vineyards_for_country_n_region, #match_wineries_for_country, #match_wineries_for_country_n_region, #match_wines_for_country, #match_wines_for_country_n_region

Constructor Details

#initialize(include_path, opts = {}) ⇒ Reader

Returns a new instance of Reader.



114
115
116
# File 'lib/winedb/reader.rb', line 114

def initialize( include_path, opts = {} )
  @include_path = include_path
end

Instance Attribute Details

#include_pathObject (readonly)

Returns the value of attribute include_path.



111
112
113
# File 'lib/winedb/reader.rb', line 111

def include_path
  @include_path
end

Instance Method Details

#load(name) ⇒ Object



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/winedb/reader.rb', line 132

def load( name )

  #  grapes - allow e.g.
  #   - /grapes or /grapes-red or /grapes-white
  if name =~ /(^|\/)grapes(-(red|white))?$/
    load_grapes( name )
  elsif match_wines_for_country_n_region( name ) do |country_key, region_key|
      ### fix: use region_key too
      load_wines_for_country( country_key, name )
     end
  elsif match_wines_for_country( name ) do |country_key|
          load_wines_for_country( country_key, name )
        end
  elsif match_wineries_for_country_n_region( name ) do |country_key, region_key|
      ### fix: use region_key too
          load_wineries_for_country( country_key, name )
        end
  elsif match_wineries_for_country( name ) do |country_key|
          load_wineries_for_country( country_key, name )
        end
  else
    logger.error "unknown wine.db fixture type >#{name}<"
    # todo/fix: exit w/ error
  end
end

#load_grapes(name, more_attribs = {}) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/winedb/reader.rb', line 159

def load_grapes( name, more_attribs={} )
  if name =~ /(^|\/)grapes-red$/
    more_attribs[ :red ] = true
  elsif name =~ /(^|\/)grapes-white$/
    more_attribs[ :white ] = true
  else
    # do nothing
  end

  load_grapes_worker( name, more_attribs )
end

#load_grapes_worker(name, more_attribs = {}) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/winedb/reader.rb', line 171

def load_grapes_worker( name, more_attribs={} )
  reader = ValuesReaderV2.new( name, include_path, more_attribs )

  reader.each_line do |new_attributes, values|
    Grape.create_or_update_from_attribs( new_attributes, values )
  end # each_line
end

#load_setup(name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/winedb/reader.rb', line 119

def load_setup( name )
  path = "#{include_path}/#{name}.yml"

  logger.info "parsing data '#{name}' (#{path})..."

  reader = FixtureReader.new( path )

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

#load_wineries_for_country(country_key, name, more_attribs = {}) ⇒ Object



235
236
237
238
239
240
241
242
243
244
# File 'lib/winedb/reader.rb', line 235

def load_wineries_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_wineries_worker( name, more_attribs )
end

#load_wineries_worker(name, more_attribs = {}) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/winedb/reader.rb', line 246

def load_wineries_worker( name, more_attribs={} )
  reader = ValuesReaderV2.new( name, include_path, more_attribs )

  reader.each_line do |new_attributes, values|
    
    #######
    # fix: move to (inside)
    #    Winery.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
    
    Winery.create_or_update_from_attribs( new_attributes, values )
  end # each_line
end

#load_wines_for_country(country_key, name, more_attribs = {}) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/winedb/reader.rb', line 180

def load_wines_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_wines_worker( name, more_attribs )
end

#load_wines_worker(name, more_attribs = {}) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/winedb/reader.rb', line 193

def load_wines_worker( name, more_attribs={} )
  reader = ValuesReaderV2.new( name, include_path, more_attribs )

  ### todo: cleanup - check if [] works for build_title...
  #     better cleaner way ???
  if more_attribs[:region_id].present?
    known_wineries_source = Winery.where( region_id:  more_attribs[:region_id] )
  elsif more_attribs[:country_id].present?
    known_wineries_source = Winery.where( country_id: more_attribs[:country_id] )
  else
    logger.warn "no region or country specified; use empty winery ary for header mapper"
    known_wineries_source = []
  end

  known_wineries = TextUtils.build_title_table_for( known_wineries_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_winery()   move region,city code out of values loop for reuse at the end
    if new_attributes[:header].present?
      winery_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 winery in line >#{winery_line}<"
      ## todo: check what map_titles_for! returns (nothing ???)
      TextUtils.map_titles_for!( 'winery', winery_line, known_wineries )
      winery_key = TextUtils.find_key_for!( 'winery', winery_line )
      logger.debug "  winery_key = >#{winery_key}<"
      unless winery_key.nil?
        ## bingo! add winery_id upfront, that is, as first value in ary
        values = values.unshift "winery:#{winery_key}"
      end
    end

    Wine.create_or_update_from_attribs( new_attributes, values )
  end # each_line
end