Class: DataMapper::CLI
- Object
- DataMapper::CLI
Attributes
Class Attributes
| config | [RW] | public |
Sets the attribute config. |
|---|---|---|---|
| options | [RW] | public |
Sets the attribute options. |
Public Visibility
Public Class Method Summary
| configure(args) | |
|---|---|
| load_models | |
| parse_args(argv = ARGV) | |
| start(argv = ARGV) | |
| usage |
Public Instance Methods Inherited from Object
Public Class Method Details
configure
public
configure(args)
[View source]
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 168 169 170 171 172 173 174
# File 'dm-more/dm-cli/lib/data_mapper/cli.rb', line 133 def configure(args) if args[0] && args[0].match(/^(.+):\/\/(?:(.*)(?::(.+))?@)?(.+)\/(.+)$/) @options = { :adapter => $1, :username => $2, :password => $3, :host => $4, :database => $5 } @config = @options.merge(:connection_string => ARGV.shift) else parse_args(args) @config[:environment] ||= "development" if @config[:config] @config.merge!(YAML::load_file(@config[:config])) @options = @config[:options] elsif @config[:yaml] @config.merge!(YAML::load_file(@config[:yaml])) @options = @config[@config[:environment]] || @config[@config[:environment].to_sym] raise "Options for environment '#{@config[:environment]}' are missing." if @options.nil? else @options = { :adapter => @config[:adapter], :username => @config[:username], :password => @config[:password], :host => @config[:host], :database => @config[:database] } end end end
load_models
public
load_models
[View source]
168 169 170 171 172 173
# File 'dm-more/dm-cli/lib/data_mapper/cli.rb', line 168 def load_models Pathname.glob("#{config[:models]}/**/*.rb") { |file| load file } end
parse_args
public
parse_args(argv = ARGV)
[View source]
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
# File 'dm-more/dm-cli/lib/data_mapper/cli.rb', line 57 def parse_args(argv = ARGV) @config ||= {} # Build a parser for the command line arguments OptionParser.new do |opt| opt.define_head "DataMapper CLI" opt.banner = usage opt.on("-m", "--models MODELS", "The directory to load models from.") do |models| @config[:models] = Pathname(models) end opt.on("-c", "--config FILE", "Entire configuration structure, useful for testing scenarios.") do |config_file| @config = YAML::load_file Pathname(config_file) end opt.on("--merb", "--rails", "Loads application settings: config/database.yml, app/models.") do @config[:models] = Pathname('app/models') @config[:yaml] = Pathname('config/database.yml') end opt.on("-y", "--yaml YAML", "The database connection configuration yaml file.") do |yaml_file| if (yaml = Pathname(yaml_file)).file? @config[:yaml] = yaml elsif (yaml = Pathname("#{Dir.getwd}/#{yaml_file}")).file? @config[:yaml] = yaml else raise "yaml file was specifed as #{yaml_file} but does not exist." end end opt.on("-l", "--log LOGFILE", "A string representing the logfile to use.") do |log_file| @config[:log_file] = Pathname(log_file) end opt.on("-e", "--environment STRING", "Run merb in the correct mode(development, production, testing)") do |environment| @config[:environment] = environment end opt.on("-a", "--adapter ADAPTER", "Number of merb daemons to run.") do |adapter| @config[:adapter] = adapter end opt.on("-u", "--username USERNAME", "The user to connect to the database as.") do |username| @config[:username] = username end opt.on("-p", "--password PASSWORD", "The password to connect to the database with") do |password| @config[:password] = password end opt.on("-h", "--host HOSTNAME", "Host to connect to.") do |host| @config[:host] = host end opt.on("-s", "--socket SOCKET", "The socket to connect to.") do |socket| @config[:socket] = socket end opt.on("-o", "--port PORT", "The port to connect to.") do |port| @config[:port] = port end opt.on("-d", "--database DATABASENAME", "Name of the database to connect to.") do |database_name| @config[:database] = database_name end opt.on("-?", "-H", "--help", "Show this help message") do puts opt exit end end.parse!(argv) end
start
public
start(argv = ARGV)
[View source]
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
# File 'dm-more/dm-cli/lib/data_mapper/cli.rb', line 172 def start(argv = ARGV) begin configure(argv) DataMapper.setup(:default, options) load_models if config[:models] puts "DataMapper has been loaded using the '#{options[:adapter] || options["adapter"]}' database '#{options[:database] || options["database"]}' on '#{options[:host] || options["host"]}' as '#{options[:username] || options["username"]}'" ENV["IRBRC"] = DataMapper.root / 'bin' / '.irbrc' IRB.start rescue => error puts error.message exit end end
usage
public
usage
[View source]
13 14 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
# File 'dm-more/dm-cli/lib/data_mapper/cli.rb', line 13 def usage "\nNote: If exactly one argument is given the CLI assumes it is a connection string. \n#{'='*80}\n= Examples\n#{'='*80} 1. Use a connection string to connect to the database $ dm mysql://root@localhost/test_development Notes: The connection string has the format: adapter://user:password@host:port/database Where adapter is in: {mysql, pgsql, sqlite...} 2. Load the database by specifying only cli options $ dm -a mysql -u root -h localhost -d test_development -e developemnt 3. Load the database using a yaml config file and specifying the environment to use $ dm --yaml config/database.yml -e development 4. Load everything from a config file, this example is equivalent to the above $ dm --config config/development.yml 5. Load the database and some model files from a directory, specifying the environment $ dm --yaml config/database.yml -e development --models app/models 6. Load an assumed structure of a typical merb application $ dm --merb -e development Note: This is similar to merb -i without the merb framework being loaded. ".gsub(/^ /,'') end