13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
|
# File 'lib/sportdb/models/ground.rb', line 13
def self.create_or_update_from_values( new_attributes, values )
logger = LogKernel::Logger.root
logger.debug " [Ground] values >#{values.join('<>')}<"
city_title = ''
values.each_with_index do |value, index|
if value =~ /^(19|20)[0-9]{2}$/ logger.info " founding/opening year #{value}"
new_attributes[ :since ] = value.to_i
elsif value =~ /^[1-9][0-9_]+[0-9]$/
logger.info " found capacity #{value}"
new_attributes[ :capacity ] = value.gsub('_', '').to_i
elsif value =~ /^[A-Z]{1,3}$/ logger.info " found region/state code #{value}; skipping for now"
elsif value =~ /\/{2}/ logger.info " found address line #{value}"
new_attributes[ :address ] = value
elsif value =~ /^clubs:/ logger.info " found clubs line #{value}; skipping for now"
elsif value =~ /^(?:[a-z]{2}\.)?wikipedia:/ logger.info " found wikipedia line #{value}; skipping for now"
else
logger.info " found city >#{value}< for ground >#{new_attributes[ :key ]}<"
city_title = value.dup
end
end
logger.debug " find ground key: #{new_attributes[ :key ]}"
rec = Ground.find_by_key( new_attributes[ :key ] )
if rec.present?
logger.debug "update Ground #{rec.id}-#{rec.key}:"
else
logger.debug "create Ground:"
rec = Ground.new
end
logger.debug new_attributes.to_json
rec.update_attributes!( new_attributes )
if city_title.present?
city_values = [city_title]
city_attributes = {
country_id: rec.country_id,
}
city = City.create_or_update_from_values( city_values, city_attributes )
rec.city_id = city.id
rec.save!
end
end
|