Class: WorldDb::Model::StateBase
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- WorldDb::Model::StateBase
show all
- Extended by:
- TextUtils::TagHelper, TextUtils::ValueHelper
- Defined in:
- lib/worlddb/models/forward.rb,
lib/worlddb/models/state_base.rb
Class Method Summary
collapse
Instance Method Summary
collapse
is_state?, match_city, match_country, match_metro, match_metro_flag, match_metro_pop, match_state_for_country, match_supra, match_supra_flag
Class Method Details
.create_or_update_from_attribs(new_attributes, values, opts = {}) ⇒ Object
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
|
# File 'lib/worlddb/models/state_base.rb', line 90
def self.create_or_update_from_attribs( new_attributes, values, opts={} )
logger = LogKernel::Logger.root
value_numbers = []
value_tag_keys = []
value_cities = []
value_tag_keys += find_tags_in_attribs!( new_attributes )
values.each_with_index do |value,index|
if match_country( value ) do |country| new_attributes[ :country_id ] = country.id
end
elsif match_km_squared( value ) do |num| value_numbers << num
end
elsif match_number( value ) do |num| value_numbers << num
end
elsif value =~ /#{STATE_CODE_PATTERN}/ new_attributes[ :code ] = value
elsif (values.size==(index+1)) && is_taglist?( value ) logger.debug " found tags: >>#{value}<<"
value_tag_keys += find_tags( value )
else
value_cities << value
next
end
end
if value_numbers.size > 0
new_attributes[ :area ] = value_numbers[0]
new_attributes[ :pop ] = value_numbers[1]
end
puts "[debug] StateBase.create_or_update_from_attribs calling #{self.name}.where" if self == State
rec = self.find_by(
key: new_attributes[ :key ],
country_id: new_attributes[ :country_id] )
else
rec = self.find_by(
key: new_attributes[ :key ],
state_id: new_attributes[ :state_id] )
end
if rec.present?
logger.debug "update #{self.name} #{rec.id}-#{rec.key}:"
else
logger.debug "create #{self.name}:" puts "[debug] StateBase.create_or_update_from_attribs calling #{self.name}.new" rec = self.new
end
logger.debug new_attributes.to_json
rec.update_attributes!( new_attributes )
City.parse( value_cities,
state_id: rec.state_id,
country_id: rec.country_id )
if value_tag_keys.size > 0
if opts[:skip_tags].present?
logger.debug " skipping add taggings (flag skip_tag)"
else
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
end
rec
end
|
.create_or_update_from_values(values, more_attribs = {}) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/worlddb/models/state_base.rb', line 76
def self.create_or_update_from_values( values, more_attribs={} )
attribs, more_values = find_key_n_title( values )
attribs = attribs.merge( more_attribs )
puts "[debug] StateBase.create_or_update_from_values calling #{self.name}" self.create_or_update_from_attribs( attribs, more_values )
end
|
.parse(*args) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/worlddb/models/state_base.rb', line 68
def self.parse( *args )
more_attribs = args.last.is_a?(Hash) ? args.pop : {} values = args
self.create_or_update_from_values( values, more_attribs )
end
|
Instance Method Details
#all_names(opts = {}) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/worlddb/models/state_base.rb', line 54
def all_names( opts={} )
return name if alt_names.blank?
buf = ''
buf << name
buf << ' | '
buf << alt_names.split('|').join(' | ')
buf
end
|
#on_create ⇒ Object
32
|
# File 'lib/worlddb/models/state_base.rb', line 32
before_create :on_create
|
#on_update ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/worlddb/models/state_base.rb', line 45
def on_update
place.update_attributes!( name: name, kind: place_kind )
end
|
#synonyms ⇒ Object
28
|
# File 'lib/worlddb/models/state_base.rb', line 28
def synonyms() alt_names; end
|
#synonyms=(value) ⇒ Object
29
|
# File 'lib/worlddb/models/state_base.rb', line 29
def synonyms=(value) self.alt_names = value; end
|
#title ⇒ Object
25
|
# File 'lib/worlddb/models/state_base.rb', line 25
def title() name; end
|
#title=(value) ⇒ Object
26
|
# File 'lib/worlddb/models/state_base.rb', line 26
def title=(value) self.name = value; end
|