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



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/worlddb/reader.rb', line 50

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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/worlddb/reader.rb', line 144

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



132
133
134
135
136
137
138
139
140
141
# File 'lib/worlddb/reader.rb', line 132

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



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/worlddb/reader.rb', line 117

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

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

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

  reader = FixtureReader.new( path )

  reader.each do |fixture|
    load( fixture )
  end
end

#load_xxx(xxx, name) ⇒ Object

use CountryAttr Reader



170
171
172
173
174
175
176
177
178
# File 'lib/worlddb/reader.rb', line 170

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