Class: WorldDB::Reader

Inherits:
Object
  • Object
show all
Includes:
Models
Defined in:
lib/worlddb/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil) ⇒ Reader

Returns a new instance of Reader.



10
11
12
13
14
15
16
17
# File 'lib/worlddb/reader.rb', line 10

def initialize( logger=nil )
  if logger.nil?
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::INFO
  else
    @logger = logger
  end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#load_builtin(name) ⇒ Object

convenience helper (requires proper named files w/ convention)



67
68
69
# File 'lib/worlddb/reader.rb', line 67

def load_builtin( name )  ## convenience helper (requires proper named files w/ convention)
  load_with_include_path( name, WorldDB.data_path )
end

#load_cities_builtin(country_key, name) ⇒ Object



100
101
102
# File 'lib/worlddb/reader.rb', line 100

def load_cities_builtin( country_key, name )
  load_cities_with_include_path( country_key, name, WorldDB.data_path )
end

#load_cities_with_include_path(country_key, name, include_path) ⇒ Object



93
94
95
96
97
98
# File 'lib/worlddb/reader.rb', line 93

def load_cities_with_include_path( country_key, name, include_path )
  country = Country.find_by_key!( country_key )
  puts "Country #{country.key} >#{country.title} (#{country.code})<"

  load_fixtures_with_include_path_for( City, name, include_path, country_id: country.id )
end

#load_countries_builtin(name, more_values = {}) ⇒ Object



76
77
78
# File 'lib/worlddb/reader.rb', line 76

def load_countries_builtin( name, more_values={} )
  load_countries_with_include_path( name, WorldDB.data_path, more_values )
end

#load_countries_with_include_path(name, include_path, more_values = {}) ⇒ Object



72
73
74
# File 'lib/worlddb/reader.rb', line 72

def load_countries_with_include_path( name, include_path, more_values={} )
  load_fixtures_with_include_path_for( Country, name, include_path, more_values )
end

#load_regions_builtin(country_key, name) ⇒ Object



88
89
90
# File 'lib/worlddb/reader.rb', line 88

def load_regions_builtin( country_key, name )
  load_regions_with_include_path( country_key, name, WorldDB.data_path )
end

#load_regions_with_include_path(country_key, name, include_path) ⇒ Object



81
82
83
84
85
86
# File 'lib/worlddb/reader.rb', line 81

def load_regions_with_include_path( country_key, name, include_path )
  country = Country.find_by_key!( country_key )
  puts "Country #{country.key} >#{country.title} (#{country.code})<"

  load_fixtures_with_include_path_for( Region, name, include_path, country_id: country.id )
end

#load_with_include_path(name, include_path) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/worlddb/reader.rb', line 42

def load_with_include_path( name, include_path )

  if name =~ /\/fifa/
     load_xxx_with_include_path( 'fifa', name, include_path )
  elsif name =~ /\/iso3/
     load_xxx_with_include_path( 'iso3', name, include_path )
  elsif name =~ /\/internet/
     load_xxx_with_include_path( 'net', name, include_path )
  elsif name =~ /\/motor/
     load_xxx_with_include_path( 'motor', name, include_path )
  elsif name =~ /^([a-z]{3,})\/countries/     # e.g. africa/countries or america/countries
    ## auto-add continent (from folder structure) as tag
    load_countries_with_include_path( name, include_path, :tags => $1 )
  elsif name =~ /\/([a-z]{2})\/cities/
    ## auto-add required country code (from folder structure)
    load_cities_with_include_path( $1, name, include_path )
  elsif name =~ /\/([a-z]{2})\/regions/
    ## auto-add required country code (from folder structure)
    load_regions_with_include_path( $1, name, include_path )
  else
    puts "*** error: unknown world.db fixture type >#{name}<"
    # todo/fix: exit w/ error
  end
end

#load_xxx_builtin(xxx, name) ⇒ Object



121
122
123
# File 'lib/worlddb/reader.rb', line 121

def load_xxx_builtin( xxx, name )
  load_xxx_with_include_path( xxx, name, WorldDB.data_path )
end

#load_xxx_with_include_path(xxx, name, include_path) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/worlddb/reader.rb', line 105

def load_xxx_with_include_path( xxx, name, include_path )
  path = "#{include_path}/#{name}.yml"

  puts "*** parsing data '#{name}' (#{path})..."

  reader = HashReader.new( logger, path )

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

  Prop.create_from_worlddb_fixture!( name, path )
end

#run(opts, args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/worlddb/reader.rb', line 21

def run( opts, args )
 
  args.each do |arg|
    name = arg     # File.basename( arg, '.*' )

    data_path = opts.load? ? WorldDB.data_path : opts.data_path

    if opts.countries?
      load_countries_with_include_path( name, data_path )
    elsif opts.regions?
      load_regions_with_include_path( opts.country, name, data_path )
    elsif opts.cities?
      load_cities_with_include_path( opts.country, name, data_path )
    else
      ## todo: issue a warning here; no fixture type specified; assume country?
    end
  end # each arg

end