persondb 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ae3400852fe3e86d39cef68b9b9d5dbfb15bc97
4
+ data.tar.gz: 1af4911c17764d27463a5bf4124910bab85d123e
5
+ SHA512:
6
+ metadata.gz: e9a92a35b7aea6f0cfaf16b7783cfed55f8db4a56e82bfde653b4aebe231bbbf7984ced2d24987a52d18c346f54bda509c92ea03ee9dc78436dc7525bca9b0c0
7
+ data.tar.gz: 579c4f48464a2c5e44b2e4a7363dd0b13699731ad90c735733d82c9f720cc0b10543d8aef4971573242d3510e124e27e67b3a4cb940f9fdcf9d49124318d381e
data/Manifest.txt CHANGED
@@ -8,7 +8,10 @@ lib/persondb/models/person.rb
8
8
  lib/persondb/models/worlddb/city.rb
9
9
  lib/persondb/models/worlddb/country.rb
10
10
  lib/persondb/models/worlddb/region.rb
11
+ lib/persondb/reader.rb
11
12
  lib/persondb/schema.rb
12
13
  lib/persondb/version.rb
14
+ test/data/players/south-america/br-brazil/players.txt
13
15
  test/helper.rb
14
16
  test/test_models.rb
17
+ test/test_reader.rb
@@ -6,7 +6,56 @@ module PersonDb
6
6
  class Person < ActiveRecord::Base
7
7
 
8
8
  self.table_name = 'persons'
9
- ## to be done
9
+
10
+ def title() name end # alias for name
11
+ def title=(value) self.name = value end # alias for name
12
+
13
+
14
+ def self.create_or_update_from_values( new_attributes, values )
15
+
16
+ ## fix: add/configure logger for ActiveRecord!!!
17
+ logger = LogKernel::Logger.root
18
+
19
+ ## check optional values
20
+ values.each_with_index do |value, index|
21
+ if value =~ /^[a-z]{2}$/ ## assume two-letter country key e.g. at,de,mx,etc.
22
+ value_country = Country.find_by_key!( value )
23
+ new_attributes[ :country_id ] = value_country.id
24
+ elsif value =~ /^[A-Z]{3}$/ ## assume three-letter code e.g. AUS, MAL, etc.
25
+ new_attributes[ :code ] = value
26
+ elsif value =~ /^([0-9]{1,2})\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s([0-9]{4})$/ ## assume birthday
27
+ value_date_str = '%02d/%s/%d' % [$1, $2, $3] ## move to matcher!!
28
+ value_date = Date.strptime( value_date_str, '%d/%b/%Y' ) ## %b - abbreviated month name (e.g. Jan,Feb, etc.)
29
+ logger.debug " birthday #{value_date_str} - #{value_date}"
30
+ new_attributes[ :born_at ] = value_date
31
+ ## todo: convert to date
32
+ else
33
+ ## todo: assume title2 ??
34
+ ## assume title2 if title2 is empty (not already in use)
35
+ ## and if it title2 contains at least two letter e.g. [a-zA-Z].*[a-zA-Z]
36
+ # issue warning: unknown type for value
37
+ logger.warn "unknown type for value >#{value}< - key #{new_attributes[:key]}"
38
+ end
39
+ end
40
+
41
+ ## quick hack: set nationality_id if not present to country_id
42
+ if new_attributes[ :nationality_id ].blank?
43
+ new_attributes[ :nationality_id ] = new_attributes[ :country_id ]
44
+ end
45
+
46
+ rec = Person.find_by_key( new_attributes[ :key ] )
47
+ if rec.present?
48
+ logger.debug "update Person #{rec.id}-#{rec.key}:"
49
+ else
50
+ logger.debug "create Person:"
51
+ rec = Person.new
52
+ end
53
+
54
+ logger.debug new_attributes.to_json
55
+
56
+ rec.update_attributes!( new_attributes )
57
+ end # create_or_update_from_values
58
+
10
59
 
11
60
  end # class Person
12
61
 
