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

#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.



59
60
61
# File 'lib/winedb/reader.rb', line 59

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

Instance Attribute Details

#include_pathObject (readonly)

Returns the value of attribute include_path.



56
57
58
# File 'lib/winedb/reader.rb', line 56

def include_path
  @include_path
end

Instance Method Details

#load(name) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/winedb/reader.rb', line 77

def load( name )

  if 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_setup(name) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/winedb/reader.rb', line 64

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



154
155
156
157
158
159
160
161
162
163
# File 'lib/winedb/reader.rb', line 154

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



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/winedb/reader.rb', line 165

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



100
101
102
103
104
105
106
107
108
109
# File 'lib/winedb/reader.rb', line 100

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



112
113
114
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
# File 'lib/winedb/reader.rb', line 112

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