winton-active_wrapper 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -24,6 +24,8 @@ $db, $log, $mail = ActiveWrapper.setup(
24
24
  :stdout => true
25
25
  )
26
26
 
27
+ $db.drop_db
28
+ $db.create_db
27
29
  $db.establish_connection
28
30
  $db.generate_migration('my_migration')
29
31
  $db.migrate('001')
@@ -2,14 +2,14 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{active_wrapper}
5
- s.version = "0.1.6"
5
+ s.version = "0.1.7"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Winton Welsh"]
9
- s.date = %q{2009-07-28}
9
+ s.date = %q{2009-07-29}
10
10
  s.email = %q{mail@wintoni.us}
11
11
  s.extra_rdoc_files = ["README.markdown"]
12
- s.files = ["active_wrapper.gemspec", "gemspec.rb", "lib", "lib/active_wrapper", "lib/active_wrapper/db.rb", "lib/active_wrapper/log.rb", "lib/active_wrapper/mail.rb", "lib/active_wrapper/tasks.rb", "lib/active_wrapper.rb", "MIT-LICENSE", "Rakefile", "README.markdown", "resources", "resources/migration.template", "spec", "spec/spec.opts", "spec/spec_helper.rb"]
12
+ s.files = ["active_wrapper.gemspec", "gemspec.rb", "lib", "lib/active_wrapper", "lib/active_wrapper/db.rb", "lib/active_wrapper/log.rb", "lib/active_wrapper/mail.rb", "lib/active_wrapper/tasks.rb", "lib/active_wrapper.rb", "MIT-LICENSE", "Rakefile", "README.markdown", "resources", "resources/migration.template", "spec", "spec/active_wrapper", "spec/active_wrapper/db_spec.rb", "spec/active_wrapper/log_spec.rb", "spec/example_project", "spec/example_project/config", "spec/example_project/config/database.yml", "spec/example_project/db", "spec/example_project/db/migrate", "spec/example_project/db/migrate/001_test.rb", "spec/example_project/Rakefile", "spec/spec.opts", "spec/spec_helper.rb"]
13
13
  s.homepage = %q{http://github.com/winton/active_wrapper}
14
14
  s.require_paths = ["lib"]
15
15
  s.rubygems_version = %q{1.3.1}
data/gemspec.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  GEM_NAME = 'active_wrapper'
2
- GEM_FILES = FileList['**/*'] - FileList['coverage', 'coverage/**/*', 'pkg', 'pkg/**/*']
2
+ GEM_FILES = FileList['**/*'] - FileList[
3
+ 'coverage', 'coverage/**/*',
4
+ 'pkg', 'pkg/**/*',
5
+ 'spec/example_project/log', 'spec/example_project/log/*'
6
+ ]
3
7
  GEM_SPEC = Gem::Specification.new do |s|
4
8
  # == CONFIGURE ==
5
9
  s.author = "Winton Welsh"
@@ -15,5 +19,5 @@ GEM_SPEC = Gem::Specification.new do |s|
15
19
  s.name = GEM_NAME
16
20
  s.platform = Gem::Platform::RUBY
17
21
  s.require_path = "lib"
18
- s.version = "0.1.6"
22
+ s.version = "0.1.7"
19
23
  end
@@ -1,68 +1,101 @@
1
1
  module ActiveWrapper
2
2
  class Db
3
3
 
4
- attr_reader :base, :env
4
+ attr_reader :base, :config, :env
5
5
 
6
6
  def initialize(options)
7
7
  @base = options[:base]
8
+ if File.exists?(path = "#{base}/config/database.yml")
9
+ @config = YAML::load(File.open(path))
10
+ end
8
11
  @env = options[:env].to_s
9
12
  end
10
13
 
11
- def establish_connection
12
- unless ActiveRecord::Base.connected?
13
- config = YAML::load(File.open("#{base}/config/database.yml"))
14
- ActiveRecord::Base.configurations = config
14
+ def connected?
15
+ ActiveRecord::Base.connected?
16
+ end
17
+
18
+ def create_db
19
+ establish_connection('database' => nil)
20
+ ActiveRecord::Base.connection.create_database config[env]['database']
21
+ establish_connection({})
22
+ end
23
+
24
+ def drop_db
25
+ establish_connection('database' => nil)
26
+ ActiveRecord::Base.connection.drop_database config[env]['database']
27
+ end
28
+
29
+ def establish_connection(options=nil)
30
+ if !connected? || options
31
+ config_clone = Marshal.load(Marshal.dump(config))
32
+ config_clone[env].merge!(options || {})
33
+ ActiveRecord::Base.configurations = config_clone
15
34
  ActiveRecord::Base.establish_connection(env)
16
35
  end
17
36
  end
18
37
 
19
38
  def migrate(version=nil)
20
- ActiveRecord::Migrator.migrate("#{base}/db/migrate", version)
39
+ redirect_stdout do
40
+ ActiveRecord::Migrator.migrate("#{base}/db/migrate", version)
41
+ end
21
42
  end
22
43
 
23
44
  def migrate_reset
24
- if @env == 'test'
25
- stdout = $stdout
26
- $stdout = File.new('/dev/null', 'w')
27
- end
28
- migrate(0)
29
- migrate
30
- if @env == 'test'
31
- $stdout = stdout
45
+ redirect_stdout do
46
+ migrate(0)
47
+ migrate
32
48
  end
33
49
  end
34
50
 
35
51
  def generate_migration(name=nil)
36
- raise "Please specify desired migration name with NAME=my_migration_name" unless name
52
+ redirect_stdout do
53
+ raise "Please specify desired migration name with NAME=my_migration_name" unless name
37
54
 
38
- migration_name = name.strip.chomp
39
- migrations_path = "#{base}/db/migrate"
40
- migrations_template = File.expand_path("#{File.dirname(__FILE__)}/../../resources/migration.template")
55
+ migration_name = name.strip.chomp
56
+ migrations_path = "#{base}/db/migrate"
57
+ migrations_template = File.expand_path("#{File.dirname(__FILE__)}/../../resources/migration.template")
41
58
 
42
- # Find the highest existing migration version or set to 1
43
- if (existing_migrations = Dir[File.join(migrations_path, '*.rb')]).length > 0
44
- version = File.basename(existing_migrations.sort.reverse.first)[/^(\d+)_/,1].to_i + 1
45
- else
46
- version = 1
47
- end
59
+ # Find the highest existing migration version or set to 1
60
+ if (existing_migrations = Dir[File.join(migrations_path, '*.rb')]).length > 0
61
+ version = File.basename(existing_migrations.sort.reverse.first)[/^(\d+)_/,1].to_i + 1
62
+ else
63
+ version = 1
64
+ end
48
65
 
49
- # Read the contents of the migration template into string
50
- migrations_template = File.read(migrations_template)
66
+ # Read the contents of the migration template into string
67
+ migrations_template = File.read(migrations_template)
51
68
 
52
- # Replace the migration name in template with the acutal one
53
- migration_content = migrations_template.gsub('__migration_name__', migration_name.camelize)
54
- migration_content = migration_content.gsub('__migration_table__', migration_name)
69
+ # Replace the migration name in template with the acutal one
70
+ migration_content = migrations_template.gsub('__migration_name__', migration_name.camelize)
71
+ migration_content = migration_content.gsub('__migration_table__', migration_name)
55
72
 
56
- # Generate migration filename
57
- migration_filename = "#{"%03d" % version}_#{migration_name}.rb"
73
+ # Generate migration filename
74
+ migration_filename = "#{"%03d" % version}_#{migration_name}.rb"
58
75
 
59
- # Write the migration
60
- File.open(File.join(migrations_path, migration_filename), "w+") do |migration|
61
- migration.puts migration_content
62
- end
76
+ # Write the migration
77
+ File.open(File.join(migrations_path, migration_filename), "w+") do |migration|
78
+ migration.puts migration_content
79
+ end
63
80
 
64
- # Done!
65
- puts "Successfully created migration #{migration_filename}"
81
+ # Done!
82
+ puts "Successfully created migration #{migration_filename}"
83
+ end
84
+ end
85
+
86
+ def method_missing(method, *args)
87
+ ActiveRecord::Base.connection.send(method, *args)
88
+ end
89
+
90
+ def redirect_stdout(&block)
91
+ if env == 'test'
92
+ stdout = $stdout
93
+ $stdout = File.new('/dev/null', 'w')
94
+ end
95
+ yield
96
+ if env == 'test'
97
+ $stdout = stdout
98
+ end
66
99
  end
67
100
  end
68
101
  end
@@ -5,6 +5,7 @@ module ActiveWrapper
5
5
 
6
6
  def initialize(options)
7
7
  @base = options[:base]
8
+ @config = {}
8
9
  @env = options[:env].to_s
9
10
 
10
11
  path = "#{base}/config/mail.yml"
@@ -15,11 +16,10 @@ module ActiveWrapper
15
16
  @config[:smtp] = options[:smtp]
16
17
  @config[:imap] = options[:imap]
17
18
  if File.exists?(path)
18
- @config = YAML::load(File.open(path))
19
- if @config && @config = @config[@env]
20
- @config = @config.to_options
21
- @config[:imap] = @config[:imap].to_options unless @config[:imap]
22
- @config[:smtp] = @config[:smtp].to_options unless @config[:smtp]
19
+ yaml = YAML::load(File.open(path))
20
+ if yaml && yaml = yaml[@env].to_options
21
+ @config[:imap] = yaml[:imap].to_options unless @config[:imap]
22
+ @config[:smtp] = yaml[:smtp].to_options unless @config[:smtp]
23
23
  end
24
24
  end
25
25
  if @config[:smtp]
@@ -7,18 +7,25 @@ module ActiveWrapper
7
7
 
8
8
  task :environment do
9
9
  $db, $log = ActiveWrapper.setup(options)
10
- $db.establish_connection
11
10
  yield if block
12
11
  end
13
12
 
14
13
  namespace :db do
14
+ desc "Create the database"
15
+ task :create do
16
+ $db.create_db
17
+ end
18
+
19
+ desc "Drop the database"
20
+ task :drop do
21
+ $db.drop_db
22
+ end
23
+
15
24
  desc "Migrate the database with optional VERSION"
16
25
  task :migrate => :environment do
17
26
  $db.migrate(ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
18
27
  end
19
- end
20
-
21
- namespace :generate do
28
+
22
29
  desc "Generate a migration with given NAME"
23
30
  task :migration => :environment do
24
31
  $db.generate_migration(ENV['NAME'])
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe ActiveWrapper::Db do
4
+
5
+ before(:each) do
6
+ $db, $log, $mail = ActiveWrapper.setup(
7
+ :base => SPEC + '/example_project',
8
+ :env => 'test'
9
+ )
10
+ $db.drop_db
11
+ $db.create_db
12
+ end
13
+
14
+ it "should establish a connection" do
15
+ $db.disconnect!
16
+ $db.establish_connection
17
+ $db.connected?.should == true
18
+ end
19
+
20
+ it "should create a database" do
21
+ $db.current_database.should == 'active_wrapper_test'
22
+ end
23
+
24
+ it "should drop a database" do
25
+ $db.drop_db
26
+ $db.current_database.should == nil
27
+ end
28
+
29
+ it "should migrate a database" do
30
+ $db.migrate
31
+ $db.execute('insert into tests () values ()')
32
+ $db.execute('select * from tests').num_rows.should == 1
33
+ end
34
+
35
+ it "should migrate reset a database" do
36
+ $db.migrate
37
+ $db.execute('insert into tests () values ()')
38
+ $db.migrate_reset
39
+ $db.execute('select * from tests').num_rows.should == 0
40
+ end
41
+
42
+ it "should generate a migration" do
43
+ $db.generate_migration 'another_test'
44
+ path = SPEC + "/example_project/db/migrate/002_another_test.rb"
45
+ File.exists?(path).should == true
46
+ FileUtils.rm_f path
47
+ end
48
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
2
+
3
+ describe ActiveWrapper::Log do
4
+
5
+ before(:each) do
6
+ FileUtils.rm_f(@path = SPEC + "/example_project/log/test.log")
7
+ $db, $log, $mail = ActiveWrapper.setup(
8
+ :base => SPEC + '/example_project',
9
+ :env => 'test'
10
+ )
11
+ end
12
+
13
+ it "should create a log file" do
14
+ File.exists?(@path).should == true
15
+ end
16
+
17
+ it "should log to the log file" do
18
+ $log.info "test"
19
+ File.read(@path).include?('test').should == true
20
+ end
21
+
22
+ it "should clear the log file while keeping the logger intact" do
23
+ $log.clear
24
+ File.read(@path).include?('test').should == false
25
+ $log.info "test"
26
+ File.read(@path).include?('test').should == true
27
+ end
28
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require File.expand_path(File.dirname(__FILE__) + "/../../lib/active_wrapper/tasks")
4
+
5
+ ActiveWrapper::Tasks.new(
6
+ :base => File.dirname(__FILE__),
7
+ :env => 'test'
8
+ )
@@ -0,0 +1,6 @@
1
+ test:
2
+ database: active_wrapper_test
3
+ adapter: mysql
4
+ username: root
5
+ password:
6
+ host: localhost
@@ -0,0 +1,10 @@
1
+ class Test < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :tests do |t|
4
+ end
5
+ end
6
+
7
+ def self.down
8
+ drop_table :tests
9
+ end
10
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: winton-active_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Winton Welsh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-28 00:00:00 -07:00
12
+ date: 2009-07-29 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -56,6 +56,16 @@ files:
56
56
  - resources
57
57
  - resources/migration.template
58
58
  - spec
59
+ - spec/active_wrapper
60
+ - spec/active_wrapper/db_spec.rb
61
+ - spec/active_wrapper/log_spec.rb
62
+ - spec/example_project
63
+ - spec/example_project/config
64
+ - spec/example_project/config/database.yml
65
+ - spec/example_project/db
66
+ - spec/example_project/db/migrate
67
+ - spec/example_project/db/migrate/001_test.rb
68
+ - spec/example_project/Rakefile
59
69
  - spec/spec.opts
60
70
  - spec/spec_helper.rb
61
71
  has_rdoc: false