Permalink
Browse files

move title finder/mapper/utils to textutils

  • Loading branch information...
1 parent aafb7dd commit e2eecf13a963a4cba43af822c5f18687a6633c79 @geraldb geraldb committed Feb 16, 2014
View
@@ -75,7 +75,6 @@ lib/sportdb/service/views/debug.erb
lib/sportdb/service/views/index.erb
lib/sportdb/service/views/layout.erb
lib/sportdb/stats.rb
-lib/sportdb/title.rb
lib/sportdb/updater.rb
lib/sportdb/utils.rb
lib/sportdb/version.rb
View
@@ -29,7 +29,6 @@
require 'sportdb/version'
-require 'sportdb/title' ## fix - move to textutils gem
require 'sportdb/models/forward'
require 'sportdb/models/world/city'
require 'sportdb/models/world/country'
View
@@ -1,10 +1,18 @@
module SportDb::Model
+##################
+# FIX: add ?
+#
+# use single table inheritance STI ????
+# - to mark two dervided classes e.g.
+# - Club ??? - why? why not?
+# - NationalTeam ??? - why? why not?
+
class Team < ActiveRecord::Base
- has_many :home_games, :class_name => 'Game', :foreign_key => 'team1_id'
- has_many :away_games, :class_name => 'Game', :foreign_key => 'team2_id'
+ has_many :home_games, class_name: 'Game', foreign_key: 'team1_id'
+ has_many :away_games, class_name: 'Game', foreign_key: 'team2_id'
REGEX_KEY = /^[a-z]{3,}$/
REGEX_CODE = /^[A-Z][A-Z0-9][A-Z0-9_]?$/ # must start w/ letter a-z (2 n 3 can be number or underscore _)
@@ -13,10 +21,16 @@ class Team < ActiveRecord::Base
validates :key, :format => { :with => REGEX_KEY, :message => 'expected three or more lowercase letters a-z' }
validates :code, :format => { :with => REGEX_CODE, :message => 'expected two or three uppercase letters A-Z (and 0-9_; must start with A-Z)' }, :allow_nil => true
- has_many :event_teams, :class_name => 'EventTeam' # join table (events+teams)
+ has_many :event_teams, class_name: 'EventTeam' # join table (events+teams)
has_many :events, :through => :event_teams
+
### fix!!! - how to do it with has_many macro? use finder_sql?
+ ## finder_sql is depreciated in Rails 4!!!
+ ## keep as is! best solution ??
+ ## a discussion here -> https://github.com/rails/rails/issues/9726
+ ## a discussion here (not really helpful) -> http://stackoverflow.com/questions/2125440/activerecord-has-many-where-two-columns-in-table-a-are-primary-keys-in-table-b
+
def games
Game.where( 'team1_id = ? or team2_id = ?', id, id ).order( 'play_at' )
end
@@ -32,8 +46,8 @@ def past_games
has_many :badges # Winner, 2nd, Cupsieger, Aufsteiger, Absteiger, etc.
- belongs_to :country, :class_name => 'WorldDb::Model::Country', :foreign_key => 'country_id'
- belongs_to :city, :class_name => 'WorldDb::Model::City', :foreign_key => 'city_id'
+ belongs_to :country, class_name: 'WorldDb::Model::Country', foreign_key: 'country_id'
+ belongs_to :city, class_name: 'WorldDb::Model::City', foreign_key: 'city_id'
@@ -1,11 +1,16 @@
# encoding: utf-8
-## todo: how to best extends city model?
module WorldDb::Model
class City
has_many :teams, class_name: 'SportDb::Model::Team', foreign_key: 'city_id'
+
+ # fix: require active record 4
+ # has_many :clubs, -> { where club: true }, class_name: 'SportDb::Model::Team', foreign_key: 'city_id'
+ # has_many :national_teams, -> { where club: false }, class_name: 'SportDb::Model::Team', foreign_key: 'city_id'
+
+
has_many :grounds, class_name: 'SportDb::Model::Ground', foreign_key: 'city_id'
has_many :games, :through => :grounds
end
@@ -3,7 +3,13 @@
module WorldDb::Model
class Continent
- has_many :teams, :through => :countries
+ has_many :teams, :through => :countries
+
+ # fix: require active record 4
+ # has_many :clubs, :through => :countries
+ # has_many :national_teams, :through => :countries
+
+
has_many :leagues, :through => :countries
has_many :grounds, :through => :countries
end # class Continent
@@ -1,11 +1,14 @@
# encoding: utf-8
-## todo: how to best extends country model?
-
module WorldDb::Model
class Country
- has_many :teams, class_name: 'SportDb::Model::Team', foreign_key: 'country_id'
+ has_many :teams, class_name: 'SportDb::Model::Team', foreign_key: 'country_id'
+
+ # fix: require active record 4
+ # has_many :clubs, -> { where club: true }, class_name: 'SportDb::Model::Team', foreign_key: 'country_id'
+ # has_many :national_teams, -> { where club: false }, class_name: 'SportDb::Model::Team', foreign_key: 'country_id'
+
has_many :leagues, class_name: 'SportDb::Model::League', foreign_key: 'country_id'
has_many :grounds, class_name: 'SportDb::Model::Ground', foreign_key: 'country_id'
end # class Country
@@ -1,11 +1,15 @@
# encoding: utf-8
-## todo: how to best extends country model?
module WorldDb::Model
class Region
- has_many :teams, class_name: 'SportDb::Model::Team', :through => :cities
+ has_many :teams, class_name: 'SportDb::Model::Team', :through => :cities
+
+ # fix: require active record 4
+ # has_many :clubs, class_name: 'SportDb::Model::Team', :through => :cities
+ # has_many :national_teams, class_name: 'SportDb::Model::Team', :through => :cities
+
has_many :grounds, class_name: 'SportDb::Model::Ground', :through => :cities
end # class Region
View
@@ -5,7 +5,6 @@ class Stats
include SportDb::Models
def tables
- puts "Stats:"
puts " #{League.count} leagues / #{Season.count} seasons"
puts " #{Event.count} events (league+season recs) / #{Round.count} rounds / #{Group.count} groups"
puts " #{Team.count} teams"
@@ -18,10 +17,6 @@ def tables
puts " #{Goal.count} goals (person+game recs)"
puts " #{Ground.count} grounds|stadiums"
-
- puts " #{Country.count} countries / #{Region.count} regions / #{City.count} cities"
-
- ## todo: add tags / taggings from worlddb
end
def props
View
@@ -1,146 +0,0 @@
-# encoding: utf-8
-
-################
-# todo: move module to textutils!!!
-
-### fix: move to textutils??
-
-
-## todo: rename to TitleHelpers? TitleMatcher? TitleMapper? TitleMapping? TitleMappings? TitleFinder? TitleHelpers?
-# or rename to KeyMapping?, KeyMapper?, KeyTable? etc.
-
-
-module TextUtils::TitleTable
-
-
-def build_title_table_for( records )
- ## build known tracks table w/ synonyms e.g.
- #
- # [[ 'wolfsbrug', [ 'VfL Wolfsburg' ]],
- # [ 'augsburg', [ 'FC Augsburg', 'Augi2', 'Augi3' ]],
- # [ 'stuttgart', [ 'VfB Stuttgart' ]] ]
-
- known_titles = []
-
- records.each_with_index do |rec,index|
-
- title_candidates = []
- title_candidates << rec.title
-
- title_candidates += rec.synonyms.split('|') if rec.synonyms.present?
-
-
- ## check if title includes subtitle e.g. Grand Prix Japan (Suzuka Circuit)
- # make subtitle optional by adding title w/o subtitle e.g. Grand Prix Japan
-
- titles = []
- title_candidates.each do |t|
- titles << t
- if t =~ /\(.+\)/
- extra_title = t.gsub( /\(.+\)/, '' ) # remove/delete subtitles
- extra_title.strip! # strip leading n trailing withspaces too!
- titles << extra_title
- end
- end
-
-
- ## NB: sort here by length (largest goes first - best match)
- # exclude code and key (key should always go last)
- titles = titles.sort { |left,right| right.length <=> left.length }
-
- ## escape for regex plus allow subs for special chars/accents
- titles = titles.map { |title| TextUtils.title_esc_regex( title ) }
-
- ## NB: only include code field - if defined
- titles << rec.code if rec.respond_to?(:code) && rec.code.present?
-
- known_titles << [ rec.key, titles ]
-
- ### fix: use plain logger
- LogUtils::Logger.root.debug " #{rec.class.name}[#{index+1}] #{rec.key} >#{titles.join('|')}<"
- end
-
- known_titles
-end
-
-
-
-def find_key_for!( name, line )
- regex = /@@oo([^@]+?)oo@@/ # e.g. everything in @@ .... @@ (use non-greedy +? plus all chars but not @, that is [^@])
-
- upcase_name = name.upcase
- downcase_name = name.downcase
-
- if line =~ regex
- value = "#{$1}"
- ### fix: use plain logger
- LogUtils::Logger.root.debug " #{downcase_name}: >#{value}<"
-
- line.sub!( regex, "[#{upcase_name}]" )
-
- return $1
- else
- return nil
- end
-end
-
-
-def find_keys_for!( name, line ) # NB: keys (plural!) - will return array
- counter = 1
- keys = []
-
- downcase_name = name.downcase
-
- key = find_key_for!( "#{downcase_name}#{counter}", line )
- while key.present?
- keys << key
- counter += 1
- key = find_key_for!( "#{downcase_name}#{counter}", line )
- end
-
- keys
-end
-
-
-def map_titles_for!( name, line, title_table )
- title_table.each do |rec|
- key = rec[0]
- values = rec[1]
- map_title_worker_for!( name, line, key, values )
- end
-end
-
-
-def map_title_worker_for!( name, line, key, values )
-
- downcase_name = name.downcase
-
- values.each do |value|
- ## nb: \b does NOT include space or newline for word boundry (only alphanums e.g. a-z0-9)
- ## (thus add it, allows match for Benfica Lis. for example - note . at the end)
-
- ## check add $ e.g. (\b| |\t|$) does this work? - check w/ Benfica Lis.$
- regex = /\b#{value}(\b| |\t|$)/ # wrap with world boundry (e.g. match only whole words e.g. not wac in wacker)
- if line =~ regex
- ### fix: use plain logger
- LogUtils::Logger.root.debug " match for #{downcase_name} >#{key}< >#{value}<"
- # make sure @@oo{key}oo@@ doesn't match itself with other key e.g. wacker, wac, etc.
- line.sub!( regex, "@@oo#{key}oo@@ " ) # NB: add one space char at end
- return true # break out after first match (do NOT continue)
- end
- end
- return false
-end
-
-
-
-end # module TextUtils::TitleTable
-
-
-
-## auto-include methods
-
-module TextUtils
- # make helpers available as class methods e.g. TextUtils.convert_unicode_dashes_to_plain_ascii
- extend TitleTable # lets us use TextUtils.build_title_table_for etc.
-end
View
@@ -1,6 +1,6 @@
module SportDb
- VERSION = '1.8.3'
+ VERSION = '1.8.4'
end

0 comments on commit e2eecf1

Please sign in to comment.