@@ -0,0 +1,33 @@
1
+ # encoding: UTF-8
2
+
3
+ module PersonDb
4
+
5
+
6
+ class PersonReader
7
+
8
+ include LogUtils::Logging
9
+
10
+ ## make models available by default with namespace
11
+ # e.g. lets you use Usage instead of Model::Usage
12
+ include Models
13
+
14
+
15
+ attr_reader :include_path
16
+
17
+ def initialize( include_path, opts = {} )
18
+ @include_path = include_path
19
+ end
20
+
21
+
22
+ def read( name, more_attribs={} )
23
+ reader = ValuesReaderV2.new( name, include_path, more_attribs )
24
+
25
+ reader.each_line do |new_attributes, values|
26
+ Person.create_or_update_from_values( new_attributes, values )
27
+ end # each lines
28
+ end
29
+
30
+
31
+ end # class PersonReader
32
+ end # module PersonDb
33
+
@@ -1,5 +1,5 @@
1
1
 
2
2
  module PersonDb
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
 
data/lib/persondb.rb CHANGED
@@ -1,15 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ # core and stdlibs
4
+ require 'json'
5
+ require 'uri'
6
+ require 'pp'
1
7
 
2
- # 3rd party gems / libs
3
8
 
9
+ # 3rd party gems / libs
4
10
  require 'active_record' ## todo: add sqlite3? etc.
5
11
 
6
12
  require 'activerecord/utils' # check - if dependency on logutils? or props? etc let others go first
7
13
 
8
14
  require 'logutils'
15
+ require 'logutils/db' # NB: explict require required for LogDb (NOT automatic)
16
+
9
17
  require 'textutils'
10
18
  require 'tagutils'
19
+
11
20
  require 'props'
12
- require 'props/db' # includes ConfDb (ConfDb::Model::Prop, etc.)
21
+ require 'props/db' # NB: explict require required for LogDb (NOT automatic); includes ConfDb (ConfDb::Model::Prop, etc.)
22
+
13
23
  require 'worlddb'
14
24
 
15
25
 
@@ -25,6 +35,7 @@ require 'persondb/models/worlddb/region'
25
35
  require 'persondb/models/worlddb/country'
26
36
 
27
37
  require 'persondb/models/person'
38
+ require 'persondb/reader'
28
39
 
29
40
 
30
41
 
@@ -38,6 +49,10 @@ module PersonDb
38
49
  "#{File.expand_path( File.dirname(File.dirname(__FILE__)) )}"
39
50
  end
40
51
 
52
+ def self.test_data_path
53
+ "#{root}/test/data"
54
+ end
55
+
41
56
 
42
57
  def self.create
43
58
  CreateDb.new.up
@@ -55,6 +70,25 @@ module PersonDb
55
70
  puts " #{Model::Person.count} persons"
56
71
  end
57
72
 
73
+ def self.setup_in_memory_db
74
+ # Database Setup & Config
75
+
76
+ ActiveRecord::Base.logger = Logger.new( STDOUT )
77
+ ## ActiveRecord::Base.colorize_logging = false - no longer exists - check new api/config setting?
78
+
79
+ ## NB: every connect will create a new empty in memory db
80
+ ActiveRecord::Base.establish_connection(
81
+ adapter: 'sqlite3',
82
+ database: ':memory:' )
83
+
84
+ ## build schema
85
+ LogDb.create
86
+ ConfDb.create
87
+ TagDb.create
88
+ WorldDb.create
89
+ PersonDb.create
90
+ end # method setup_in_memory_db
91
+
58
92
 
59
93
  end # module PersonDb
60
94
 
