Module: Bj::ClassMethods

Defined in:
lib/bj/bj.rb

Instance Method Summary collapse

Instance Method Details

#bootObject



66
67
68
69
# File 'lib/bj/bj.rb', line 66

def boot
  load File.join(rails_root, "config", "boot.rb")
  load File.join(rails_root, "config", "environment.rb")
end

#chroot(options = {}, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bj/bj.rb', line 45

def chroot options = {}, &block
  if defined? @chrooted and @chrooted
    return(block ? block.call(@chrooted) : @chrooted)
  end
  if block
    begin
      chrooted = @chrooted
      Dir.chdir(@chrooted = rails_root) do
        raise RailsRoot, "<#{ Dir.pwd }> is not a rails root" unless Util.valid_rails_root?(Dir.pwd)
        block.call(@chrooted)
      end
    ensure
      @chrooted = chrooted 
    end
  else
    Dir.chdir(@chrooted = rails_root)
    raise RailsRoot, "<#{ Dir.pwd }> is not a rails root" unless Util.valid_rails_root?(Dir.pwd)
    @chrooted
  end
end

#transaction(options = {}, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bj/bj.rb', line 21

def transaction options = {}, &block
  options.to_options!

  cur_rails_env = Bj.rails_env.to_s
  new_rails_env = options[:rails_env].to_s

  cur_spec = configurations[cur_rails_env]
  table.establish_connection(cur_spec) unless table.connected?

  if(new_rails_env.empty? or cur_rails_env == new_rails_env) 
    table.transaction{ block.call(table.connection) }
  else
    new_spec = configurations[new_rails_env]
    table.establish_connection(new_spec)
    Bj.rails_env = new_rails_env
    begin
      table.transaction{ block.call(table.connection) }
    ensure
      table.establish_connection(cur_spec)
      Bj.rails_env = cur_rails_env
    end
  end
end