active_wrapper 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +18 -0
- data/README.markdown +91 -0
- data/Rakefile +54 -0
- data/active_wrapper.gemspec +33 -0
- data/gemspec.rb +23 -0
- data/lib/active_wrapper/db.rb +101 -0
- data/lib/active_wrapper/log.rb +39 -0
- data/lib/active_wrapper/mail.rb +47 -0
- data/lib/active_wrapper/tasks.rb +43 -0
- data/lib/active_wrapper.rb +39 -0
- data/resources/migration.template +11 -0
- data/spec/active_wrapper/db_spec.rb +48 -0
- data/spec/active_wrapper/log_spec.rb +28 -0
- data/spec/example_project/Rakefile +8 -0
- data/spec/example_project/config/database.yml +7 -0
- data/spec/example_project/db/migrate/001_test.rb +10 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +16 -0
- metadata +91 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright (c) 2009 Winton Welsh
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
7
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
8
|
+
subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
15
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
16
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
ActiveWrapper
|
2
|
+
=============
|
3
|
+
|
4
|
+
Wraps ActiveRecord and Logger for use in non-Rails environments.
|
5
|
+
|
6
|
+
Setup
|
7
|
+
-----
|
8
|
+
|
9
|
+
<pre>
|
10
|
+
sudo gem install active_wrapper
|
11
|
+
</pre>
|
12
|
+
|
13
|
+
Usage
|
14
|
+
-----
|
15
|
+
|
16
|
+
<pre>
|
17
|
+
require 'rubygems'
|
18
|
+
require 'active_wrapper'
|
19
|
+
|
20
|
+
$db, $log, $mail = ActiveWrapper.setup(
|
21
|
+
:base => File.dirname(__FILE__),
|
22
|
+
:env => 'development',
|
23
|
+
:log => 'custom',
|
24
|
+
:stdout => true
|
25
|
+
)
|
26
|
+
|
27
|
+
$db.drop_db
|
28
|
+
$db.create_db
|
29
|
+
$db.establish_connection
|
30
|
+
$db.generate_migration('my_migration')
|
31
|
+
$db.migrate('001')
|
32
|
+
$db.migrate_reset
|
33
|
+
$log.info('log this')
|
34
|
+
$log.clear
|
35
|
+
$mail.deliver(:from => 'me@me.com', :to => 'you@you.com', :subject => 'subject', :body => 'body')
|
36
|
+
</pre>
|
37
|
+
|
38
|
+
<code>ActiveWrapper</code> looks for the following files within the <code>:base</code> directory:
|
39
|
+
|
40
|
+
* <b>config/database.yml</b>
|
41
|
+
* <b>config/mail.yml</b>
|
42
|
+
* <b>db/migrate/*.rb</b>
|
43
|
+
|
44
|
+
The <code>:env</code> option is <code>"development"</code> by default.
|
45
|
+
|
46
|
+
Logger
|
47
|
+
------
|
48
|
+
|
49
|
+
In the previous example, the log is stored in <b>log/custom.log</b>.
|
50
|
+
|
51
|
+
If no <code>:log</code> name is specified, the <code>:env</code> option is used for the log name.
|
52
|
+
|
53
|
+
You may also set <code>:log</code> to false to disable logging entirely.
|
54
|
+
|
55
|
+
Setting <code>:stdout</code> to true causes stdout and stderr to redirect to the logger. It is false by default.
|
56
|
+
|
57
|
+
Mailer
|
58
|
+
------
|
59
|
+
|
60
|
+
Your <b>mail.yml</b> should look something like this:
|
61
|
+
|
62
|
+
<pre>
|
63
|
+
development:
|
64
|
+
smtp:
|
65
|
+
address: smtp.gmail.com
|
66
|
+
authentication: :plain
|
67
|
+
domain: gmail.com
|
68
|
+
password: password
|
69
|
+
port: 587
|
70
|
+
enable_starttls_auto: true
|
71
|
+
user_name: user@gmail.com
|
72
|
+
</pre>
|
73
|
+
|
74
|
+
This file is optional.
|
75
|
+
|
76
|
+
Rakefile
|
77
|
+
--------
|
78
|
+
|
79
|
+
Add this to your project's <b>Rakefile</b> for database migration and log tasks:
|
80
|
+
|
81
|
+
<pre>
|
82
|
+
require 'rubygems'
|
83
|
+
require 'rake'
|
84
|
+
require 'active_wrapper/tasks'
|
85
|
+
|
86
|
+
ActiveWrapper::Tasks.new(:log => 'custom') do
|
87
|
+
# Put stuff you would normally put in the environment task here
|
88
|
+
end
|
89
|
+
</pre>
|
90
|
+
|
91
|
+
Pass the same options to <code>ActiveWrapper::Tasks.new</code> as you would <code>ActiveWrapper.new</code>.
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
require 'gemspec'
|
6
|
+
|
7
|
+
desc "Generate gemspec"
|
8
|
+
task :gemspec do
|
9
|
+
File.open("#{Dir.pwd}/#{GEM_NAME}.gemspec", 'w') do |f|
|
10
|
+
f.write(GEM_SPEC.to_ruby)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Install gem"
|
15
|
+
task :install do
|
16
|
+
Rake::Task['gem'].invoke
|
17
|
+
`sudo gem uninstall #{GEM_NAME} -x`
|
18
|
+
`sudo gem install pkg/#{GEM_NAME}*.gem`
|
19
|
+
`rm -Rf pkg`
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "Package gem"
|
23
|
+
Rake::GemPackageTask.new(GEM_SPEC) do |pkg|
|
24
|
+
pkg.gem_spec = GEM_SPEC
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "Setup project"
|
28
|
+
task :setup do
|
29
|
+
name = File.basename(Dir.pwd)
|
30
|
+
`rm -Rf .git`
|
31
|
+
begin
|
32
|
+
dir = Dir['**/gem_template*']
|
33
|
+
from = dir.pop
|
34
|
+
if from
|
35
|
+
rb = from.include?('.rb')
|
36
|
+
to = File.dirname(from) + "/#{name}#{'.rb' if rb}"
|
37
|
+
FileUtils.mv(from, to)
|
38
|
+
end
|
39
|
+
end while dir.length > 0
|
40
|
+
Dir["**/*"].each do |path|
|
41
|
+
next if path.include?('Rakefile')
|
42
|
+
if File.file?(path)
|
43
|
+
`sed -i "" 's/gem_template/#{name}/g' #{path}`
|
44
|
+
end
|
45
|
+
end
|
46
|
+
`git init`
|
47
|
+
end
|
48
|
+
|
49
|
+
desc "Run specs"
|
50
|
+
Spec::Rake::SpecTask.new do |t|
|
51
|
+
t.rcov = true
|
52
|
+
t.spec_opts = ["--format", "specdoc", "--colour"]
|
53
|
+
t.spec_files = FileList["spec/**/*_spec.rb"]
|
54
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{active_wrapper}
|
5
|
+
s.version = "0.2.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Winton Welsh"]
|
9
|
+
s.date = %q{2009-08-13}
|
10
|
+
s.email = %q{mail@wintoni.us}
|
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/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
|
+
s.homepage = %q{http://github.com/winton/active_wrapper}
|
14
|
+
s.require_paths = ["lib"]
|
15
|
+
s.rubygems_version = %q{1.3.1}
|
16
|
+
s.summary = %q{Wraps ActiveRecord and Logger for use in non-Rails environments}
|
17
|
+
|
18
|
+
if s.respond_to? :specification_version then
|
19
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
20
|
+
s.specification_version = 2
|
21
|
+
|
22
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
23
|
+
s.add_runtime_dependency(%q<activerecord>, ["= 2.3.2"])
|
24
|
+
s.add_runtime_dependency(%q<actionmailer>, ["= 2.3.2"])
|
25
|
+
else
|
26
|
+
s.add_dependency(%q<activerecord>, ["= 2.3.2"])
|
27
|
+
s.add_dependency(%q<actionmailer>, ["= 2.3.2"])
|
28
|
+
end
|
29
|
+
else
|
30
|
+
s.add_dependency(%q<activerecord>, ["= 2.3.2"])
|
31
|
+
s.add_dependency(%q<actionmailer>, ["= 2.3.2"])
|
32
|
+
end
|
33
|
+
end
|
data/gemspec.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
GEM_NAME = 'active_wrapper'
|
2
|
+
GEM_FILES = FileList['**/*'] - FileList[
|
3
|
+
'coverage', 'coverage/**/*',
|
4
|
+
'pkg', 'pkg/**/*',
|
5
|
+
'spec/example_project/log', 'spec/example_project/log/*'
|
6
|
+
]
|
7
|
+
GEM_SPEC = Gem::Specification.new do |s|
|
8
|
+
# == CONFIGURE ==
|
9
|
+
s.author = "Winton Welsh"
|
10
|
+
s.email = "mail@wintoni.us"
|
11
|
+
s.homepage = "http://github.com/winton/#{GEM_NAME}"
|
12
|
+
s.summary = "Wraps ActiveRecord and Logger for use in non-Rails environments"
|
13
|
+
# == CONFIGURE ==
|
14
|
+
s.add_dependency('activerecord', '=2.3.2')
|
15
|
+
s.add_dependency('actionmailer', '=2.3.2')
|
16
|
+
s.extra_rdoc_files = [ "README.markdown" ]
|
17
|
+
s.files = GEM_FILES.to_a
|
18
|
+
s.has_rdoc = false
|
19
|
+
s.name = GEM_NAME
|
20
|
+
s.platform = Gem::Platform::RUBY
|
21
|
+
s.require_path = "lib"
|
22
|
+
s.version = "0.2.0"
|
23
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module ActiveWrapper
|
2
|
+
class Db
|
3
|
+
|
4
|
+
attr_reader :base, :config, :env
|
5
|
+
|
6
|
+
def initialize(options)
|
7
|
+
@base = options[:base]
|
8
|
+
if File.exists?(path = "#{base}/config/database.yml")
|
9
|
+
@config = YAML::load(File.open(path))
|
10
|
+
end
|
11
|
+
@env = options[:env].to_s
|
12
|
+
end
|
13
|
+
|
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
|
34
|
+
ActiveRecord::Base.establish_connection(env)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def migrate(version=nil)
|
39
|
+
redirect_stdout do
|
40
|
+
ActiveRecord::Migrator.migrate("#{base}/db/migrate", version)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def migrate_reset
|
45
|
+
redirect_stdout do
|
46
|
+
migrate(0)
|
47
|
+
migrate
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def generate_migration(name=nil)
|
52
|
+
redirect_stdout do
|
53
|
+
raise "Please specify desired migration name with NAME=my_migration_name" unless name
|
54
|
+
|
55
|
+
migration_name = name.strip.chomp
|
56
|
+
migrations_path = "#{base}/db/migrate"
|
57
|
+
migrations_template = File.expand_path("#{File.dirname(__FILE__)}/../../resources/migration.template")
|
58
|
+
|
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
|
65
|
+
|
66
|
+
# Read the contents of the migration template into string
|
67
|
+
migrations_template = File.read(migrations_template)
|
68
|
+
|
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)
|
72
|
+
|
73
|
+
# Generate migration filename
|
74
|
+
migration_filename = "#{"%03d" % version}_#{migration_name}.rb"
|
75
|
+
|
76
|
+
# Write the migration
|
77
|
+
File.open(File.join(migrations_path, migration_filename), "w+") do |migration|
|
78
|
+
migration.puts migration_content
|
79
|
+
end
|
80
|
+
|
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
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module ActiveWrapper
|
2
|
+
class Log
|
3
|
+
|
4
|
+
attr_reader :base, :log, :logger, :stdout
|
5
|
+
|
6
|
+
def initialize(options)
|
7
|
+
@base = options[:base]
|
8
|
+
@log = options[:log]
|
9
|
+
@stdout = options[:stdout]
|
10
|
+
|
11
|
+
return if @log == false
|
12
|
+
|
13
|
+
FileUtils.mkdir_p("#{base}/log")
|
14
|
+
file = File.open("#{base}/log/#{log}.log", 'a')
|
15
|
+
file.sync = true
|
16
|
+
|
17
|
+
if stdout
|
18
|
+
@logger = Logger.new($stdout)
|
19
|
+
$stdout.reopen(file)
|
20
|
+
$stderr.reopen(file)
|
21
|
+
else
|
22
|
+
@logger = Logger.new(file)
|
23
|
+
end
|
24
|
+
|
25
|
+
ActiveRecord::Base.logger = @logger
|
26
|
+
end
|
27
|
+
|
28
|
+
def clear
|
29
|
+
Dir["#{base}/log/*.log"].each do |file|
|
30
|
+
f = File.open(file, "w")
|
31
|
+
f.close
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def method_missing(method, *args)
|
36
|
+
logger.send(method, *args)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module ActiveWrapper
|
2
|
+
class Mail
|
3
|
+
|
4
|
+
attr_reader :base, :env, :config
|
5
|
+
|
6
|
+
def initialize(options)
|
7
|
+
@base = options[:base]
|
8
|
+
@config = {
|
9
|
+
:smtp => options[:smtp] || {},
|
10
|
+
:imap => options[:imap] || {}
|
11
|
+
}
|
12
|
+
@env = options[:env].to_s
|
13
|
+
|
14
|
+
path = "#{base}/config/mail.yml"
|
15
|
+
|
16
|
+
if @env == 'test'
|
17
|
+
ActionMailer::Base.delivery_method = :test
|
18
|
+
@config = nil
|
19
|
+
else
|
20
|
+
if File.exists?(path)
|
21
|
+
yaml = YAML::load(File.open(path))
|
22
|
+
if yaml && yaml = yaml[@env].to_options
|
23
|
+
@config[:imap] = yaml[:imap].to_options unless @config[:imap]
|
24
|
+
@config[:smtp] = yaml[:smtp].to_options unless @config[:smtp]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
if @config[:smtp]
|
28
|
+
ActionMailer::Base.delivery_method = :smtp
|
29
|
+
ActionMailer::Base.smtp_settings = @config[:smtp]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def deliver(options)
|
35
|
+
Mailer.deliver_email(options)
|
36
|
+
end
|
37
|
+
|
38
|
+
class Mailer < ActionMailer::Base
|
39
|
+
def email(options)
|
40
|
+
from options[:from]
|
41
|
+
recipients options[:to]
|
42
|
+
subject options[:subject]
|
43
|
+
body options[:body]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../active_wrapper")
|
2
|
+
|
3
|
+
module ActiveWrapper
|
4
|
+
class Tasks
|
5
|
+
|
6
|
+
def initialize(options={}, &block)
|
7
|
+
|
8
|
+
task :environment do
|
9
|
+
$db, $log = ActiveWrapper.setup(options)
|
10
|
+
yield if block
|
11
|
+
end
|
12
|
+
|
13
|
+
namespace :db do
|
14
|
+
desc "Create the database"
|
15
|
+
task :create => :environment do
|
16
|
+
$db.create_db
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "Drop the database"
|
20
|
+
task :drop => :environment do
|
21
|
+
$db.drop_db
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "Migrate the database with optional VERSION"
|
25
|
+
task :migrate => :environment do
|
26
|
+
$db.migrate(ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
27
|
+
end
|
28
|
+
|
29
|
+
desc "Generate a migration with given NAME"
|
30
|
+
task :migration => :environment do
|
31
|
+
$db.generate_migration(ENV['NAME'])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
namespace :log do
|
36
|
+
desc "Clear all logs"
|
37
|
+
task :clear => :environment do
|
38
|
+
$log.clear
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
Dir["#{File.dirname(__FILE__)}/../vendor/*/lib"].each do |path|
|
2
|
+
$:.unshift path
|
3
|
+
end
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
gem 'activerecord', '=2.3.2'
|
7
|
+
gem 'actionmailer', '=2.3.2'
|
8
|
+
require 'activerecord'
|
9
|
+
require 'actionmailer'
|
10
|
+
require 'logger'
|
11
|
+
require 'yaml'
|
12
|
+
|
13
|
+
require File.dirname(__FILE__) + "/active_wrapper/db"
|
14
|
+
require File.dirname(__FILE__) + "/active_wrapper/log"
|
15
|
+
require File.dirname(__FILE__) + "/active_wrapper/mail"
|
16
|
+
|
17
|
+
module ActiveWrapper
|
18
|
+
class <<self
|
19
|
+
|
20
|
+
def setup(options={})
|
21
|
+
|
22
|
+
options = {
|
23
|
+
:base => File.dirname($0),
|
24
|
+
:env => 'development',
|
25
|
+
:log => options[:env] || 'development'
|
26
|
+
}.merge(options.reject { |k, v| v.nil? })
|
27
|
+
|
28
|
+
db = Db.new(options)
|
29
|
+
log = Log.new(options)
|
30
|
+
mail = Mail.new(options)
|
31
|
+
|
32
|
+
ActionMailer::Base.logger = log
|
33
|
+
|
34
|
+
[ db, log, mail ]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
ActiveRecord::Base.default_timezone = :utc
|
@@ -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
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
$TESTING=true
|
2
|
+
SPEC = File.dirname(__FILE__)
|
3
|
+
$:.unshift File.expand_path("#{SPEC}/../lib")
|
4
|
+
|
5
|
+
require 'active_wrapper'
|
6
|
+
require 'pp'
|
7
|
+
|
8
|
+
Spec::Runner.configure do |config|
|
9
|
+
end
|
10
|
+
|
11
|
+
# For use with rspec textmate bundle
|
12
|
+
def debug(object)
|
13
|
+
puts "<pre>"
|
14
|
+
puts object.pretty_inspect.gsub('<', '<').gsub('>', '>')
|
15
|
+
puts "</pre>"
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_wrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Winton Welsh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-06 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - "="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.2
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: actionmailer
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.3.2
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email: mail@wintoni.us
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README.markdown
|
43
|
+
files:
|
44
|
+
- active_wrapper.gemspec
|
45
|
+
- gemspec.rb
|
46
|
+
- lib/active_wrapper/db.rb
|
47
|
+
- lib/active_wrapper/log.rb
|
48
|
+
- lib/active_wrapper/mail.rb
|
49
|
+
- lib/active_wrapper/tasks.rb
|
50
|
+
- lib/active_wrapper.rb
|
51
|
+
- MIT-LICENSE
|
52
|
+
- Rakefile
|
53
|
+
- README.markdown
|
54
|
+
- resources/migration.template
|
55
|
+
- spec/active_wrapper/db_spec.rb
|
56
|
+
- spec/active_wrapper/log_spec.rb
|
57
|
+
- spec/example_project/config/database.yml
|
58
|
+
- spec/example_project/db/migrate/001_test.rb
|
59
|
+
- spec/example_project/Rakefile
|
60
|
+
- spec/spec.opts
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
has_rdoc: true
|
63
|
+
homepage: http://github.com/winton/active_wrapper
|
64
|
+
licenses: []
|
65
|
+
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
version:
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: "0"
|
82
|
+
version:
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.3.5
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: Wraps ActiveRecord and Logger for use in non-Rails environments
|
90
|
+
test_files: []
|
91
|
+
|