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.



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

def initialize(argv)
  @opts = Options.instance.parse(argv)
  $: << @opts.modelpath
end

Instance Method Details

#load_dataObject

Load data from YAML files into the in-memory representation return true if all OK



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

def load_data
  nfiles = nent = nerr = 0
  Dir.glob(@opts.loadpaths) do |f|
	next unless File.extname(f) == '.yaml'
	nfiles += 1
	puts "loading file: #{File.basename(f)}" if @opts.verbose > 1
	header = nil
	YAML::load_documents(File.open(f)) do |doc|
	  unless header
 header = doc
 puts "Header info is #{header.to_s}" if @opts.verbose > 2
 next
	  end
	  e = doc.flatten
	  if e.size != 2
 nerr += 1
 puts "ERROR: entity instance has multiple keys"
 next
	  end
	  Ditto::Entity.load_instance e[0].to_sym, e[1]
	  nent += 1
	end
  end
  puts "#{nfiles} data files, #{nent} instances loaded, #{nerr} errors" if (nfiles > 0 and @opts.verbose > 0)
  return nerr == 0
end

#load_definitionsObject

Load definitions from Ditto files



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ditto/runner.rb', line 28

def load_definitions
  nfiles = 0
  nerr = 0
  Dir.glob(@opts.loadpaths) do |f|
	next unless File.extname(f) == '.ditto'
	puts "loading file: #{File.basename(f)}" if @opts.verbose > 1
	begin
	  load File.absolute_path(f)
	  nfiles += 1
	rescue LoadError => le
	  loc = le.backtrace[2].split(':')
	  puts "Error: #{le.to_s} (line #{loc[1]} in #{loc[0]})\nDid you set --modelpath ?"
	  nerr += 1
	end
  end
  puts "#{nfiles} definition files loaded" if (nfiles > 0 and @opts.verbose > 0)
  return nerr == 0
end

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ditto/runner.rb', line 11

def run
  begin
	return 1 unless load_definitions
	return 2 unless Ditto::Entity.check_definitions(@opts.verbose)
	return 3 unless load_data
	return 4 unless Ditto::Entity.validate_instances(@opts.verbose)
	return 5 unless store_data
	return 0
  rescue StandardError => e
	STDERR.puts "\nERROR: #{e.to_s}"
	STDERR.puts e.backtrace if @opts.debug
	return 99
  end
end

#store_dataObject

Add the data to the database



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ditto/runner.rb', line 79

def store_data
  return true if Ditto::Entity.instance_count == 0
  return true if Ditto::Entity.entity_count == 0

  puts "Running against: #{@opts.connstring}" if @opts.verbose > 0
  DataMapper::Logger.new(STDOUT, (@opts.verbose > 1) ? :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
  rescue StandardError => e
	STDERR.puts "\nERROR: #{e.to_s}"
	STDERR.puts e.backtrace if @opts.debug
	return false
  end
end