Class: WorldDB::Runner
- Inherits:
-
Object
- Object
- WorldDB::Runner
- Includes:
- Models
- Defined in:
- lib/worlddb/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
-
#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.
15 16 17 |
# File 'lib/worlddb/cli/runner.rb', line 15 def logger @logger end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
15 16 17 |
# File 'lib/worlddb/cli/runner.rb', line 15 def opts @opts end |
Instance Method Details
#run(args) ⇒ Object
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 112 113 |
# File 'lib/worlddb/cli/runner.rb', line 18 def run( args ) opt=OptionParser.new do |cmd| cmd. = "Usage: worlddb [options]" ## NB: reserve -c for use with -c/--config cmd.on( '--create', 'Create DB schema' ) { opts.create = true } cmd.on( '--setup', "Create DB schema 'n' load builtin world data" ) { opts.setup = true } ### 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( '--country KEY', "Default country for regions 'n' cities" ) { |key| opts.country = key; } ## todo: add (generic) alias -t/--type cities for --cities ## -t/--type regions for --regions ## -t/--type countries for --countries --why? why not? cmd.on( '--countries', 'Use country plain text fixture reader' ) { opts.countries = true } cmd.on( '--regions', 'Use regions plain text fixture reader' ) { opts.regions = true } cmd.on( '--cities', 'Use cities plain text fixture reader' ) { opts.cities = true } ## todo: change to --builtin?? more clear why? why not? cmd.on( '--load', 'Use loader for builtin world data' ) { opts.load = true } cmd.on( '--delete', 'Delete all records' ) { opts.delete = true } cmd.on( '-v', '--version', "Show version" ) do puts WorldDB. 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 worlddb - world.db command line tool, version #{VERSION} #{cmd.help} Examples: worlddb at/cities # import austrian cities worlddb --create # create database schema More Examples: worlddb # show stats (table counts, table props) Further information: http://geraldb.github.com/world.db EOS exit end end opt.parse!( args ) puts WorldDB. puts "working directory: #{Dir.pwd}" db_config = { adapter: 'sqlite3', database: "#{opts.output_path}/world.db" } puts "Connecting to db using settings: " pp db_config ActiveRecord::Base.establish_connection( db_config ) if opts.setup? WorldDB.create WorldDB.read_all else WorldDB.create if opts.create? WorldDB.delete! if opts.delete? if opts.countries? || opts.regions? || opts.cities? Reader.new( logger ).run( opts, args ) # load/read plain text country/region/city fixtures else Loader.new( logger ).run( opts, args ) # load ruby fixtures end end WorldDB.stats puts 'Done.' end |