Class: DbCompile::Transaction

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

Overview

Encapsulates entire transaction and dependency checking

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Transaction

Returns a new instance of Transaction.



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

def initialize(path)
  @path = path
  @run_queue = []
  @deps_queue = []
  @manifest = YAML::load_file(File.join(path, 'compile.yml'))

  @manifest.each{ |construct_name, data|
    if data
      data.each{ |object_name, dependencies|
        install_dependencies(construct_name, object_name)
      }
    end
  }
end

Instance Method Details

#build_contruct(construct_name, object_name) ⇒ Object



46
47
48
49
# File 'lib/dbcompile/transaction.rb', line 46

def build_contruct(construct_name, object_name)
  klass = "DbCompile::#{construct_name.singularize.camelize}".constantize
  klass.new(object_name, @path)
end

#executeObject



51
52
53
54
55
56
57
58
59
# File 'lib/dbcompile/transaction.rb', line 51

def execute
  @run_queue.each{ |construct_name, object_name|
    construct = build_contruct(construct_name, object_name)
    msg = "Compiling #{construct.path}"
    puts msg
    ActiveRecord::Base.logger.info msg
    construct.execute
  }
end

#install_dependencies(construct_name, object_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dbcompile/transaction.rb', line 26

def install_dependencies(construct_name, object_name)
  if @deps_queue.include? [construct_name, object_name]
    raise CircularDependenciesException.new(
      "Dependency of #{construct_name} #{object_name} referenced it as a dependency!")
  end
  @deps_queue << [construct_name, object_name]
  if not @run_queue.include? [construct_name, object_name]
    dependencies = @manifest[construct_name][object_name]
    if dependencies
      dependencies.each{ |dep_construct_name, name_list|
        name_list.each{ |dep_object_name|
          install_dependencies(dep_construct_name, dep_object_name)
        }
      }
    end
    @run_queue << [construct_name, object_name]
  end
  @deps_queue.pop
end

#verifyObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dbcompile/transaction.rb', line 61

def verify
  msg = "Verifying compilation"
  puts msg
  ActiveRecord::Base.logger.info msg
  no_errors = true
  @run_queue.each{ |construct_name, object_name|
    construct = build_contruct(construct_name, object_name)
    case construct.verify
      when nil
        msg = "#{construct_name.capitalize} #{object_name} could not be verified."
      when true
        msg = "#{construct_name.capitalize} #{object_name} successfully created."
      when false
        msg = "#{construct_name.capitalize} #{object_name} creation failed."
        no_errors = false
    end
    puts msg
    ActiveRecord::Base.logger.info msg
  }
  return no_errors
end