worlddb 2.0.9 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/HISTORY.md +1 -1
  3. data/Manifest.txt +1 -38
  4. data/README.md +0 -67
  5. data/Rakefile +7 -11
  6. data/lib/worlddb.rb +8 -187
  7. data/lib/worlddb/cli/main.rb +3 -8
  8. data/lib/worlddb/{version.rb → cli/version.rb} +9 -7
  9. data/lib/worlddb/console.rb +5 -10
  10. metadata +10 -110
  11. data/.gemtest +0 -0
  12. data/lib/worlddb/deleter.rb +0 -32
  13. data/lib/worlddb/matcher.rb +0 -143
  14. data/lib/worlddb/models/city.rb +0 -240
  15. data/lib/worlddb/models/city_comp.rb +0 -27
  16. data/lib/worlddb/models/continent.rb +0 -41
  17. data/lib/worlddb/models/continent_comp.rb +0 -24
  18. data/lib/worlddb/models/country.rb +0 -328
  19. data/lib/worlddb/models/country_code.rb +0 -41
  20. data/lib/worlddb/models/country_comp.rb +0 -35
  21. data/lib/worlddb/models/forward.rb +0 -57
  22. data/lib/worlddb/models/lang.rb +0 -18
  23. data/lib/worlddb/models/lang_comp.rb +0 -23
  24. data/lib/worlddb/models/name.rb +0 -13
  25. data/lib/worlddb/models/place.rb +0 -16
  26. data/lib/worlddb/models/region.rb +0 -176
  27. data/lib/worlddb/models/region_comp.rb +0 -26
  28. data/lib/worlddb/models/tagdb/tag.rb +0 -16
  29. data/lib/worlddb/models/tagdb/tagging.rb +0 -15
  30. data/lib/worlddb/models/usage.rb +0 -17
  31. data/lib/worlddb/patterns.rb +0 -54
  32. data/lib/worlddb/reader.rb +0 -224
  33. data/lib/worlddb/reader_file.rb +0 -86
  34. data/lib/worlddb/reader_zip.rb +0 -160
  35. data/lib/worlddb/readers/city.rb +0 -81
  36. data/lib/worlddb/readers/country.rb +0 -78
  37. data/lib/worlddb/readers/lang.rb +0 -107
  38. data/lib/worlddb/readers/region.rb +0 -79
  39. data/lib/worlddb/readers/usage.rb +0 -98
  40. data/lib/worlddb/schema.rb +0 -202
  41. data/lib/worlddb/stats.rb +0 -31
  42. data/test/helper.rb +0 -29
  43. data/test/test_fixture_matchers.rb +0 -112
  44. data/test/test_model_city.rb +0 -60
  45. data/test/test_model_comp.rb +0 -48
  46. data/test/test_model_country.rb +0 -53
  47. data/test/test_model_region.rb +0 -50
  48. data/test/test_models.rb +0 -35