@@ -0,0 +1,39 @@
1
+ ###
2
+ # NB: let official (player) shirt name go first (e.g. Neymar Jr)
3
+
4
+
5
+ ## GK / Goalkeepers
6
+
7
+ Jefferson|Jefferson DE OLIVEIRA GALVAO, 2 Jan 1983, 190
8
+ Júlio César|Júlio César SOARES DE ESPINDOLA, 3 Sep 1979, 186
9
+ D. Cavalieri|Diego CAVALIERI, 1 Dec 1982, 189
10
+
11
+
12
+ ## DF / Defenders
13
+
14
+ Dani Alves|Daniel ALVES DA SILVA, 6 May 1983, 172
15
+ T. Silva|Thiago SILVA|Thiago EMILIANO DA SILVA, 22 Sep 1984, 183
16
+ David Luiz, David Luiz MOREIRA MARINHO, 22 Apr 1987, 189
17
+ Marcelo|Marcelo VIEIRA DA SILVA JUNIOR, 12 May 1988, 174
18
+ Dante|Dante BONFIM COSTA SANTOS, 18 Oct 1983, 188
19
+ Filipe Luís|Filipe Luís KASMIRSKI, 9 Aug 1985, 182
20
+ Réver|Réver Humberto ALVES ARAUJO, 4 Jan 1985, 192
21
+
22
+ ## MF / Midfielders
23
+
24
+ Fernando|Fernando Lucas MARTINS, 3 Mar 1992, 175
25
+ Lucas|Lucas RODRIGUES MOURA SILVA, 13 Aug 1992, 173
26
+ Hernanes|Anderson Hernanes DE CARVALHO VIANA LIMA, 29 May 1985, 180
27
+ Oscar|Oscar DOS SANTOS EMBOABA JUNIOR, 9 Sep 1991, 180
28
+ Jean|Jean Raphael VANDERLEI MOREIRA, 24 Jun 1986, 170
29
+ L. Gustavo|Luiz Gustavo|Luiz Gustavo DIAS, 23 Jul 1987, 186
30
+ Paulinho|José Paulo BEZERRA MACIEL JUNIOR, 25 Jul 1988, 182
31
+ Bernard|Bernard ANICIO CALDEIRA DUARTE, 8 Sep 1992, 164
32
+ Jádson|Jádson RODRIGUES, 5 Oct 1983, 169
33
+
34
+ ## FW / Forwards
35
+
36
+ Fred|Frederico CHAVES GUEDES, 3 Oct 1983, 185
37
+ Neymar Jr|Neymar|Neymar DA SILVA SANTOS JUNIOR, 5 Feb 1992, 174
38
+ Hulk|Givanildo VIEIRA DE SOUSA, 25 Jul 1986, 180
39
+ Jô|João Alves de Assis Silva, 20 Mar 1987
data/test/helper.rb CHANGED
@@ -10,68 +10,23 @@ require 'minitest/autorun'
10
10
  # include MiniTest::Unit # lets us use TestCase instead of MiniTest::Unit::TestCase
11
11
 
12
12
 
13
- # ruby stdlibs
14
-
15
- require 'json'
16
- require 'uri'
17
- require 'pp'
18
-
19
13
  # our own code
20
14
 
21
15
  require 'persondb'
22
- require 'logutils/db' # NB: explict require required for LogDb (not automatic)
23
16
 
17
+ ################
18
+ # shortcuts
24
19
  Country = WorldDb::Model::Country
25
20
  Region = WorldDb::Model::Region
26
21
  City = WorldDb::Model::City
27
22
 
28
23
  ## todo: get all models aliases (e.g. from console script)
29
24
 
30
- Person = PersonDb::Model::Person
31
-
32
-
33
-
34
-
35
- def setup_in_memory_db
36
- # Database Setup & Config
37
-
38
- db_config = {
39
- adapter: 'sqlite3',
40
- database: ':memory:'
41
- }
42
-
43
- pp db_config
44
-
45
- ActiveRecord::Base.logger = Logger.new( STDOUT )
46
- ## ActiveRecord::Base.colorize_logging = false - no longer exists - check new api/config setting?
47
-
48
- ## NB: every connect will create a new empty in memory db
49
- ActiveRecord::Base.establish_connection( db_config )
50
-
51
-
52
- ## build schema
53
-
54
- LogDb.create
55
- ConfDb.create
56
- TagDb.create
57
- WorldDb.create
58
- PersonDb.create
59
- end
60
-
61
-
62
- def fillup_in_memory_db
63
- ## add some counties
64
-
65
- at = Country.create!( key: 'at', title: 'Austria', code: 'AUT', pop: 0, area: 0 )
66
- n = Region.create!( key: 'n', title: 'Niederösterreich', country_id: at.id )
67
- feuersbrunn = City.create!( key: 'feuersbrunn', title: 'Feuersbrunn', country_id: at.id, region_id: n.id )
68
-
69
- end
25
+ Person = PersonDb::Model::Person
26
+ PersonReader = PersonDb::PersonReader
70
27
 
