Class: SportDB::Runner
- Inherits:
-
Object
- Object
- SportDB::Runner
- Defined in:
- lib/sportdb/cli/runner.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #dump_props ⇒ Object
-
#dump_stats ⇒ Object
method run.
-
#initialize ⇒ Runner
constructor
A new instance of Runner.
- #run(args) ⇒ Object
Constructor Details
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
13 14 15 |
# File 'lib/sportdb/cli/runner.rb', line 13 def logger @logger end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
13 14 15 |
# File 'lib/sportdb/cli/runner.rb', line 13 def opts @opts end |
Instance Method Details
#dump_props ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/sportdb/cli/runner.rb', line 121 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_stats ⇒ Object
method run
113 114 115 116 117 118 119 |
# File 'lib/sportdb/cli/runner.rb', line 113 def dump_stats # todo: use %5d or similar to format string puts "Stats:" puts " #{Event.count} events" puts " #{Team.count} teams" puts " #{Game.count} games" end |
#run(args) ⇒ Object
15 16 17 18 19 20 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 |
# File 'lib/sportdb/cli/runner.rb', line 15 def run( args ) opt=OptionParser.new do |cmd| cmd. = "Usage: sportdb [options]" ## todo: change to different flag?? use -c/--config ??? cmd.on( '-c', '--create', 'Create DB Schema' ) { opts.create = 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. exit end cmd.on( "--verbose", "Show debug trace" ) do logger.datetime_format = "%H:%H:%S" logger.level = Logger::DEBUG ## todo: set ActiveRecord Logger to stdout?? end cmd.on_tail( "-h", "--help", "Show this message" ) do puts <<EOS sportdb - sport.db command line tool #{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. 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 args.each do |arg| name = arg # File.basename( arg, '.*' ) path = "#{opts.data_path}/#{name}.rb" puts "*** loading data '#{name}' (#{path})..." text = File.read( path ) SportDB.module_eval( text ) # NB: same as # # module SportDB # <code here> # end # require path # require "#{Dir.pwd}/db/#{seed}.rb" # Prop.create!( :key => "db.#{name}.version", :value => SportDB::VERSION ) end dump_stats dump_props puts 'Done.' end |