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_state, #match_breweries_for_country, #match_breweries_for_country_n_state, #match_brewpubs_for_country, #match_brewpubs_for_country_n_state

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_state( name ) do |country_key, state_key|
          load_beers_for_country_n_state( country_key, state_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_state( name ) do |country_key, state_key|
          load_breweries_for_country_n_state( country_key, state_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_state( name ) do |country_key, state_key|
          load_breweries_for_country_n_state( country_key, state_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



104
105
106
107
108
109
110
111
112
113
# File 'lib/beerdb/reader.rb', line 104

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_state(country_key, state_key, name, more_attribs = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/beerdb/reader.rb', line 83

def load_beers_for_country_n_state( country_key, state_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: state lookup requires country id (state key only unique for country)
  ##  check: was find_by_key_and_country_id
  state  = State.find_by( key: state_key, country_id: country.id )
  if state.nil?
    # note: allow unknown state keys; issue warning n skip state
    logger.warn "State w/ key >#{state_key}< not found; skip adding state"
  else
    logger.debug "State #{state.key} >#{state.title}<"
    more_attribs[ :state_id  ] = state.id
  end

  more_attribs[ :txt ] = name  # store source ref

  load_beers_worker( name, more_attribs )
end

#load_beers_worker(name, more_attribs) ⇒ Object



151
152
153
154
# File 'lib/beerdb/reader.rb', line 151

def load_beers_worker( name, more_attribs )
  reader = create_beers_reader( name, more_attribs )
  reader.read
end

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



138
139
140
141
142
143
144
145
146
147
# File 'lib/beerdb/reader.rb', line 138

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_state(country_key, state_key, name, more_attribs = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/beerdb/reader.rb', line 116

def load_breweries_for_country_n_state( country_key, state_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: state lookup requires country id (state key only unique for country)
  ##   was: State.find_by_key_and_country_id( state_key, country.id )
  state = State.find_by( key: state_key, country_id: country.id )
  if state.nil?
    # note: allow unknown state keys; issue warning n skip state
    logger.warn "State w/ key >#{state_key}< not found; skip adding state"
  else
    logger.debug "State #{state.key} >#{state.title}<"
    more_attribs[ :state_id  ] = state.id
  end

  more_attribs[ :txt ] = name  # store source ref

  load_breweries_worker( name, more_attribs )
end

#load_breweries_worker(name, more_attribs) ⇒ Object



156
157
158
159
# File 'lib/beerdb/reader.rb', line 156

def load_breweries_worker( name, more_attribs )
  reader = create_breweries_reader( name, more_attribs )
  reader.read
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