71
- setup_in_memory_db()
72
- fillup_in_memory_db()
73
28
 
74
- AT = Country.find_by_key!( 'at' )
75
- N = Region.find_by_key!( 'n' )
76
- FEUERSBRUNN = City.find_by_key!( 'feuersbrunn' )
29
+ ############
30
+ # setup
31
+ PersonDb.setup_in_memory_db
77
32
 
data/test/test_models.rb CHANGED
@@ -8,12 +8,26 @@ class TestModels < MiniTest::Unit::TestCase
8
8
 
9
9
  def setup # runs before every test
10
10
  PersonDb.delete! # always clean-out tables
11
+ WorldDb.delete!
12
+ add_world
11
13
  end
12
-
14
+
15
+ def add_world
16
+ ## add some counties
17
+ at = Country.create!( key: 'at', title: 'Austria', code: 'AUT', pop: 0, area: 0 )
18
+ n = Region.create!( key: 'n', title: 'Niederösterreich', country_id: at.id )
19
+ feuersbrunn = City.create!( key: 'feuersbrunn', title: 'Feuersbrunn', country_id: at.id, region_id: n.id )
20
+ end
21
+
22
+
13
23
  def test_worlddb_assocs
14
- assert_equal 0, AT.persons.count
15
- assert_equal 0, N.persons.count
16
- assert_equal 0, FEUERSBRUNN.persons.count
24
+ at = Country.find_by_key!( 'at' )
25
+ n = Region.find_by_key!( 'n' )
26
+ feuersbrunn = City.find_by_key!( 'feuersbrunn' )
27
+
28
+ assert_equal 0, at.persons.count
29
+ assert_equal 0, n.persons.count
30
+ assert_equal 0, feuersbrunn.persons.count
17
31
  end
18
32
 
19
33
  def test_count
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+
3
+ require 'helper'
4
+
5
+
6
+ class TestReader < MiniTest::Unit::TestCase
7
+
8
+ def setup # runs before every test
9
+ PersonDb.delete! # always clean-out tables
10
+ WorldDb.delete!
11
+ add_world
12
+ end
13
+
14
+ def add_world
15
+ ## add some counties
16
+ br = Country.create!( key: 'br', title: 'Brazil', code: 'BRA', pop: 0, area: 0 )
17
+ end
18
+
19
+ def test_read
20
+ br = Country.find_by_key!( 'br' )
21
+
22
+ reader = PersonReader.new( PersonDb.test_data_path )
23
+ reader.read( 'players/south-america/br-brazil/players', country_id: br.id )
24
+
25
+ assert_equal 23, Person.count
26
+ assert_equal 23, br.persons.count
27
+ end
28
+
29
+ end # class TestReader
30
+
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: persondb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Gerald Bauer
@@ -13,103 +12,130 @@ date: 2014-05-19 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: props
16
- requirement: &83236560 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *83236560
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: logutils
27
- requirement: &83236220 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
31
  - - ! '>='
31
32
  - !ruby/object:Gem::Version
32
33
  version: '0'
33
34
  type: :runtime
34
35
  prerelease: false
35
- version_requirements: *83236220
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: textutils
38
- requirement: &83235830 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
45
  - - ! '>='
42
46
  - !ruby/object:Gem::Version
43
47
  version: '0'
44
48
  type: :runtime
45
49
  prerelease: false
46
- version_requirements: *83235830
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
47
55
  - !ruby/object:Gem::Dependency
48
56
  name: worlddb
49
- requirement: &83235410 !ruby/object:Gem::Requirement
50
- none: false
57
+ requirement: !ruby/object:Gem::Requirement
51
58
  requirements:
52
59
  - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  type: :runtime
56
63
  prerelease: false
57
- version_requirements: *83235410
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: tagutils
60
- requirement: &83234330 !ruby/object:Gem::Requirement
61
- none: false
71
+ requirement: !ruby/object:Gem::Requirement
62
72
  requirements:
