Module: SportDb

Defined in:
lib/sportdb/models/models/forward.rb,
lib/sportdb/models.rb,
lib/sportdb/models.rb,
lib/sportdb/models/stats.rb,
lib/sportdb/models/utils.rb,
lib/sportdb/models/schema.rb,
lib/sportdb/models/deleter.rb,
lib/sportdb/models/formats.rb,
lib/sportdb/models/version.rb,
lib/sportdb/models/models/goal.rb,
lib/sportdb/models/models/team.rb,
lib/sportdb/models/models/assoc.rb,
lib/sportdb/models/models/badge.rb,
lib/sportdb/models/models/event.rb,
lib/sportdb/models/models/group.rb,
lib/sportdb/models/models/match.rb,
lib/sportdb/models/models/round.rb,
lib/sportdb/models/models/stage.rb,
lib/sportdb/models/models/ground.rb,
lib/sportdb/models/models/league.rb,
lib/sportdb/models/models/lineup.rb,
lib/sportdb/models/models/season.rb,
lib/sportdb/models/models/stats/event_standing.rb,
lib/sportdb/models/models/stats/group_standing.rb,
lib/sportdb/models/models/stats/alltime_standing.rb

Overview

forward references

require first to resolve circular references

Defined Under Namespace

Modules: Model, Module Classes: CreateDb, Deleter, Stats

Constant Summary collapse

TEAM_KEY_RE =

todo: add a patterns.md page to github ??

- add regexper pics??
%r{ \A
 [a-z_][a-z0-9_]*
\z}x
TEAM_KEY_MESSAGE =
"expected one or more lowercase letters a-z (or 0-9 or _; must start with a-z or _)"
TEAM_CODE_RE =

must start w/ letter A-Z (2nd,3rd,4th or 5th can be number)

%r{ \A
 [A-Z_][A-Z0-9_]*
\z}x
TEAM_CODE_MESSAGE =
"expected one or more uppercase letters A-Z (or 0-9 or _; must start with A-Z or _)"
VERSION =

add convenience shortcuts

Module::Models::VERSION
Models =

add backwards compatible n convenience namespace

Model

Class Method Summary collapse

Class Method Details

.auto_migrate!Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sportdb/models.rb', line 62

def self.auto_migrate!
  ### todo/fix:
  ##    check props table and versions!!!!!

  # first time? - auto-run db migratation, that is, create db tables
  unless LogDb::Model::Log.table_exists?
    LogDb.create     # add logs table
  end

  unless ConfDb::Model::Prop.table_exists?
    ConfDb.create    # add props table
  end


  unless TagDb::Model::Tag.table_exists?
    TagDb.create    # add tags & taggings tables
  end

  unless WorldDb::Model::Place.table_exists?
    WorldDb.create   # add places, & co. tables
  end

  unless PersonDb::Model::Person.table_exists?
    PersonDb.create  # add persons table
  end

  unless SportDb::Model::League.table_exists?
    SportDb.create
  end
end

.connect(config = {}) ⇒ Object



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
165
166
167
# File 'lib/sportdb/models.rb', line 113

def self.connect( config={} )
  if config.empty?
    puts "ENV['DATBASE_URL'] - >#{ENV['DATABASE_URL']}<"

    ### change default to ./sport.db ?? why? why not?
    db = URI.parse( ENV['DATABASE_URL'] || 'sqlite3:///sport.db' )

    config =  if db.scheme == 'postgres'
                  { adapter: 'postgresql',
                    host:     db.host,
                    port:     db.port,
                    username: db.user,
                    password: db.password,
                    database: db.path[1..-1],
                    encoding: 'utf8'
                  }
                else # assume sqlite3
                  { adapter:  db.scheme,       # sqlite3
                    database: db.path[1..-1]   # sport.db (NB: cut off leading /, thus 1..-1)
                  }
                end
  else
    ## note: for compatibility lets you also pass-in/use string keys
    ##   e.g. YAML.load uses/returns always string keys - always auto-convert to symbols
    config = config.symbolize_keys
  end


  ## todo/check/fix: move jruby "hack" to attic - why? why not?
  ## todo/check: use if defined?( JRUBY_VERSION ) instead ??
  ## if RUBY_PLATFORM =~ /java/ && config[:adapter] == 'sqlite3'
    # quick hack for JRuby sqlite3 support via jdbc
  ##  puts "jruby quick hack - adding jdbc libs for jruby sqlite3 database support"
  ##  require 'jdbc/sqlite3'
  ##  require 'active_record/connection_adapters/jdbc_adapter'
  ##  require 'active_record/connection_adapters/jdbcsqlite3_adapter'
  ## end

  puts "Connecting to db using settings: "
  pp config
  ActiveRecord::Base.establish_connection( config )
  # ActiveRecord::Base.logger = Logger.new( STDOUT )

  ## if sqlite3 add (use) some pragmas for speedups
  if config[:adapter]  == 'sqlite3'  &&
     config[:database] != ':memory:'
    ## note: if in memory database e.g. ':memory:' no pragma needed!!
    ## try to speed up sqlite
    ##   see http://www.sqlite.org/pragma.html
    con = ActiveRecord::Base.connection
    con.execute( 'PRAGMA synchronous=OFF;' )
    con.execute( 'PRAGMA journal_mode=OFF;' )
    con.execute( 'PRAGMA temp_store=MEMORY;' )
  end
end

.connect!(config = {}) ⇒ Object

use/change to **config - to allow “inline” conif with keywords

(without enclosing {}) - why? why not?


108
109
110
111
# File 'lib/sportdb/models.rb', line 108

def self.connect!( config={} )  # convenience shortcut w/ automigrate
  connect( config )
  auto_migrate!
end

.createObject



47
48
49
50
# File 'lib/sportdb/models.rb', line 47

def self.create
  CreateDb.new.up
  ConfDb::Model::Prop.create!( key: 'db.schema.sport.version', value: VERSION )
end

.create_allObject



52
53
54
55
56
57
58
59
60
# File 'lib/sportdb/models.rb', line 52

def self.create_all
  ## build schema - convenience helper
  LogDb.create
  ConfDb.create
  TagDb.create
  WorldDb.create
  PersonDb.create
  SportDb.create
end

.delete!Object

delete ALL records (use with care!)



95
96
97
98
# File 'lib/sportdb/models.rb', line 95

def self.delete!
  puts '*** deleting sport table records/data...'
  Deleter.new.run
end

.open(path) ⇒ Object

shortcut for sqlite only



171
172
173
174
175
176
177
# File 'lib/sportdb/models.rb', line 171

def self.open( path )  ## shortcut for sqlite only
  config = {
    adapter:  'sqlite3',
    database: path  # e.g. ':memory', './sport.db'
  }
  connect!( config )
end

.setup_in_memory_dbObject Also known as: open_mem, open_memory, open_in_memory



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/sportdb/models.rb', line 179

def self.setup_in_memory_db
  # Database Setup & Config
  ##  add logger as option - why? why not?
  ##  e.g.  open_mem( logger: true ) or
  ##         always possible to add logger "manually" before 
  ##         thus, no need for option/flag really ??
  ## ActiveRecord::Base.logger = Logger.new( STDOUT )
  ## ActiveRecord::Base.colorize_logging = false  - no longer exists - check new api/config setting?
 
  config = {
    adapter:  'sqlite3',
    database: ':memory:'
  }
  connect( config )

  ## build schema
  create_all
end

.tablesObject



101
102
103
# File 'lib/sportdb/models.rb', line 101

def self.tables
  Stats.new.tables
end