Class: BeerDb::Model::Beer

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_or_update_from_attribs(attribs, values) ⇒ Object



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
# File 'lib/beerdb/models/beer.rb', line 73

def self.create_or_update_from_attribs( attribs, values )

  # fix: add/configure logger for ActiveRecord!!!
  logger = LogKernel::Logger.root

  value_tag_keys    = []

  ## check for grades (e.g. ***/**/*) in titles (will add attribs[:grade] to hash)
  ## if grade missing; set default to 4; lets us update overwrite 1,2,3 values on update
  attribs[ :grade ] ||= 4

  ### check for "default" tags - that is, if present attribs[:tags] remove from hash
  value_tag_keys += find_tags_in_attribs!( attribs )

  ## check for optional values
  values.each_with_index do |value,index|
    if match_country(value) do |country|
         attribs[ :country_id ] = country.id
       end
    elsif match_state_for_country(value, attribs[:country_id]) do |state|
            attribs[ :state_id ] = state.id
          end
    elsif match_city(value) do |city|
            if city.present?
              attribs[ :city_id ] = city.id
            else
              ## todo/fix: add strict mode flag - fail w/ exit 1 in strict mode
              logger.warn "city with key #{value[5..-1]} missing for beer #{attribs[:key]}"
            end
          end
    elsif match_brewery(value) do |brewery|
            attribs[ :brewery_id ] = brewery.id

            # for easy queries cache city and state ids

            # 1) check if brewery has city - if yes, use it for beer too
            if brewery.city.present?
              attribs[ :city_id ] = brewery.city.id
            end

            # 2) check if brewery has city w/ state if yes, use it for beer to
            #   if not check for state for brewery
            if brewery.city.present? && brewery.city.state.present?
              attribs[ :state_id ] = brewery.city.state.id
            elsif brewery.state.present?
              attribs[ :state_id ] = brewery.state.id
            end
          end
    elsif match_year( value ) do |num|  # founded/established year e.g. 1776
            attribs[ :since ] = num
          end
    elsif match_website( value ) do |website|  # check for url/internet address e.g. www.ottakringer.at
            attribs[ :web ] = website
          end
    elsif match_abv( value ) do |num|   # abv (alcohol by volume)
            # nb: also allows leading < e.g. <0.5%
            attribs[ :abv ] = num
          end
    elsif match_og( value ) do |num|  # plato (stammwuerze/gravity?) e.g. 11.2°
            # nb: no whitespace allowed between ° and number e.g. 11.2°
            attribs[ :og ] = num
          end
    elsif match_kcal( value ) do |num| # kcal
            # nb: allow 44.4 kcal/100ml or 44.4 kcal or 44.4kcal
            attribs[ :kcal ] = num
          end
    elsif (values.size==(index+1)) && is_taglist?( value )  # tags must be last entry
      logger.debug "   found tags: >>#{value}<<"
      value_tag_keys += find_tags( value )
    else
      # issue warning: unknown type for value
      logger.warn "unknown type for value >#{value}< - key #{attribs[:key]}"
    end
  end # each value

  #  rec = Beer.find_by_key_and_country_id( attribs[ :key ], attribs[ :country_id] )
  rec = Beer.find_by_key( attribs[ :key ] )

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

  logger.debug attribs.to_json

  rec.update_attributes!( attribs )

  ##################
  # add taggings

##
##  fix: delete all tags first or only add diff?
##  fix e.g.
##
## [debug] update Beer 1340-opatsvetlevycepni:
## [debug] {"title":"Opat Světlé Výčepní","key":"opatsvetlevycepni","country_id":130,"txt":"cz-czech-republic!/beers","grade":4,"brewery_id":839,"city_id":1154,"state_id":241,"abv":4.2,"og":11.0}
## [debug]    adding 1 taggings: >>pale lager<<...
## rake aborted!
## ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: columns taggable_id, taggable_type, tag_id are not unique: INSERT INTO "taggings" ("created_at", "tag_id", "taggable_id", "taggable_type", "updated_at") VALUES (?, ?, ?, ?, ?)

=begin
  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
=end

  rec # NB: return created or updated obj

end

.create_or_update_from_values(values, more_attribs = {}) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/beerdb/models/beer.rb', line 63

def self.create_or_update_from_values( values, more_attribs={} )

  attribs, more_values = find_key_n_title( values )
  attribs = attribs.merge( more_attribs )

  # check for optional values
  Beer.create_or_update_from_attribs( attribs, more_values )
end

Instance Method Details

#as_json_v2(opts = {}) ⇒ Object



57
58
59
60
# File 'lib/beerdb/models/beer.rb', line 57

def as_json_v2( opts={} )
  # Note: do NOT overwrite "default" / builtin as_json, thus, lets use as_json_v2
  BeerSerializer.new( self ).as_json
end

#colorObject

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



43
# File 'lib/beerdb/models/beer.rb', line 43

def color()  puts "*** depreceated fn api - use srm"; srm;  end

#color=(value) ⇒ Object



46
47
48
49
# File 'lib/beerdb/models/beer.rb', line 46

def color=(value)
  puts "*** depreceated fn api - use srm="
  self.srm = value
end

#keyObject

fix/todo: move to regex to patterns; see worlddb



27
# File 'lib/beerdb/models/beer.rb', line 27

validates :key, :format => { :with => /\A[a-z][a-z0-9]+\z/, :message => 'expected two or more lowercase letters a-z or 0-9 digits' }

#platoObject



44
# File 'lib/beerdb/models/beer.rb', line 44

def plato()  puts "*** depreceated fn api - use og";  og;   end

#plato=(value) ⇒ Object



51
52
53
54
# File 'lib/beerdb/models/beer.rb', line 51

def plato=(value)
  puts "*** depreceated fn api - use og="
  self.og = value
end