Class: WorldDb::Reader
- Inherits:
-
Object
- Object
- WorldDb::Reader
- Includes:
- LogUtils::Logging, Models
- Defined in:
- lib/worlddb/reader.rb
Instance Attribute Summary collapse
-
#include_path ⇒ Object
readonly
Returns the value of attribute include_path.
Instance Method Summary collapse
-
#initialize(include_path, opts = {}) ⇒ Reader
constructor
A new instance of Reader.
- #load(name) ⇒ Object
- #load_cities(country_key, name) ⇒ Object
- #load_countries(name, more_values = {}) ⇒ Object
-
#load_fixture_setup(name) ⇒ Object
fix/todo: rename ??.
- #load_langs(name) ⇒ Object
- #load_regions(country_key, name) ⇒ Object
- #load_setup(setup) ⇒ Object
- #load_tags(name, more_values = {}) ⇒ Object
- #load_usages(name) ⇒ Object
- #load_xxx(xxx, name) ⇒ Object
- #skip_tags? ⇒ Boolean
- #strict? ⇒ Boolean
- #with_path_for(name) {|path| ... } ⇒ Object
Constructor Details
#initialize(include_path, opts = {}) ⇒ Reader
Returns a new instance of Reader.
21 22 23 24 25 26 27 28 29 |
# File 'lib/worlddb/reader.rb', line 21 def initialize( include_path, opts = {} ) @include_path = include_path ## option: do NOT generate/add any tags for countries/regions/cities @skip_tags = opts[:skip_tags].present? ? true : false ## option: for now issue warning on update, that is, if key/record (country,region,city) already exists @strict = opts[:strict].present? ? true : false end |
Instance Attribute Details
#include_path ⇒ Object (readonly)
Returns the value of attribute include_path.
11 12 13 |
# File 'lib/worlddb/reader.rb', line 11 def include_path @include_path end |
Instance Method Details
#load(name) ⇒ 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 |
# File 'lib/worlddb/reader.rb', line 90 def load( name ) if name =~ /^lang/ load_langs( name ) elsif name =~ /\/lang/ load_usages( name ) elsif name =~ /\/fifa/ load_xxx( 'fifa', name ) elsif name =~ /\/iso3/ load_xxx( 'iso3', name ) elsif name =~ /\/internet/ load_xxx( 'net', name ) elsif name =~ /\/motor/ load_xxx( 'motor', name ) elsif name =~ /^tag.*\.(\d)$/ ( name, :grade => $1.to_i ) elsif name =~ /^([a-z][a-z\-]+[a-z])\/countries/ # e.g. africa/countries or america/countries ### NB: continent changed to regions (e.g. middle-east, caribbean, north-america, etc.) ### fix/cleanup/todo: ## auto-add continent (from folder structure) as tag ## load_countries( name, include_path, :tags => $1 ) load_countries( name ) elsif name =~ /\/([a-z]{2})\/cities/ ## auto-add required country code (from folder structure) load_cities( $1, name ) elsif name =~ /\/([a-z]{2})\/regions/ ## auto-add required country code (from folder structure) load_regions( $1, name ) else logger.error "unknown world.db fixture type >#{name}<" # todo/fix: exit w/ error end end |
#load_cities(country_key, name) ⇒ Object
138 139 140 141 142 143 |
# File 'lib/worlddb/reader.rb', line 138 def load_cities( country_key, name ) country = Country.find_by_key!( country_key ) logger.debug "Country #{country.key} >#{country.title} (#{country.code})<" load_fixtures_for( City, name, country_id: country.id ) end |
#load_countries(name, more_values = {}) ⇒ Object
125 126 127 |
# File 'lib/worlddb/reader.rb', line 125 def load_countries( name, more_values={} ) load_fixtures_for( Country, name, more_values ) end |
#load_fixture_setup(name) ⇒ Object
fix/todo: rename ??
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 |
# File 'lib/worlddb/reader.rb', line 53 def load_fixture_setup( name ) ## todo/fix: cleanup quick and dirty code path = "#{include_path}/#{name}.yml" logger.info "parsing data '#{name}' (#{path})..." text = File.read_utf8( path ) hash = YAML.load( text ) ### build up array for fixtures from hash ary = [] hash.each do |key_wild, value_wild| key = key_wild.to_s.strip logger.debug "yaml key:#{key_wild.class.name} >>#{key}<<, value:#{value_wild.class.name} >>#{value_wild}<<" if value_wild.kind_of?( String ) # assume single fixture name ary << value_wild elsif value_wild.kind_of?( Array ) # assume array of fixture names as strings ary = ary + value_wild else logger.error "unknow fixture type in setup (yaml key:#{key_wild.class.name} >>#{key}<<, value:#{value_wild.class.name} >>#{value_wild}<<); skipping" end end logger.debug "fixture setup:" logger.debug ary.to_json ary end |
#load_langs(name) ⇒ Object
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 |
# File 'lib/worlddb/reader.rb', line 162 def load_langs( name ) with_path_for( name ) do |path| reader = HashReader.new( path ) reader.each do |key, value| logger.debug "adding lang >>#{key}<< >>#{value}<<..." lang_key = key.strip lang_title = value.strip lang_attribs = {} ## check if it exists lang = Lang.find_by_key( lang_key ) if lang.present? logger.debug "update lang #{lang.id}-#{lang.key}:" else logger.debug "create lang:" lang = Lang.new lang_attribs[ :key ] = lang_key end lang_attribs[ :title ] = lang_title logger.debug lang_attribs.to_json lang.update_attributes!( lang_attribs ) end # each key,value end # with_path_for end |
#load_regions(country_key, name) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/worlddb/reader.rb', line 130 def load_regions( country_key, name ) country = Country.find_by_key!( country_key ) logger.debug "Country #{country.key} >#{country.title} (#{country.code})<" load_fixtures_for( Region, name, country_id: country.id ) end |
#load_setup(setup) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/worlddb/reader.rb', line 32 def load_setup( setup ) ary = load_fixture_setup( setup ) ### fix/todo: # use to setups - use comment to remove fixtures # too big for heroku free db plan (10,000 record limit) # - sorry, can't load by default fixture_excludes = [ 'south-america/ve/cities' ] ary = ary - fixture_excludes ary.each do |name| load( name ) end end |
#load_tags(name, more_values = {}) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/worlddb/reader.rb', line 198 def ( name, more_values={} ) with_path_for( name ) do |path| reader = HashReader.new( path ) grade = 1 if more_values[:grade].present? grade = more_values[:grade].to_i end reader.each do |key, value| ### split value by comma (e.g. northern america,southern america, etc.) logger.debug "adding grade #{grade} tags >>#{key}<< >>#{value}<<..." tag_pairs = value.split(',') tag_pairs.each do |pair| ## split key|title values = pair.split('|') key = values[0] ### remove (optional comment) from key (e.g. carribean (islands)) key = key.gsub( /\(.+\)/, '' ) ## remove leading n trailing space key = key.strip title = values[1] || '' # nb: title might be empty/missing title = title.strip tag_attribs = {} ## check if it exists ## todo/fix: add country_id for lookup? tag = Tag.find_by_key( key ) if tag.present? logger.debug "update tag #{tag.id}-#{tag.key}:" else logger.debug "create tag:" tag = Tag.new tag_attribs[ :key ] = key end tag_attribs[ :title ] = title tag_attribs[ :grade ] = grade logger.debug tag_attribs.to_json tag.update_attributes!( tag_attribs ) end end # each key,value end # with_path_for end |
#load_usages(name) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/worlddb/reader.rb', line 254 def load_usages( name ) path = "#{include_path}/#{name}.yml" puts "*** parsing data '#{name}' (#{path})..." reader = HashReader.new( path ) reader.each do |key, value| logger.debug " adding langs >>#{value}<<to country >>#{key}<<" country = Country.find_by_key!( key ) lang_keys = value.split(',') lang_keys.each do |lang_key| ### remove (optional comment) from key (e.g. carribean (islands)) lang_key = lang_key.gsub( /\(.+\)/, '' ) ## remove leading n trailing space lang_key = lang_key.strip lang = Lang.find_by_key!( lang_key ) Usage.create!( country_id: country.id, lang_id: lang.id, official: true, minor: false ) end end Prop.create_from_fixture!( name, path ) end |
#load_xxx(xxx, name) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/worlddb/reader.rb', line 283 def load_xxx( xxx, name ) path = "#{include_path}/#{name}.yml" puts "*** parsing data '#{name}' (#{path})..." reader = HashReader.new( path ) reader.each do |key, value| country = Country.find_by_key!( key ) country.send( "#{xxx}=", value ) country.save! end Prop.create_from_fixture!( name, path ) end |
#skip_tags? ⇒ Boolean
13 14 15 |
# File 'lib/worlddb/reader.rb', line 13 def @skip_tags == true end |
#strict? ⇒ Boolean
17 18 19 |
# File 'lib/worlddb/reader.rb', line 17 def strict? @strict == true end |
#with_path_for(name) {|path| ... } ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/worlddb/reader.rb', line 147 def with_path_for( name ) ## todo: find a better name? # e.g. find_path_for or open_fixture_for ?? path = "#{include_path}/#{name}.yml" logger.info "parsing data '#{name}' (#{path})..." yield( path ) Prop.create_from_fixture!( name, path ) end |