Class: BeerDb::Models::Brewery

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
TextUtils::ValueHelper
Defined in:
lib/beerdb/models/brewery.rb,
lib/beerdb/models/forward.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_or_update_from_values(new_attributes, values) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
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
81
82
83
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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
# File 'lib/beerdb/models/brewery.rb', line 43

def self.create_or_update_from_values( new_attributes, values )

  ## fix: add/configure logger for ActiveRecord!!!

  logger = LogKernel::Logger.root

  value_tag_keys    = []
  value_brands      = ''

  ### check for "default" tags - that is, if present new_attributes[:tags] remove from hash


  if new_attributes[:tags].present?
    more_tag_keys = new_attributes[:tags].split('|')
    new_attributes.delete(:tags)

    ## unify; replace _ w/ space; remove leading n trailing whitespace

    more_tag_keys = more_tag_keys.map do |key|
      key = key.gsub( '_', ' ' )
      key = key.strip
      key
    end

    value_tag_keys += more_tag_keys
  end


  ## check for optional values

  values.each_with_index do |value,index|
    if value =~ /^country:/   ## country:

      value_country_key = value[8..-1]  ## cut off country: prefix

      value_country = Country.find_by_key!( value_country_key )
      new_attributes[ :country_id ] = value_country.id
    elsif value =~ /^region:/   ## region:

      value_region_key = value[7..-1]  ## cut off region: prefix

      value_region = Region.find_by_key_and_country_id!( value_region_key, new_attributes[:country_id] )
      new_attributes[ :region_id ] = value_region.id
    elsif is_region?( value )  ## assume region code e.g. TX or N

      value_region = Region.find_by_key_and_country_id!( value.downcase, new_attributes[:country_id] )
      new_attributes[ :region_id ] = value_region.id
    elsif value =~ /^city:/   ## city:

      value_city_key = value[5..-1]  ## cut off city: prefix

      value_city = City.find_by_key( value_city_key )
      if value_city.present?
        new_attributes[ :city_id ] = value_city.id
      else
        ## todo/fix: add strict mode flag - fail w/ exit 1 in strict mode

        logger.warn "city with key #{value_city_key} missing"
      end

      ## for easy queries: cache region_id (from city)

      #  - check if city w/ region if yes, use it for brewery too

      if value_city.present? && value_city.region.present?
        new_attributes[ :region_id ] = value_city.region.id
      end
    elsif is_year?( value )  # founded/established year e.g. 1776

      new_attributes[ :since ] = value.to_i
    elsif value =~ /^(?:([0-9][0-9_ ]+[0-9]|[0-9]{1,2})\s*hl)$/  # e.g. 20_000 hl or 50hl etc.

      value_prod = $1.gsub( /[ _]/, '' ).to_i
      new_attributes[ :prod ] = value_prod
    elsif is_website?( value )   # check for url/internet address e.g. www.ottakringer.at

      # fix: support more url format (e.g. w/o www. - look for .com .country code etc.)

      new_attributes[ :web ] = value
    elsif is_address?( value ) # if value includes // assume address e.g. 3970 Weitra // Sparkasseplatz 160

      new_attributes[ :address ] = TextUtils.normalize_address( value )
    elsif value =~ /^brands:/   # brands:

      value_brands = value[7..-1]  ## cut off brands: prefix

      value_brands = value_brands.strip  # remove leading and trailing spaces

      # NB: brands get processed after record gets created (see below)

    elsif (values.size==(index+1)) && is_taglist?( value ) # tags must be last entry


      logger.debug "   found tags: >>#{value}<<"

      tag_keys = value.split('|')

      ## unify; replace _ w/ space; remove leading n trailing whitespace

      tag_keys = tag_keys.map do |key|
        key = key.gsub( '_', ' ' )
        key = key.strip
        key
      end
        
      value_tag_keys += tag_keys
    else
      # issue warning: unknown type for value

      logger.warn "unknown type for value >#{value}< - key #{new_attributes[:key]}"
    end
  end # each value


  ## todo: check better style using self.find_by_key?? why? why not?

  rec = Brewery.find_by_key( new_attributes[ :key ] )

  if rec.present?
    logger.debug "update Brewery #{rec.id}-#{rec.key}:"
  else
    logger.debug "create Brewery:"
    rec = Brewery.new
  end
    
  logger.debug new_attributes.to_json

  rec.update_attributes!( new_attributes )

  ###################

  # auto-add brands if presents


  if value_brands.present?
    logger.debug " auto-adding brands >#{value_brands}<"

    # remove optional english translation in square brackets ([]) e.g. Wien [Vienna]

    value_brands = TextUtils.strip_translations( value_brands )

    # remove optional longer title part in () e.g. Las Palmas (de Gran Canaria), Palma (de Mallorca)

    value_brands = TextUtils.strip_subtitles( value_brands )

    # remove optional longer title part in {} e.g. Ottakringer {Bio} or {Alkoholfrei}

    value_brands = TextUtils.strip_tags( value_brands )

    value_brand_titles = value_brands.split( ',' )

    # pass 1) remove leading n trailing spaces

    value_brand_titles = value_brand_titles.map { |value| value.strip }

    value_brand_titles.each do |brand_title|
      
      # autogenerate key from title

      brand_key = TextUtils.title_to_key( brand_title )

      brand = Brand.find_by_key( brand_key )

      brand_attributes = {
        title:      brand_title,
        brewery_id: rec.id,
        country_id: rec.country_id,
        region_id:  rec.region_id,
        city_id:    rec.city_id
      }

      if brand.present?
        logger.debug "update Brand #{brand.id}-#{brand.key}:"
      else
        logger.debug "create Brand:"
        brand = Brand.new
        brand_attributes[ :key ] = brand_key   # NB: new record; include/update key

      end
    
      logger.debug brand_attributes.to_json

      brand.update_attributes!( brand_attributes )
    end
  end

  ##################

  ## add taggings


  if value_tag_keys.size > 0
      
    value_tag_keys.uniq!  # remove duplicates

    logger.debug "   adding #{value_tag_keys.size} taggings: >>#{value_tag_keys.join('|')}<<..."

    ### fix/todo: check tag_ids and only update diff (add/remove ids)


    value_tag_keys.each do |key|
      tag = Tag.find_by_key( key )
      if tag.nil?  # create tag if it doesn't exit

        logger.debug "   creating tag >#{key}<"
        tag = Tag.create!( key: key )
      end
      rec.tags << tag
    end
  end

  rec # NB: return created or updated obj


end

.rndObject

find random beer - fix: use “generic” activerecord helper and include/extend class



26
27
28
29
# File 'lib/beerdb/models/brewery.rb', line 26

def self.rnd  # find random beer  - fix: use "generic" activerecord helper and include/extend class

  rnd_offset = rand( Brewery.count )   ## NB: call "global" std lib rand

  Brewery.offset( rnd_offset ).limit( 1 )
end

Instance Method Details

#foundedObject

support old names (read-only) for now (remove later)



34
35
36
# File 'lib/beerdb/models/brewery.rb', line 34

def founded
  since
end

#founded=(value) ⇒ Object



38
39
40
# File 'lib/beerdb/models/brewery.rb', line 38

def founded=(value)
  self.since = value
end