Class: Ditto::Runner

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

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



7
8
9
# File 'lib/ditto/runner.rb', line 7

def initialize(argv)
  @opts = Options.new(argv)
end

Instance Method Details

#load_files(list, type = nil) ⇒ Object

Load any filetype depending on it’s extension



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
# File 'lib/ditto/runner.rb', line 37

def load_files list, type = nil
  nfiles = 0
  nerr = 0
  list.each do |f|
	ext = File.extname(f)
	next if type and ext != type
	print "loading #{f}" if @opts.verbose > 1
	nfiles += 1
	case ext
	when '.yaml'
	  nent = Entity.load_instances f, @opts.verbose
	  puts "#{nent} instances loaded" if @opts.verbose > 0
	when '.ditto'
	  Ditto::Entity.load_from_file f
	  puts "" if @opts.verbose > 1
	when '.dm'
	  begin
 load File.absolute_path(f)
	  rescue ScriptError, StandardError => le
 loc = le.backtrace[2].split(':')
 puts "Error: #{le.to_s} (line #{loc[1]} in #{loc[0]})"
	  end
	  puts "" if @opts.verbose > 1
	end
  end
  puts "#{nfiles} files loaded" if @opts.verbose > 0
end

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ditto/runner.rb', line 11

def run
  begin
	if @opts.check
	  puts "Checking..." if @opts.verbose > 0
	  load_files @opts.loadfiles
	  exit
	end
	puts "loading instances..." if @opts.verbose > 0
	load_files @opts.loadfiles, '.yaml'
	raise "dittomart file must be specified!" unless @opts.dittomart
	puts "loading dittomart: #{@opts.dittomart}" if @opts.verbose > 0
	load_files @opts.martfiles, '.dm'
	Ditto::Entity.load_entities @opts.entitydir, @opts.verbose
	store_data
	return 0
  rescue Ditto::Error => de
	STDERR.puts "\nError: #{de.message(@opts.debug)}"
  rescue StandardError => e
	STDERR.puts "\n#{e.class}: #{e.message}"
	STDERR.puts e.backtrace if @opts.debug
  end
  return 99
end

#store_dataObject

Add the data to the database



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ditto/runner.rb', line 67

def store_data
  puts "connecting to #{@opts.connstring}" if @opts.verbose > 0
  DataMapper::Logger.new(STDOUT, (@opts.verbose > 2) ? :debug : :info)
  DataMapper.setup(:default, @opts.connstring)
  DataMapper.finalize
  begin
	if @opts.droptables
	  DataMapper.auto_migrate!
	else
	  DataMapper.auto_upgrade!
	end
	return Ditto::Entity.store_all @opts.verbose
  rescue StandardError => e
	STDERR.puts "\nERROR: #{e.to_s}"
	STDERR.puts e.backtrace if @opts.debug
	return false
  end
end