Class: SportDB::Runner

Inherits:
Object
  • Object
show all
Includes:
Models
Defined in:
lib/sportdb/cli/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



11
12
13
14
15
16
# File 'lib/sportdb/cli/runner.rb', line 11

def initialize
  @logger = Logger.new(STDOUT)
  @logger.level = Logger::INFO

  @opts    = Opts.new
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/sportdb/cli/runner.rb', line 18

def logger
  @logger
end

#optsObject (readonly)

Returns the value of attribute opts.



18
19
20
# File 'lib/sportdb/cli/runner.rb', line 18

def opts
  @opts
end

Instance Method Details

#dump_propsObject



130
131
132
133
134
135
136
# File 'lib/sportdb/cli/runner.rb', line 130

def dump_props
  # todo: use %5 or similar to format string
  puts "Props:"
  Prop.order( 'created_at asc' ).all.each do |prop|
    puts "  #{prop.key} / #{prop.value} || #{prop.created_at}"
  end
end

#dump_statsObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/sportdb/cli/runner.rb', line 119

def dump_stats
  # todo: use %5d or similar to format string
  puts "Stats:"
  puts "  #{Event.count} events  /  #{Round.count} rounds  /  #{Group.count} groups"
  puts "  #{League.count} leagues  /  #{Season.count} seasons"
  puts "  #{Country.count} countries / #{Region.count} regions / #{City.count} cities"
  puts "  #{Team.count} teams"
  puts "  #{Game.count} games"
  puts "  #{Badge.count} badges"
end

#run(args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sportdb/cli/runner.rb', line 21

def run( args )
  opt=OptionParser.new do |cmd|
  
    cmd.banner = "Usage: sportdb [options]"

    cmd.on( '-e', '--event KEY', 'Event to Load or Generate' ) { |key| opts.event = key; }
    cmd.on( '-g', '--generate', 'Generate Fixtures from Template' ) { opts.generate = true }

    ## todo: change to different flag??   use -c/--config ???
    cmd.on( '-c', '--create', 'Create DB Schema' ) { opts.create = true }

    cmd.on( '--delete', 'Delete all records' ) { opts.delete = true }
    
    cmd.on( '--load', 'Use Loader for Builtin Sports Data' ) { opts.load = true }
    
    cmd.on( '-o', '--output PATH', "Output Path (default is #{opts.output_path})" ) { |path| opts.output_path = path }

    ### todo: in future allow multiple search path??
    cmd.on( '-i', '--include PATH', "Data Path (default is #{opts.data_path})" ) { |path| opts.data_path = path }

    cmd.on( '-v', '--version', "Show version" ) do
      puts SportDB.banner
      exit
    end

    cmd.on( "--verbose", "Show debug trace" )  do
      logger.datetime_format = "%H:%H:%S"
      logger.level = Logger::DEBUG
      
      ActiveRecord::Base.logger = Logger.new(STDOUT)
    end

    cmd.on_tail( "-h", "--help", "Show this message" ) do
      puts <<EOS

sportdb - sport.db command line tool, version #{VERSION}

#{cmd.help}

Examples:
  sportdb cl/teams cl/2012_13/cl                     # import champions league (cl)
  sportdb -c                                         # create database schema

More Examples:
  sportdb                                            # show stats (table counts, table props)
  sportdb -i ../sport.db/db cl/teams cl/2012_13/cl   # import champions league (cl) in db folder

Further information:
http://geraldb.github.com/sport.db

EOS
      exit
    end
  end

  opt.parse!( args )

  puts SportDB.banner

  puts "working directory: #{Dir.pwd}"
 
  db_config = {
   :adapter  => 'sqlite3',
   :database => "#{opts.output_path}/sport.db"
  }

  puts "Connecting to db using settings: "
  pp db_config

  ActiveRecord::Base.establish_connection( db_config )
  
  if opts.create?
    CreateDB.up
  end
  
  if opts.delete?
    SportDB.delete!
  end

  if opts.event.present?
    if opts.generate?
      Templater.new( logger ).run( opts, args ) # export/generate ruby fixtures
    else
      Reader.new( logger ).run( opts, args )  # load/read plain text fixtures
    end
  else
    Loader.new( logger ).run( opts, args ) # load ruby fixtures
  end

  
  dump_stats
  dump_props
  
  puts 'Done.'
  
end