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_wines_for_country

Constructor Details

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

Returns a new instance of Reader.



39
40
41
# File 'lib/winedb/reader.rb', line 39

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

Instance Attribute Details

#include_pathObject (readonly)

Returns the value of attribute include_path.



36
37
38
# File 'lib/winedb/reader.rb', line 36

def include_path
  @include_path
end

Instance Method Details

#load(name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/winedb/reader.rb', line 57

def load( name )

  if match_wines_for_country( name ) do |country_key|
          load_wines_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



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/winedb/reader.rb', line 44

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



126
127
128
129
130
131
132
133
134
135
# File 'lib/winedb/reader.rb', line 126

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/winedb/reader.rb', line 137

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



72
73
74
75
76
77
78
79
80
81
# File 'lib/winedb/reader.rb', line 72

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/winedb/reader.rb', line 84

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