Class: Sports::Team

Inherits:
Object
  • Object
show all
Includes:
SportDb::NameHelper
Defined in:
lib/sports/structs/team.rb

Overview

todo/fix: remove self.create in structs!!! use just new!!!

Direct Known Subclasses

Club, NationalTeam

Constant Summary

Constants included from SportDb::NameHelper

SportDb::NameHelper::LANG_RE, SportDb::NameHelper::NORM_RE, SportDb::NameHelper::YEAR_RE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SportDb::NameHelper

#has_lang?, #has_year?, #normalize, #sanitize, #strip_lang, #strip_norm, #strip_year, #variants

Constructor Details

#initialize(**kwargs) ⇒ Team

Returns a new instance of Team.



49
50
51
52
53
54
# File 'lib/sports/structs/team.rb', line 49

def initialize( **kwargs )
  @alt_names      = []
  @alt_names_auto = []

  update( kwargs )  unless kwargs.empty?
end

Instance Attribute Details

#alt_namesObject

todo: use just names for alt_names - why? why not?



9
10
11
# File 'lib/sports/structs/team.rb', line 9

def alt_names
  @alt_names
end

#alt_names_autoObject

special import only attribs



28
29
30
# File 'lib/sports/structs/team.rb', line 28

def alt_names_auto
  @alt_names_auto
end

#codeObject

todo: use just names for alt_names - why? why not?



9
10
11
# File 'lib/sports/structs/team.rb', line 9

def code
  @code
end

#countryObject

todo: use just names for alt_names - why? why not?



9
10
11
# File 'lib/sports/structs/team.rb', line 9

def country
  @country
end

#keyObject

all names



9
10
11
# File 'lib/sports/structs/team.rb', line 9

def key
  @key
end

#nameObject

todo: use just names for alt_names - why? why not?



9
10
11
# File 'lib/sports/structs/team.rb', line 9

def name
  @name
end

#wikipediaObject

wikipedia page name (for english (en))



29
30
31
# File 'lib/sports/structs/team.rb', line 29

def wikipedia
  @wikipedia
end

#yearObject

todo: use just names for alt_names - why? why not?



9
10
11
# File 'lib/sports/structs/team.rb', line 9

def year
  @year
end

#year_endObject

todo: use just names for alt_names - why? why not?



9
10
11
# File 'lib/sports/structs/team.rb', line 9

def year_end
  @year_end
end

Instance Method Details

#add_variants(name_or_names) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/sports/structs/team.rb', line 90

def add_variants( name_or_names )
  names = name_or_names.is_a?(Array) ? name_or_names : [name_or_names]
  names.each do |name|
    name = sanitize( name )
    self.alt_names_auto += variants( name )
  end
end

#duplicatesObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/sports/structs/team.rb', line 78

def duplicates
  names = [name] + alt_names + alt_names_auto

  ## calculate (count) frequency and select if greater than one
  names.reduce( {} ) do |h,name|
     norm = normalize( sanitize(name) )
     h[norm] ||= []
     h[norm] << name; h
  end.select { |norm,names| names.size > 1 }
end

#duplicates?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/sports/structs/team.rb', line 71

def duplicates?
  names = [name] + alt_names + alt_names_auto
  names = names.map { |name| normalize( sanitize(name) ) }

  names.size != names.uniq.size
end

#historic?Boolean Also known as: past?

Returns:

  • (Boolean)


32
# File 'lib/sports/structs/team.rb', line 32

def historic?()  @year_end ? true : false; end

#namesObject



15
16
17
18
# File 'lib/sports/structs/team.rb', line 15

def names
  ## todo/check: add alt_names_auto too? - why? why not?
  [@name] + @alt_names
end

#update(**kwargs) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/sports/structs/team.rb', line 56

def update( **kwargs )
  @key         = kwargs[:key]        if kwargs.has_key? :key
  @name        = kwargs[:name]       if kwargs.has_key? :name
  @code        = kwargs[:code]       if kwargs.has_key? :code
  @alt_names   = kwargs[:alt_names]  if kwargs.has_key? :alt_names
  self   ## note - MUST return self for chaining
end

#wikipedia?Boolean

Returns:

  • (Boolean)


35
# File 'lib/sports/structs/team.rb', line 35

def wikipedia?()  @wikipedia; end

#wikipedia_urlObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sports/structs/team.rb', line 36

def wikipedia_url
  if @wikipedia
    ##  note: replace spaces with underscore (-)
    ##  e.g. Club Brugge KV => Club_Brugge_KV
    ##  todo/check/fix:
    ##    check if "plain" dash (-) needs to get replaced with typographic dash??
    "https://en.wikipedia.org/wiki/#{@wikipedia.gsub(' ','_')}"
  else
    nil
  end
end