Class: ActiveWrapper::Db
- Inherits:
-
Object
show all
- Defined in:
- lib/active_wrapper/db.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options) ⇒ Db
Returns a new instance of Db.
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/active_wrapper/db.rb', line 6
def initialize(options)
@adapter = options[:adapter]
@base = options[:base]
if File.exists?(path = "#{base}/config/database.yml")
@config = YAML::load(File.open(path))
else
raise "Could not find #{path}"
end
@env = options[:env].to_s
unless config[env]
raise "Environment \"#{env}\" not found in #{path}"
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
94
95
96
|
# File 'lib/active_wrapper/db.rb', line 94
def method_missing(method, *args)
ActiveRecord::Base.connection.send(method, *args)
end
|
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
4
5
6
|
# File 'lib/active_wrapper/db.rb', line 4
def base
@base
end
|
#config ⇒ Object
Returns the value of attribute config.
4
5
6
|
# File 'lib/active_wrapper/db.rb', line 4
def config
@config
end
|
#env ⇒ Object
Returns the value of attribute env.
4
5
6
|
# File 'lib/active_wrapper/db.rb', line 4
def env
@env
end
|
Instance Method Details
#connected? ⇒ Boolean
20
21
22
|
# File 'lib/active_wrapper/db.rb', line 20
def connected?
ActiveRecord::Base.connected?
end
|
#create_db ⇒ Object
24
25
26
27
28
|
# File 'lib/active_wrapper/db.rb', line 24
def create_db
establish_connection('database' => nil)
ActiveRecord::Base.connection.create_database config[env]['database']
establish_connection({})
end
|
#drop_db ⇒ Object
30
31
32
33
|
# File 'lib/active_wrapper/db.rb', line 30
def drop_db
establish_connection('database' => nil)
ActiveRecord::Base.connection.drop_database config[env]['database']
end
|
#establish_connection(options = nil) ⇒ Object
35
36
37
38
39
40
41
42
43
|
# File 'lib/active_wrapper/db.rb', line 35
def establish_connection(options=nil)
if !connected? || options
config_clone = Marshal.load(Marshal.dump(config))
config_clone[env].merge!(options || {})
config_clone[env]['adapter'] = @adapter if @adapter
ActiveRecord::Base.configurations = config_clone
ActiveRecord::Base.establish_connection(env)
end
end
|
#generate_migration(name = nil) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/active_wrapper/db.rb', line 59
def generate_migration(name=nil)
redirect_stdout do
raise "Please specify desired migration name with NAME=my_migration_name" unless name
migration_name = name.strip.chomp
migrations_path = "#{base}/db/migrate"
migrations_template = File.expand_path("#{File.dirname(__FILE__)}/../../resources/migration.template")
if (existing_migrations = Dir[File.join(migrations_path, '*.rb')]).length > 0
version = File.basename(existing_migrations.sort.reverse.first)[/^(\d+)_/,1].to_i + 1
else
version = 1
end
migrations_template = File.read(migrations_template)
migration_content = migrations_template.gsub('__migration_name__', migration_name.camelize)
migration_content = migration_content.gsub('__migration_table__', migration_name)
migration_filename = "#{"%03d" % version}_#{migration_name}.rb"
File.open(File.join(migrations_path, migration_filename), "w+") do |migration|
migration.puts migration_content
end
puts "Successfully created migration #{migration_filename}"
end
end
|
#migrate(version = nil) ⇒ Object
45
46
47
48
49
50
|
# File 'lib/active_wrapper/db.rb', line 45
def migrate(version=nil)
establish_connection
redirect_stdout do
ActiveRecord::Migrator.migrate("#{base}/db/migrate", version)
end
end
|
#migrate_reset ⇒ Object
52
53
54
55
56
57
|
# File 'lib/active_wrapper/db.rb', line 52
def migrate_reset
redirect_stdout do
migrate(0)
migrate
end
end
|
#redirect_stdout(&block) ⇒ Object
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/active_wrapper/db.rb', line 98
def redirect_stdout(&block)
if env == 'test'
stdout = $stdout.dup
$stdout = $stdout.reopen(RUBY_PLATFORM =~ /mswin/ ? "NUL" : "/dev/null")
end
yield
if env == 'test'
$stdout.reopen(stdout)
end
end
|