Class: WorldDb::Model::Region
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- WorldDb::Model::Region
- Extended by:
- TextUtils::TagHelper, TextUtils::ValueHelper
- Defined in:
- lib/worlddb/models/region_compat.rb,
lib/worlddb/models/region.rb,
lib/worlddb/models/forward.rb
Overview
collect depreciated or methods for future removal here
- keep for now for commpatibility (for old code)
Class Method Summary collapse
- .create_or_update_from_attribs(new_attributes, values, opts = {}) ⇒ Object
- .create_or_update_from_values(values, more_attribs = {}) ⇒ Object
Instance Method Summary collapse
- #all_names(opts = {}) ⇒ Object
- #is_county? ⇒ Boolean
- #is_district? ⇒ Boolean
- #on_create ⇒ Object
- #on_update ⇒ Object
-
#place_kind ⇒ Object
use place_kind_of_code ??.
-
#regions ⇒ Object
subregions.
- #synonyms ⇒ Object
- #synonyms=(value) ⇒ Object
- #title ⇒ Object
- #title=(value) ⇒ Object
-
#title_w_synonyms(opts = {}) ⇒ Object
depreciated: use all_names instead.
Class Method Details
.create_or_update_from_attribs(new_attributes, values, opts = {}) ⇒ Object
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 |
# File 'lib/worlddb/models/region.rb', line 85 def self.create_or_update_from_attribs( new_attributes, values, opts={} ) ## opts e.g. :skip_tags true|false ## fix: add/configure logger for ActiveRecord!!! logger = LogKernel::Logger.root value_numbers = [] value_tag_keys = [] value_cities = [] ### check for "default" tags - that is, if present new_attributes[:tags] remove from hash value_tag_keys += ( new_attributes ) ## check for optional values values.each_with_index do |value,index| if match_country( value ) do |country| # country: new_attributes[ :country_id ] = country.id end elsif match_km_squared( value ) do |num| # allow numbers like 453 km² value_numbers << num end elsif match_number( value ) do |num| # numeric (nb: can use any _ or spaces inside digits e.g. 1_000_000 or 1 000 000) value_numbers << num end elsif value =~ /#{REGION_CODE_PATTERN}/ ## assume two or three-letter code new_attributes[ :code ] = value elsif (values.size==(index+1)) && is_taglist?( value ) # tags must be last entry logger.debug " found tags: >>#{value}<<" value_tag_keys += ( value ) else ### assume it is the capital city - mark it for auto add value_cities << value next # issue warning: unknown type for value # logger.warn "unknown type for value >#{value}<" end end # each value if value_numbers.size > 0 new_attributes[ :area ] = value_numbers[0] new_attributes[ :pop ] = value_numbers[1] end # if value_numbers.size > 0 ## todo: assert that country_id is present/valid, that is, NOT null rec = Region.find_by_key_and_country_id( new_attributes[ :key ], new_attributes[ :country_id] ) if rec.present? logger.debug "update Region #{rec.id}-#{rec.key}:" else logger.debug "create Region:" rec = Region.new end logger.debug new_attributes.to_json rec.update_attributes!( new_attributes ) ################# # auto add capital cities City.create_or_update_from_titles( value_cities, region_id: rec.id, country_id: rec.country_id ) ### todo/fix: add captial ref to country/region ## todo/fix: use update_from_title and only allow one capital city ################## # add taggings ## todo/fix: reuse - move add taggings into method etc. if value_tag_keys.size > 0 if opts[:skip_tags].present? logger.debug " skipping add taggings (flag skip_tag)" else 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. << tag end end end rec end |
.create_or_update_from_values(values, more_attribs = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/worlddb/models/region.rb', line 74 def self.create_or_update_from_values( values, more_attribs={} ) ## key & title & country required attribs, more_values = find_key_n_title( values ) attribs = attribs.merge( more_attribs ) ## check for optional values Region.create_or_update_from_attribs( attribs, more_values ) end |
Instance Method Details
#all_names(opts = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/worlddb/models/region.rb', line 59 def all_names( opts={} ) ### fix: ## allow to passing in sep or separator e.g. | or other return name if alt_names.blank? buf = '' buf << name buf << ' | ' buf << alt_names.split('|').join(' | ') buf end |
#is_county? ⇒ Boolean
45 |
# File 'lib/worlddb/models/region.rb', line 45 def is_county?() c? == true; end |
#is_district? ⇒ Boolean
44 |
# File 'lib/worlddb/models/region.rb', line 44 def is_district?() d? == true; end |
#on_create ⇒ Object
34 35 36 37 |
# File 'lib/worlddb/models/region.rb', line 34 def on_create place_rec = Place.create!( name: name, kind: place_kind ) self.place_id = place_rec.id end |
#on_update ⇒ Object
39 40 41 42 |
# File 'lib/worlddb/models/region.rb', line 39 def on_update ## fix/todo: check - if name or kind changed - only update if changed ?? why? why not?? place.update_attributes!( name: name, kind: place_kind ) end |
#place_kind ⇒ Object
use place_kind_of_code ??
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/worlddb/models/region.rb', line 47 def place_kind # use place_kind_of_code ?? ### fix: use "generic" level counter - make it a database field (default/top level is 1) if is_district? 'ADM2' elsif is_county? 'ADM3' else 'ADM1' end end |
#regions ⇒ Object
subregions
28 |
# File 'lib/worlddb/models/region.rb', line 28 has_many :regions, class_name: 'Region', foreign_key: 'region_id' |
#synonyms ⇒ Object
17 |
# File 'lib/worlddb/models/region_compat.rb', line 17 def synonyms() alt_names; end |
#synonyms=(value) ⇒ Object
18 |
# File 'lib/worlddb/models/region_compat.rb', line 18 def synonyms=(value) self.alt_names = value; end |
#title ⇒ Object
12 |
# File 'lib/worlddb/models/region_compat.rb', line 12 def title() name; end |
#title=(value) ⇒ Object
13 |
# File 'lib/worlddb/models/region_compat.rb', line 13 def title=(value) self.name = value; end |
#title_w_synonyms(opts = {}) ⇒ Object
depreciated: use all_names instead
20 |
# File 'lib/worlddb/models/region_compat.rb', line 20 def title_w_synonyms( opts={} ) all_names( opts ); end |