Class: DBModelApp

Inherits:
Object
  • Object
show all
Defined in:
lib/dbmodel.rb

Overview

DBModel main application object. When invoking dbmodel from the command line, a DBModelApp object is created and run.

Constant Summary collapse

OPTIONS =
[
  ['--dry-run',  '-n', GetoptLong::NO_ARGUMENT,
    "Do a dry run without executing actions."],
  ['--help',     '-H', GetoptLong::NO_ARGUMENT,
    "Display this help message."],
  ['--quiet',    '-q', GetoptLong::NO_ARGUMENT,
    "Do not log messages to standard output."],
  ['--usage',    '-h', GetoptLong::NO_ARGUMENT,
    "Display usage."],
  ['--version',  '-V', GetoptLong::NO_ARGUMENT,
    "Display the program version."],
]

Instance Method Summary collapse

Constructor Details

#initializeDBModelApp

Create a DBModelApp object.



186
187
# File 'lib/dbmodel.rb', line 186

def initialize
end

Instance Method Details

#command_line_optionsObject

Return a list of the command line options supported by the program.



214
215
216
# File 'lib/dbmodel.rb', line 214

def command_line_options
  OPTIONS.collect { |lst| lst[0..-2] }
end

#do_option(opt, value) ⇒ Object

Do the option defined by opt and value.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/dbmodel.rb', line 219

def do_option(opt, value)
  case opt
  when '--dry-run'
    $dryrun = true
  when '--help'
    help
    exit
  when '--quiet'
    $silent = true
  when '--usage'
    usage
    exit
  when '--version'
    puts "dbmodel, version #{DBMODELVERSION}"
    exit
  else
    fail "Unknown option: #{opt}"
  end
end

#get_filenamesObject

Collect the list of dbmodel filenames on the command line.

Default filename is “dbmodel.xml”



247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/dbmodel.rb', line 247

def get_filenames
  filenames = []
  ARGV.each { |arg| filenames << arg if arg !~ /^-+.*/ }
  if filenames.size == 0
    if File.exist?('dbmodel.xml')
      filenames.push("dbmodel.xml")
    else
      puts "\nCould not find any database model XML files."
    end
  end
  filenames
end

#handle_optionsObject

Read and handle the command line options.



240
241
242
243
# File 'lib/dbmodel.rb', line 240

def handle_options
  opts = GetoptLong.new(*command_line_options)
  opts.each { |opt, value| do_option(opt, value) }
end

#helpObject

Display the dbmodel command line help.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/dbmodel.rb', line 195

def help
  usage
  puts
  puts "Options are ..."
  puts
  OPTIONS.sort.each do |long, short, mode, desc|
    if mode == GetoptLong::REQUIRED_ARGUMENT
      if desc =~ /\b([A-Z]{2,})\b/
        long = long + "=#{$1}"
      end
    end
    printf "  %-20s (%s)\n", long, short
    printf "      %s\n", desc
  end
  puts "\nPlease read the rdoc README for more information."
end

#runObject

Run the dbmodel app



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/dbmodel.rb', line 261

def run
  handle_options
  begin
    get_filenames.each do |f|
      require File.dirname(f) + '/../config/environment'  # Better way to do this?
      @dbmodelxml = REXML::Document.new(File.open(f))
      # Verify that this is a DBDesigner XML file
      if @dbmodelxml.elements['DBMODEL'].nil?
        puts "\nFile '#{f}' is not a DBDesigner 4 XML file. Skipping..."
      else
        puts "\nReading the datamodel XML (#{f}) from DBDesigner..." if not $silent
        # Create a hash for each table with ID key
        tables = Tables.new
        @dbmodelxml.elements.each("//TABLE") { |t| tables.add_table(t) }
        # Add relationship information to the tables
        @dbmodelxml.elements.each("//RELATION") { |r| tables.add_relationship(r) }
        # Modify the files for each table
        tables.update_files(f)
      end
    end
  rescue Exception => ex
    puts "dbmodel aborted!"
    if ex.message =~ /config\/environment/
      puts "Make sure your datamodel XML file is in the Rails app /db directory\n"
    end
    puts ex.message
    puts ex.backtrace.find {|str| str =~ /\.rb/ } || ""
    exit(1)
  end    
end

#usageObject

Display the program usage line.



190
191
192
# File 'lib/dbmodel.rb', line 190

def usage
  puts "dbmodel {options} [dbmodelfiles ...]"
end