63
73
  - - ! '>='
64
74
  - !ruby/object:Gem::Version
65
75
  version: '0'
66
76
  type: :runtime
67
77
  prerelease: false
68
- version_requirements: *83234330
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: activerecord-utils
71
- requirement: &83249150 !ruby/object:Gem::Requirement
72
- none: false
85
+ requirement: !ruby/object:Gem::Requirement
73
86
  requirements:
74
87
  - - ! '>='
75
88
  - !ruby/object:Gem::Version
76
89
  version: '0'
77
90
  type: :runtime
78
91
  prerelease: false
79
- version_requirements: *83249150
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
80
97
  - !ruby/object:Gem::Dependency
81
98
  name: activerecord
82
- requirement: &83248700 !ruby/object:Gem::Requirement
83
- none: false
99
+ requirement: !ruby/object:Gem::Requirement
84
100
  requirements:
85
101
  - - ! '>='
86
102
  - !ruby/object:Gem::Version
87
103
  version: '0'
88
104
  type: :runtime
89
105
  prerelease: false
90
- version_requirements: *83248700
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
91
111
  - !ruby/object:Gem::Dependency
92
112
  name: rdoc
93
- requirement: &83248000 !ruby/object:Gem::Requirement
94
- none: false
113
+ requirement: !ruby/object:Gem::Requirement
95
114
  requirements:
96
115
  - - ~>
97
116
  - !ruby/object:Gem::Version
98
117
  version: '4.0'
99
118
  type: :development
100
119
  prerelease: false
101
- version_requirements: *83248000
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: '4.0'
102
125
  - !ruby/object:Gem::Dependency
103
126
  name: hoe
104
- requirement: &83247450 !ruby/object:Gem::Requirement
105
- none: false
127
+ requirement: !ruby/object:Gem::Requirement
106
128
  requirements:
107
129
  - - ~>
108
130
  - !ruby/object:Gem::Version
109
- version: '3.11'
131
+ version: '3.10'
110
132
  type: :development
111
133
  prerelease: false
112
- version_requirements: *83247450
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ~>
137
+ - !ruby/object:Gem::Version
138
+ version: '3.10'
113
139
  description: persondb - person schema n models for easy (re)use
114
140
  email: webslideshow@googlegroups.com
115
141
  executables: []
@@ -129,14 +155,18 @@ files:
129
155
  - lib/persondb/models/worlddb/city.rb
130
156
  - lib/persondb/models/worlddb/country.rb
131
157
  - lib/persondb/models/worlddb/region.rb
158
+ - lib/persondb/reader.rb
132
159
  - lib/persondb/schema.rb
133
160
  - lib/persondb/version.rb
161
+ - test/data/players/south-america/br-brazil/players.txt
134
162
  - test/helper.rb
135
163
  - test/test_models.rb
164
+ - test/test_reader.rb
136
165
  - .gemtest
137
166
  homepage: https://github.com/geraldb/person.db.ruby
138
167
  licenses:
139
168
  - Public Domain
169
+ metadata: {}
140
170
  post_install_message:
141
171
  rdoc_options:
142
172
  - --main
@@ -144,22 +174,21 @@ rdoc_options:
144
174
  require_paths:
145
175
  - lib
146
176
  required_ruby_version: !ruby/object:Gem::Requirement
147
- none: false
148
177
  requirements:
149
178
  - - ! '>='
150
179
  - !ruby/object:Gem::Version
151
180
  version: 1.9.2
152
181
  required_rubygems_version: !ruby/object:Gem::Requirement
153
- none: false
154
182
  requirements:
155
183
  - - ! '>='
156
184
  - !ruby/object:Gem::Version
157
185
  version: '0'
158
186
  requirements: []
159
187
  rubyforge_project:
160
- rubygems_version: 1.8.17
188
+ rubygems_version: 2.1.10
161
189
  signing_key:
162
- specification_version: 3
190
+ specification_version: 4
163
191
  summary: persondb - person schema n models for easy (re)use
164
192
  test_files:
165
193
  - test/test_models.rb
194
+ - test/test_reader.rb