Class: WorldDb::Reader

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging, Models, TextUtils::ValueHelper, Matcher
Defined in:
lib/worlddb/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Matcher

#match_cities_for_country, #match_countries_for_continent, #match_regions_abbr_for_country, #match_regions_for_country, #match_regions_iso_for_country, #match_regions_nuts_for_country, #match_xxx_for_country, #match_xxx_for_country_n_region

Constructor Details

#initialize(include_path, opts = {}) ⇒ Reader

Returns a new instance of Reader.



26
27
28
29
30
31
32
33
34
# File 'lib/worlddb/reader.rb', line 26

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_pathObject (readonly)

Returns the value of attribute include_path.



20
21
22
# File 'lib/worlddb/reader.rb', line 20

def include_path
  @include_path
end

Instance Method Details

#load(name) ⇒ Object



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
# File 'lib/worlddb/reader.rb', line 58

def load( name )

  if name =~ /^continents/
     load_continent_defs( name )
  elsif name =~ /\/continents/
     load_continent_refs( name )
  elsif name =~ /^lang/
     ## todo: pass along opts too
     ## use match_usage( name ) - why? why not?? ???
     LangReader.new( include_path ).read( name )
  elsif name =~ /\/lang/
     ## todo: pass along opts too
     ## use match_usage( name ) - why? why not?? ???
     UsageReader.new( include_path ).read( 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$/
     ## todo: pass along opts too
     ## use match_tags( name ) - why? why not?? ???
     TagDb::TagReader.new( include_path ).read( name )
  elsif match_countries_for_continent( name ) do |continent|  # # e.g. africa/countries or america/countries
          ### NB: continent changed to regions (e.g. middle-east, caribbean, north-america, etc.)
          ## auto-add continent (from folder structure) as tag
          ## fix: allow dash/hyphen/minus in tag

          r = CountryReader.new( include_path )
          r.read( name, tags: continent.tr('-', '_') )
        end
  elsif match_cities_for_country( name ) do |country_key|  #  name =~ /\/([a-z]{2})\/cities/
          ## auto-add required country code (from folder structure)
          country = Country.find_by_key!( country_key )
          logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"

          r = CityReader.new( include_path )
          r.read( name, country_id: country.id )
        end
  elsif match_regions_abbr_for_country( name ) do |country_key|   # name =~ /\/([a-z]{2})\/regions\.abbr/
          load_regions_xxx( country_key, 'abbr', name )
        end
  elsif match_regions_iso_for_country( name ) do |country_key|  # name =~ /\/([a-z]{2})\/regions\.iso/
          load_regions_xxx( country_key, 'iso', name )
        end
  elsif match_regions_nuts_for_country( name ) do |country_key|  # name =~ /\/([a-z]{2})\/regions\.nuts/
          load_regions_xxx( country_key, 'nuts', name )
        end
  elsif match_regions_for_country( name ) do |country_key|  # name =~ /\/([a-z]{2})\/regions/
          ## auto-add required country code (from folder structure)
          country = Country.find_by_key!( country_key )
          logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"

          r = RegionReader.new( include_path )
          r.read( name, country_id: country.id )
        end
  else
    logger.error "unknown world.db fixture type >#{name}<"
    # todo/fix: exit w/ error
  end
end

#load_continent_defs(name, more_attribs = {}) ⇒ Object

use ContinentDef Reader



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/worlddb/reader.rb', line 152

def load_continent_defs( name, more_attribs={} )
  reader = ValuesReaderV2.new( name, include_path, more_attribs )

  reader.each_line do |attribs, values|

    ## check optional values
    values.each_with_index do |value, index|
      logger.warn "unknown type for value >#{value}<"
    end

    rec = Continent.find_by_key( attribs[ :key ] )
    if rec.present?
      logger.debug "update Continent #{rec.id}-#{rec.key}:"
    else
      logger.debug "create Continent:"
      rec = Continent.new
    end
    
    logger.debug attribs.to_json
 
    rec.update_attributes!( attribs )

  end # each lines
end

#load_continent_refs(name) ⇒ Object

use ContinentRefReader



140
141
142
143
144
145
146
147
148
149
# File 'lib/worlddb/reader.rb', line 140

def load_continent_refs( name )
  reader = HashReaderV2.new( name, include_path )

  reader.each do |key, value|
    country = Country.find_by_key!( key )
    continent = Continent.find_by_key!( value )
    country.continent_id = continent.id
    country.save!
  end
end

#load_regions_xxx(country_key, xxx, name) ⇒ Object

use RegionAttrReader



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/worlddb/reader.rb', line 125

def load_regions_xxx( country_key, xxx, name )
  country = Country.find_by_key!( country_key )
  logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"

  reader = HashReaderV2.new( name, include_path )

  reader.each do |key, value|
    region = Region.find_by_country_id_and_key!( country.id, key )
    region.send( "#{xxx}=", value )
    region.save!
  end
end

#load_setup(name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/worlddb/reader.rb', line 37

def load_setup( name )
  path = "#{include_path}/#{name}.yml"

  logger.info "parsing data '#{name}' (#{path})..."

  reader = FixtureReader.new( path )

  # too big for heroku free db plan (10,000 record limit)
  #  - sorry, can't load by default
  fixture_excludes = [
    'south-america/ve/cities'
  ]

  reader.each do |fixture_name|
    next if fixture_excludes.include?( fixture_name )  # skip loading
    
    load( fixture_name )
  end
end

#load_xxx(xxx, name) ⇒ Object

use CountryAttr Reader



178
179
180
181
182
183
184
185
186
# File 'lib/worlddb/reader.rb', line 178

def load_xxx( xxx, name )
  reader = HashReaderV2.new( name, include_path )

  reader.each do |key, value|
    country = Country.find_by_key!( key )
    country.send( "#{xxx}=", value )
    country.save!
  end
end

#skip_tags?Boolean

Returns:

  • (Boolean)


22
# File 'lib/worlddb/reader.rb', line 22

def skip_tags?()   @skip_tags == true;  end

#strict?Boolean

Returns:

  • (Boolean)


23
# File 'lib/worlddb/reader.rb', line 23

def strict?()      @strict == true;     end