38
39
40
41
42
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
|
# File 'lib/winedb/models/wine.rb', line 38
def self.create_or_update_from_attribs( attribs, values )
logger = LogKernel::Logger.root
value_tag_keys = []
value_tag_keys += find_tags_in_attribs!( attribs )
values.each_with_index do |value,index|
if match_country(value) do |country|
attribs[ :country_id ] = country.id
end
elsif match_region_for_country(value, attribs[:country_id]) do |region|
attribs[ :region_id ] = region.id
end
elsif match_city(value) do |city|
if city.present?
attribs[ :city_id ] = city.id
else
logger.warn "city with key #{value[5..-1]} missing for beer #{attribs[:key]}"
end
end
elsif match_year( value ) do |num| attribs[ :since ] = num
end
elsif match_website( value ) do |website| attribs[ :web ] = website
end
elsif value =~ /^winery:/ winery_key = value[7..-1].strip winery = Winery.find_by_key!( winery_key )
attribs[ :winery_id ] = winery.id
elsif (values.size==(index+1)) && is_taglist?( value ) logger.debug " found tags: >>#{value}<<"
value_tag_keys += find_tags( value )
else
logger.warn "unknown type for value >#{value}< - key #{attribs[:key]}"
end
end
rec = Wine.find_by_key( attribs[ :key ] )
if rec.present?
logger.debug "update Wine #{rec.id}-#{rec.key}:"
else
logger.debug "create Wine:"
rec = Wine.new
end
logger.debug attribs.to_json
rec.update_attributes!( attribs )
if value_tag_keys.size > 0
value_tag_keys.uniq! logger.debug " adding #{value_tag_keys.size} taggings: >>#{value_tag_keys.join('|')}<<..."
value_tag_keys.each do |key|
tag = Tag.find_by_key( key )
if tag.nil? logger.debug " creating tag >#{key}<"
tag = Tag.create!( key: key )
end
rec.tags << tag
end
end
rec
end
|