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

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

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#load_cities_with_include_path(country_key, name, include_path) ⇒ Object



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

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

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

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



53
54
55
# File 'lib/worlddb/reader.rb', line 53

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_langs_with_include_path(name, include_path) ⇒ Object



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

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

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

  reader = HashReader.new( logger, 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?
      puts "*** update lang #{lang.id}-#{lang.key}:"
    else
      puts "*** create lang:"
      lang = Lang.new
      lang_attribs[ :key ] = lang_key
    end
      
    lang_attribs[ :title ] = lang_title
      
    puts lang_attribs.to_json
 
    lang.update_attributes!( lang_attribs )
  end # each key,value

  Prop.create_from_worlddb_fixture!( name, path )
end

#load_regions_with_include_path(country_key, name, include_path) ⇒ Object



58
59
60
61
62
63
# File 'lib/worlddb/reader.rb', line 58

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

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

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/worlddb/reader.rb', line 111

def load_tags_with_include_path( name, include_path, more_values={} )
  path = "#{include_path}/#{name}.yml"

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

  reader = HashReader.new( logger, 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.)
    puts "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?
        puts "*** update tag #{tag.id}-#{tag.key}:"
      else
        puts "*** create tag:"
        tag = Tag.new
        tag_attribs[ :key ] = key
      end
      
      tag_attribs[ :title ] = title
      tag_attribs[ :grade ] = grade
      
      puts tag_attribs.to_json
 
      tag.update_attributes!( tag_attribs )
    end
  end # each key,value

  Prop.create_from_worlddb_fixture!( name, path )
end

#load_usages_with_include_path(name, include_path) ⇒ Object



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

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

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

  reader = HashReader.new( logger, path )

  reader.each do |key, value|
    puts "   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_worlddb_fixture!( name, path )
end

#load_with_include_path(name, include_path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/worlddb/reader.rb', line 21

def load_with_include_path( name, include_path )

  if name =~ /^lang/
     load_langs_with_include_path( name, include_path )
  elsif name =~ /\/lang/
     load_usages_with_include_path( name, include_path )
  elsif 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 =~ /^tag.*\.(\d)$/
     load_tags_with_include_path( name, include_path, :grade => $1.to_i )
  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
    logger.error "unknown world.db fixture type >#{name}<"
    # todo/fix: exit w/ error
  end
end

#load_xxx_with_include_path(xxx, name, include_path) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/worlddb/reader.rb', line 196

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