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

load from gem (built-in)



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/worlddb/reader.rb', line 77

def load_builtin( name )  ## convenience helper (requires proper named files w/ convention)
  if name =~ /\/countries/
     load_countries_builtin( name )
  elsif name =~ /\/([a-z]{2})\/cities/
     load_cities_builtin( $1, name )
  elsif name =~ /\/([a-z]{2})\/regions/
     load_regions_builtin( $1, name )
  else
     puts "*** error: unknown world.db fixture type >#{name}<"
     # todo/fix: exit w/ error
  end
end

#load_cities_builtin(country_key, name) ⇒ Object



101
102
103
104
105
106
# File 'lib/worlddb/reader.rb', line 101

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

  load_fixtures_builtin_for( City, name, country_id: country.id )
end

#load_cities_with_include_path(country_key, name, include_path) ⇒ Object



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

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) ⇒ Object



90
91
92
# File 'lib/worlddb/reader.rb', line 90

def load_countries_builtin( name ) 
  load_fixtures_builtin_for( Country, name )
end

#load_countries_with_include_path(name, include_path) ⇒ Object



56
57
58
# File 'lib/worlddb/reader.rb', line 56

def load_countries_with_include_path( name, include_path )
  load_fixtures_with_include_path_for( Country, name, include_path )
end

#load_regions_builtin(country_key, name) ⇒ Object



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

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

  load_fixtures_builtin_for( Region, name, country_id: country.id )
end

#load_regions_with_include_path(country_key, name, include_path) ⇒ Object



60
61
62
63
64
65
# File 'lib/worlddb/reader.rb', line 60

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

load from file system



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/worlddb/reader.rb', line 43

def load_with_include_path( name, include_path )
  if name =~ /\/countries/
    load_countries_with_include_path( name, include_path )
  elsif name =~ /\/([a-z]{2})\/cities/
    load_cities_with_include_path( $1, name, include_path )
  elsif name =~ /\/([a-z]{2})\/regions/
    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

#run(opts, args) ⇒ Object



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

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

    if opts.load?
      load_countries_builtin( name )             if opts.countries?
      load_regions_builtin( opts.country, name ) if opts.regions?
      load_cities_builtin( opts.country, name )  if opts.cities?
    else
      load_countries_with_include_path( name, opts.data_path )              if opts.countries?
      load_regions_with_include_path( opts.country, name, opts.data_path )  if opts.regions?
      load_cities_with_include_path( opts.country, name, opts.data_path )   if opts.cities?
    end
  end

end