@@ -1,35 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module WorldDb
4
- module Model
5
-
6
- #############################################################
7
- # collect depreciated or methods for future removal here
8
- # - keep for now for commpatibility (for old code)
9
-
10
-
11
- class Country
12
-
13
- def title() name; end
14
- def title=(value) self.name = value; end
15
-
16
- scope :by_title, ->{ order( 'name asc' ) } # order by title (a-z)
17
-
18
- def iso2() alpha2; end
19
- def iso2=(value) self.alpha2 = value; end
20
-
21
- def iso3() alpha3; end
22
- def iso3=(value) self.alpha3 = value; end
23
-
24
-
25
- def synonyms() alt_names; end
26
- def synonyms=(value) self.alt_names = value; end
27
-
28
- def title_w_synonyms( opts={} ) all_names( opts ); end # depreciated: use all_names instead
29
-
30
-
31
- end # class Country
32
-
33
- end # module Model
34
- end # module WorldDb
35
-
@@ -1,57 +0,0 @@
1
-
2
- ### forward references
3
- ## require first to resolve circular references
4
-
5
- module WorldDb
6
- module Model
7
-
8
- #############
9
- # ConfDb
10
- Prop = ConfDb::Model::Prop
11
-
12
- ###########
13
- # TagDb
14
- Tagging = TagDb::Model::Tagging
15
- Tag = TagDb::Model::Tag
16
-
17
-
18
- class Name < ActiveRecord::Base ; end
19
- class Place < ActiveRecord::Base ; end
20
- class Continent < ActiveRecord::Base ; end
21
- class Country < ActiveRecord::Base ; end
22
- class Region < ActiveRecord::Base ; end
23
- class City < ActiveRecord::Base ; end
24
-
25
- class Lang < ActiveRecord::Base ; end
26
- class Usage < ActiveRecord::Base ; end
27
-
28
- class CountryCode < ActiveRecord::Base ; end
29
-
30
- end
31
-
32
- # note: convenience alias for Model
33
- # lets you use include WorldDb::Models
34
- Models = Model
35
- end # module # WorldDb
36
-
37
-
38
- module TagDb
39
- module Model
40
-
41
- # add alias? why? why not? # is there a better way?
42
- # - just include WorldDb::Models - why? why not?
43
-
44
- Name = WorldDb::Model::Name
45
- Place = WorldDb::Model::Place
46
- Continent = WorldDb::Model::Continent
47
- Country = WorldDb::Model::Country
48
- Region = WorldDb::Model::Region
49
- City = WorldDb::Model::City
50
-
51
- Lang = WorldDb::Model::Lang
52
- Usage = WorldDb::Model::Usage
53
-
54
- CountryCode = WorldDb::Model::CountryCode
55
-
56
- end
57
- end
@@ -1,18 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module WorldDb
4
- module Model
5
-
6
- class Lang < ActiveRecord::Base
7
-
8
- has_many :usages # join table for countries_langs
9
-
10
- has_many :countries, :through => :usages
11
-
12
- validates :key, format: { with: /#{LANG_KEY_PATTERN}/, message: LANG_KEY_PATTERN_MESSAGE }
13
-
14
- end # class Lang
15
-
16
- end # module Model
17
- end # module WorldDb
18
-
@@ -1,23 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module WorldDb
4
- module Model
5
-
6
- #############################################################
7
- # collect depreciated or methods for future removal here
8
- # - keep for now for commpatibility (for old code)
9
-
10
-
11
- class Lang
12
-
13
- #####################################################
14
- # alias for name (remove! add depreciated api call ???)
15
- def title() name; end
16
- def title=(value) self.name = value; end
17
-
18
- scope :by_title, ->{ order( 'name asc' ) } # order by title (a-z)
19
-
20
- end # class Lang
21
-
22
- end # module Model
23
- end # module WorldDb
@@ -1,13 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module WorldDb
4
- module Model
5
-
6
- class Name < ActiveRecord::Base
7
-
8
-
9
- end # class Name
10
-
11
-
12
- end # module Model
13
- end # module WorldDb
@@ -1,16 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module WorldDb
4
- module Model
5
-
6
- class Place < ActiveRecord::Base
7
-
8
- ## todo: depending on type
9
- ## has_one continent, country, region, city etc.
10
-
11
- end # class Place
12
-
13
-
14
- end # module Model
15
- end # module WorldDb
16
-
@@ -1,176 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- module WorldDb
4
- module Model
5
-
6
-
7
- class Region < ActiveRecord::Base
8
-
9
- extend TextUtils::TagHelper # will add self.find_tags, self.find_tags_in_attribs!, etc.
10
-
11
- # NB: use extend - is_<type>? become class methods e.g. self.is_<type>? for use in
12
- # self.create_or_update_from_values
13
- extend TextUtils::ValueHelper # e.g. is_year?, is_region?, is_address?, is_taglist? etc.
14
-
15
- belongs_to :place, class_name: 'Place', foreign_key: 'place_id'
16
- belongs_to :country, class_name: 'Country', foreign_key: 'country_id'
17
-
18
- has_many :cities, class_name: 'City', foreign_key: 'region_id'
19
-
20
- has_many_tags
21
-
22
- validates :key, format: { with: /#{REGION_KEY_PATTERN}/, message: REGION_KEY_PATTERN_MESSAGE }
23
- validates :code, format: { with: /#{REGION_CODE_PATTERN}/, message: REGION_CODE_PATTERN_MESSAGE }, allow_nil: true
24
-
25
-
26
- before_create :on_create
27
- before_update :on_update
28
-
29
- def on_create
30
- place_rec = Place.create!( name: name, kind: place_kind )
31
- self.place_id = place_rec.id
32
- end
33
-
34
- def on_update
35
- ## fix/todo: check - if name or kind changed - only update if changed ?? why? why not??
36
- place.update_attributes!( name: name, kind: place_kind )
37
- end
38
-
39
- def place_kind # use place_kind_of_code ??
40
- 'ADM1'
41
- end
42
-
43
-
44
- def all_names( opts={} )
45
- ### fix:
46
- ## allow to passing in sep or separator e.g. | or other
47
-
48
- return name if alt_names.blank?
49
-
50
- buf = ''
51
- buf << name
52
- buf << ' | '
53
- buf << alt_names.split('|').join(' | ')
54
- buf
55
- end
56
-
57
-
58
-
59
- def self.create_or_update_from_values( values, more_attribs={} )
60
-
61
- ## key & title & country required
62
- attribs, more_values = find_key_n_title( values )
63
- attribs = attribs.merge( more_attribs )
64
-
65
- ## check for optional values
66
- Region.create_or_update_from_attribs( attribs, more_values )
67
- end
68
-
69
-
70
- def self.create_or_update_from_attribs( new_attributes, values, opts={} )
71
-
72
- ## opts e.g. :skip_tags true|false
73
-
74
- ## fix: add/configure logger for ActiveRecord!!!
75
- logger = LogKernel::Logger.root
76
-
77
- value_numbers = []
78
- value_tag_keys = []
79
- value_cities = []
80
-
81
- ### check for "default" tags - that is, if present new_attributes[:tags] remove from hash
82
- value_tag_keys += find_tags_in_attribs!( new_attributes )
83
-
84
- ## check for optional values
85
- values.each_with_index do |value,index|
86
- if match_country( value ) do |country| # country:
87
- new_attributes[ :country_id ] = country.id
88
- end
89
- elsif match_km_squared( value ) do |num| # allow numbers like 453 km²
90
- value_numbers << num
91
- end
92
- elsif match_number( value ) do |num| # numeric (nb: can use any _ or spaces inside digits e.g. 1_000_000 or 1 000 000)
93
- value_numbers << num
94
- end
95
- elsif value =~ /#{REGION_CODE_PATTERN}/ ## assume two or three-letter code
96
- new_attributes[ :code ] = value
97
- elsif (values.size==(index+1)) && is_taglist?( value ) # tags must be last entry
98
- logger.debug " found tags: >>#{value}<<"
99
- value_tag_keys += find_tags( value )
100
- else
101
-
102
- ### assume it is the capital city - mark it for auto add
103
- value_cities << value
104
- next
105
-
106
- # issue warning: unknown type for value
107
- # logger.warn "unknown type for value >#{value}<"
108
- end
109
- end # each value
110
-
111
- if value_numbers.size > 0
112
- new_attributes[ :area ] = value_numbers[0]
113
- new_attributes[ :pop ] = value_numbers[1]
114
- end # if value_numbers.size > 0
115
-
116
- ## todo: assert that country_id is present/valid, that is, NOT null
117
- rec = Region.find_by_key_and_country_id( new_attributes[ :key ], new_attributes[ :country_id] )
118
-
119
- if rec.present?
120
- logger.debug "update Region #{rec.id}-#{rec.key}:"
121
- else
122
- logger.debug "create Region:"
123
- rec = Region.new
124
- end
125
-
126
- logger.debug new_attributes.to_json
127
-
128
- rec.update_attributes!( new_attributes )
129
-
130
- #################
131
- # auto add capital cities
132
-
133
- City.create_or_update_from_titles( value_cities,
134
- region_id: rec.id,
135
- country_id: rec.country_id )
136
-
137
- ### todo/fix: add captial ref to country/region
138
- ## todo/fix: use update_from_title and only allow one capital city
139
-
140
-
141
- ##################
142
- # add taggings
143
-
144
- ## todo/fix: reuse - move add taggings into method etc.
145
-
146
- if value_tag_keys.size > 0
147
-
148
- if opts[:skip_tags].present?
149
- logger.debug " skipping add taggings (flag skip_tag)"
150
- else
151
- value_tag_keys.uniq! # remove duplicates
152
- logger.debug " adding #{value_tag_keys.size} taggings: >>#{value_tag_keys.join('|')}<<..."
153
-
154
- ### fix/todo: check tag_ids and only update diff (add/remove ids)
155
-
156
- value_tag_keys.each do |key|
157
- tag = Tag.find_by_key( key )
158
- if tag.nil? # create tag if it doesn't exit
159
- logger.debug " creating tag >#{key}<"
160
- tag = Tag.create!( key: key )
161
- end
162
- rec.tags << tag
163
- end
164
- end
165
- end
166
-
167
- rec
168
-
169
- end # method create_or_update_from_values
170
-
171
-
172
-
173
- end # class Region
174
-
175
- end # module Model
176
- end # module WorldDb
@@ -1,26 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module WorldDb
4
- module Model
5
-
6
- #############################################################
7
- # collect depreciated or methods for future removal here
8
- # - keep for now for commpatibility (for old code)
9
-
10
- class Region
11
-
12
- def title() name; end
13
- def title=(value) self.name = value; end
14
-
15
- scope :by_title, ->{ order( 'name asc' ) } # order by title (a-z)
16
-
17
- def synonyms() alt_names; end
18
- def synonyms=(value) self.alt_names = value; end
19
-
20
- def title_w_synonyms( opts={} ) all_names( opts ); end # depreciated: use all_names instead
21
-
22
-
23
- end # class Region
24
-
25
- end # module Model
26
- end # module WorldDb
@@ -1,16 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module TagDb
4
- module Model
5
-
6
- class Tag
7
-
8
- has_many :cities, :through => :taggings, :source => :taggable, source_type: 'WorldDb::Model::City', class_name: 'WorldDb::Model::City'
9
- has_many :countries, :through => :taggings, :source => :taggable, source_type: 'WorldDb::Model::Country', class_name: 'WorldDb::Model::Country'
10
- has_many :regions, :through => :taggings, :source => :taggable, source_type: 'WorldDb::Model::Region', class_name: 'WorldDb::Model::Region'
11
-
12
- end # class Tag
13
-
14
- end # module Model
15
- end # module WorldDb
16
-
@@ -1,15 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module TagDb
4
- module Model
5
-
6
-
7
- class Tagging
8
-
9
- ## add some code here
10
-
11
- end # class Tagging
12
-
13
-
14
- end # module Model
15
- end # module WorldDb
@@ -1,17 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module WorldDb
4
- module Model
5
-
6
-
7
- class Usage < ActiveRecord::Base
8
-
9
- belongs_to :country
10
- belongs_to :lang
11
-
12
- end # class Usage
13
-
14
-
15
- end # module Model
16
- end # module WorldDb
17
-
@@ -1,54 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module WorldDb
4
-
5
- # collection of regex patterns for reuse (WorldDb specific)
6
-
7
- ### todo: add a patterns.md page to github ??
8
- ## - add regexper pics??
9
-
10
- ############
11
- # about ruby regexps
12
- #
13
- # try the rubular - Ruby regular expression editor and tester
14
- # -> http://rubular.com
15
- # code -> ?? by ??
16
- #
17
- #
18
- # Jeff Avallone's Regexper - Shows State-Automata Diagrams
19
- # try -> http://regexper.com
20
- # code -> https://github.com/javallone/regexper
21
- #
22
- #
23
- # Regular Expressions | The Bastards Book of Ruby by Dan Nguyen
24
- # http://ruby.bastardsbook.com/chapters/regexes/
25
- #
26
- # move to notes regex|patterns on geraldb.github.io ??
27
- #
28
-
29
- COUNTRY_KEY_PATTERN = '\A[a-z]{2,3}\z' # allow two AND three letter keys e.g. at, mx, eng, sco, etc.
30
- COUNTRY_KEY_PATTERN_MESSAGE = "expected two or three lowercase letters a-z /#{COUNTRY_KEY_PATTERN}/"
31
-
32
- COUNTRY_CODE_PATTERN = '\A[A-Z_]{3}\z'
33
- COUNTRY_CODE_PATTERN_MESSAGE = "expected three uppercase letters A-Z (and _) /#{COUNTRY_CODE_PATTERN}/"
34
-
35
-
36
- REGION_KEY_PATTERN = '\A[a-z]+\z'
37
- REGION_KEY_PATTERN_MESSAGE = "expected one or more lowercase letters a-z /#{REGION_KEY_PATTERN}/"
38
-
39
- REGION_CODE_PATTERN = '\A[A-Z_]{2,3}\z'
40
- REGION_CODE_PATTERN_MESSAGE = "expected two or three uppercase letters A-Z (and _) /#{REGION_CODE_PATTERN}/"
41
-
42
-
43
- CITY_KEY_PATTERN = '\A[a-z]{3,}\z'
44
- CITY_KEY_PATTERN_MESSAGE = "expected three or more lowercase letters a-z' /#{CITY_KEY_PATTERN}/"
45
-
46
- CITY_CODE_PATTERN = '\A[A-Z_]{3}\z'
47
- CITY_CODE_PATTERN_MESSAGE = "expected three uppercase letters A-Z (and _)' /#{CITY_CODE_PATTERN}/"
48
-
49
-
50
- LANG_KEY_PATTERN = '\A[a-z]{2}\z'
51
- LANG_KEY_PATTERN_MESSAGE = "expected two lowercase letters a-z' /#{LANG_KEY_PATTERN}/"
52
-
53
- end # module WorldDb
54
-