active_wrapper-ar2 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/LICENSE +18 -0
- data/README.md +75 -0
- data/Rakefile +89 -0
- data/active_wrapper.gemspec +32 -0
- data/config/gemsets.yml +9 -0
- data/config/gemspec.yml +13 -0
- data/lib/active_wrapper.rb +35 -0
- data/lib/active_wrapper/db.rb +109 -0
- data/lib/active_wrapper/gems.rb +154 -0
- data/lib/active_wrapper/log.rb +39 -0
- data/lib/active_wrapper/tasks.rb +43 -0
- data/resources/migration.template +11 -0
- data/spec/active_wrapper/db_spec.rb +48 -0
- data/spec/active_wrapper/gems_spec.rb +249 -0
- data/spec/active_wrapper/log_spec.rb +28 -0
- data/spec/fixtures/example_project/Rakefile +8 -0
- data/spec/fixtures/example_project/config/database.yml +6 -0
- data/spec/fixtures/example_project/db/migrate/001_test.rb +10 -0
- data/spec/fixtures/gemsets.yml +9 -0
- data/spec/fixtures/gemspec.yml +15 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +13 -0
- metadata +37 -6
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright (c) 2010
|
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.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
ActiveWrapper
|
2
|
+
=============
|
3
|
+
|
4
|
+
Wraps ActiveRecord and Logger for use in non-Rails environments.
|
5
|
+
|
6
|
+
Compatibility
|
7
|
+
-------------
|
8
|
+
|
9
|
+
Maintained under Ruby 1.9.2.
|
10
|
+
|
11
|
+
Setup
|
12
|
+
-----
|
13
|
+
|
14
|
+
<pre>
|
15
|
+
gem install active_wrapper
|
16
|
+
</pre>
|
17
|
+
|
18
|
+
Usage
|
19
|
+
-----
|
20
|
+
|
21
|
+
<pre>
|
22
|
+
require 'rubygems'
|
23
|
+
require 'active_wrapper'
|
24
|
+
|
25
|
+
$db, $log = ActiveWrapper.setup(
|
26
|
+
:base => File.dirname(__FILE__),
|
27
|
+
:env => 'development',
|
28
|
+
:log => 'custom',
|
29
|
+
:stdout => true
|
30
|
+
)
|
31
|
+
|
32
|
+
$db.drop_db
|
33
|
+
$db.create_db
|
34
|
+
$db.establish_connection
|
35
|
+
$db.generate_migration('my_migration')
|
36
|
+
$db.migrate('001')
|
37
|
+
$db.migrate_reset
|
38
|
+
$log.info('log this')
|
39
|
+
$log.clear
|
40
|
+
</pre>
|
41
|
+
|
42
|
+
<code>ActiveWrapper</code> looks for the following files within the <code>:base</code> directory:
|
43
|
+
|
44
|
+
* <b>config/database.yml</b>
|
45
|
+
* <b>db/migrate/*.rb</b>
|
46
|
+
|
47
|
+
The <code>:env</code> option is <code>"development"</code> by default.
|
48
|
+
|
49
|
+
Logger
|
50
|
+
------
|
51
|
+
|
52
|
+
In the previous example, the log is stored in <b>log/custom.log</b>.
|
53
|
+
|
54
|
+
If no <code>:log</code> name is specified, the <code>:env</code> option is used for the log name.
|
55
|
+
|
56
|
+
You may also set <code>:log</code> to false to disable logging entirely.
|
57
|
+
|
58
|
+
Setting <code>:stdout</code> to true causes stdout and stderr to redirect to the logger. It is false by default.
|
59
|
+
|
60
|
+
Rakefile
|
61
|
+
--------
|
62
|
+
|
63
|
+
Add this to your project's <b>Rakefile</b> for database migration and log tasks:
|
64
|
+
|
65
|
+
<pre>
|
66
|
+
require 'rubygems'
|
67
|
+
require 'rake'
|
68
|
+
require 'active_wrapper/tasks'
|
69
|
+
|
70
|
+
ActiveWrapper::Tasks.new(:log => 'custom') do
|
71
|
+
# Put stuff you would normally put in the environment task here
|
72
|
+
end
|
73
|
+
</pre>
|
74
|
+
|
75
|
+
Pass the same options to <code>ActiveWrapper::Tasks.new</code> as you would <code>ActiveWrapper.new</code>.
|
data/Rakefile
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/lib/active_wrapper/gems'
|
2
|
+
|
3
|
+
ActiveWrapper::Gems.activate %w(rake rspec)
|
4
|
+
|
5
|
+
require 'rake'
|
6
|
+
require 'spec/rake/spectask'
|
7
|
+
|
8
|
+
def gemspec
|
9
|
+
@gemspec ||= begin
|
10
|
+
file = File.expand_path('../active_wrapper.gemspec', __FILE__)
|
11
|
+
eval(File.read(file), binding, file)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
if defined?(Spec::Rake::SpecTask)
|
16
|
+
desc "Run specs"
|
17
|
+
Spec::Rake::SpecTask.new do |t|
|
18
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
19
|
+
t.spec_opts = %w(-fs --color)
|
20
|
+
end
|
21
|
+
task :spec
|
22
|
+
task :default => :spec
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Build gem(s)"
|
26
|
+
task :gem do
|
27
|
+
old_gemset = ENV['GEMSET']
|
28
|
+
root = File.expand_path('../', __FILE__)
|
29
|
+
pkg = "#{root}/pkg"
|
30
|
+
system "rm -Rf #{pkg}"
|
31
|
+
ActiveWrapper::Gems.gemset_names.each do |gemset|
|
32
|
+
ENV['GEMSET'] = gemset.to_s
|
33
|
+
system "cd #{root} && gem build active_wrapper.gemspec"
|
34
|
+
system "mkdir -p #{pkg} && mv *.gem pkg"
|
35
|
+
end
|
36
|
+
ENV['GEMSET'] = old_gemset
|
37
|
+
end
|
38
|
+
|
39
|
+
namespace :gem do
|
40
|
+
desc "Install gem(s)"
|
41
|
+
task :install do
|
42
|
+
Rake::Task['gem'].invoke
|
43
|
+
Dir["#{File.dirname(__FILE__)}/pkg/*.gem"].each do |pkg|
|
44
|
+
system "gem install #{pkg} --no-ri --no-rdoc"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
desc "Push gem(s)"
|
49
|
+
task :push do
|
50
|
+
Rake::Task['gem'].invoke
|
51
|
+
Dir["#{File.dirname(__FILE__)}/pkg/*.gem"].each do |pkg|
|
52
|
+
system "gem push #{pkg}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
namespace :gems do
|
58
|
+
desc "Install gem dependencies (DEV=0 DOCS=0 GEMSPEC=default SUDO=0)"
|
59
|
+
task :install do
|
60
|
+
dev = ENV['DEV'] == '1'
|
61
|
+
docs = ENV['DOCS'] == '1' ? '' : '--no-ri --no-rdoc'
|
62
|
+
gemset = ENV['GEMSET']
|
63
|
+
sudo = ENV['SUDO'] == '1' ? 'sudo' : ''
|
64
|
+
|
65
|
+
ActiveWrapper::Gems.gemset = gemset if gemset
|
66
|
+
|
67
|
+
if dev
|
68
|
+
gems = ActiveWrapper::Gems.gemspec.development_dependencies
|
69
|
+
else
|
70
|
+
gems = ActiveWrapper::Gems.gemspec.dependencies
|
71
|
+
end
|
72
|
+
|
73
|
+
gems.each do |name|
|
74
|
+
name = name.to_s
|
75
|
+
version = ActiveWrapper::Gems.versions[name]
|
76
|
+
if Gem.source_index.find_name(name, version).empty?
|
77
|
+
version = version ? "-v #{version}" : ''
|
78
|
+
system "#{sudo} gem install #{name} #{version} #{docs}"
|
79
|
+
else
|
80
|
+
puts "already installed: #{name} #{version}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
desc "Validate the gemspec"
|
87
|
+
task :gemspec do
|
88
|
+
gemspec.validate
|
89
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
root = File.expand_path('../', __FILE__)
|
3
|
+
lib = "#{root}/lib"
|
4
|
+
$:.unshift lib unless $:.include?(lib)
|
5
|
+
|
6
|
+
require 'active_wrapper/gems'
|
7
|
+
ActiveWrapper::Gems.gemset ||= ENV['GEMSET'] || :default
|
8
|
+
|
9
|
+
Gem::Specification.new do |s|
|
10
|
+
ActiveWrapper::Gems.gemspec.hash.each do |key, value|
|
11
|
+
if key == 'name' && ActiveWrapper::Gems.gemset != :default
|
12
|
+
s.name = "#{value}-#{ActiveWrapper::Gems.gemset}"
|
13
|
+
elsif key == 'summary' && ActiveWrapper::Gems.gemset == :solo
|
14
|
+
s.summary = value + " (no dependencies)"
|
15
|
+
elsif !%w(dependencies development_dependencies).include?(key)
|
16
|
+
s.send "#{key}=", value
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
ActiveWrapper::Gems.dependencies.each do |g|
|
21
|
+
s.add_dependency g.to_s, ActiveWrapper::Gems.versions[g]
|
22
|
+
end
|
23
|
+
|
24
|
+
ActiveWrapper::Gems.development_dependencies.each do |g|
|
25
|
+
s.add_development_dependency g.to_s, ActiveWrapper::Gems.versions[g]
|
26
|
+
end
|
27
|
+
|
28
|
+
s.executables = `cd #{root} && git ls-files -- {bin}/*`.split("\n").collect { |f| File.basename(f) }
|
29
|
+
s.files = `cd #{root} && git ls-files`.split("\n")
|
30
|
+
s.require_paths = %w(lib)
|
31
|
+
s.test_files = `cd #{root} && git ls-files -- {features,test,spec}/*`.split("\n")
|
32
|
+
end
|
data/config/gemsets.yml
ADDED
data/config/gemspec.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
name: active_wrapper
|
2
|
+
version: 0.4.3
|
3
|
+
authors:
|
4
|
+
- Winton Welsh
|
5
|
+
email: mail@wintoni.us
|
6
|
+
homepage: http://github.com/winton/active_wrapper
|
7
|
+
summary: Wraps ActiveRecord and Logger for use in non-Rails environments
|
8
|
+
description: Wraps ActiveRecord and Logger for use in non-Rails environments
|
9
|
+
dependencies:
|
10
|
+
- activerecord
|
11
|
+
development_dependencies:
|
12
|
+
- rake
|
13
|
+
- rspec
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/active_wrapper/gems'
|
2
|
+
|
3
|
+
ActiveWrapper::Gems.activate %w(activerecord)
|
4
|
+
|
5
|
+
require 'active_record'
|
6
|
+
require 'fileutils'
|
7
|
+
require 'logger'
|
8
|
+
require 'yaml'
|
9
|
+
|
10
|
+
$:.unshift File.dirname(__FILE__) + '/active_wrapper'
|
11
|
+
|
12
|
+
require 'db'
|
13
|
+
require 'log'
|
14
|
+
|
15
|
+
module ActiveWrapper
|
16
|
+
class <<self
|
17
|
+
|
18
|
+
def setup(options={})
|
19
|
+
|
20
|
+
env = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || ENV['MERB_ENV'] || 'development'
|
21
|
+
options = {
|
22
|
+
:base => File.dirname($0),
|
23
|
+
:env => options[:env] || env,
|
24
|
+
:log => options[:env] || env
|
25
|
+
}.merge(options.reject { |k, v| v.nil? })
|
26
|
+
|
27
|
+
db = Db.new(options)
|
28
|
+
log = Log.new(options)
|
29
|
+
|
30
|
+
[ db, log ]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
ActiveRecord::Base.default_timezone = :utc
|
@@ -0,0 +1,109 @@
|
|
1
|
+
module ActiveWrapper
|
2
|
+
class Db
|
3
|
+
|
4
|
+
attr_reader :base, :config, :env
|
5
|
+
|
6
|
+
def initialize(options)
|
7
|
+
@adapter = options[:adapter]
|
8
|
+
@base = options[:base]
|
9
|
+
if File.exists?(path = "#{base}/config/database.yml")
|
10
|
+
@config = YAML::load(File.open(path))
|
11
|
+
else
|
12
|
+
raise "Could not find #{path}"
|
13
|
+
end
|
14
|
+
@env = options[:env].to_s
|
15
|
+
unless config[env]
|
16
|
+
raise "Environment \"#{env}\" not found in #{path}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def connected?
|
21
|
+
ActiveRecord::Base.connected?
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_db
|
25
|
+
establish_connection('database' => nil)
|
26
|
+
ActiveRecord::Base.connection.create_database config[env]['database']
|
27
|
+
establish_connection({})
|
28
|
+
end
|
29
|
+
|
30
|
+
def drop_db
|
31
|
+
establish_connection('database' => nil)
|
32
|
+
ActiveRecord::Base.connection.drop_database config[env]['database']
|
33
|
+
end
|
34
|
+
|
35
|
+
def establish_connection(options=nil)
|
36
|
+
if !connected? || options
|
37
|
+
config_clone = Marshal.load(Marshal.dump(config))
|
38
|
+
config_clone[env].merge!(options || {})
|
39
|
+
config_clone[env]['adapter'] = @adapter if @adapter
|
40
|
+
ActiveRecord::Base.configurations = config_clone
|
41
|
+
ActiveRecord::Base.establish_connection(env)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def migrate(version=nil)
|
46
|
+
establish_connection
|
47
|
+
redirect_stdout do
|
48
|
+
ActiveRecord::Migrator.migrate("#{base}/db/migrate", version)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def migrate_reset
|
53
|
+
redirect_stdout do
|
54
|
+
migrate(0)
|
55
|
+
migrate
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def generate_migration(name=nil)
|
60
|
+
redirect_stdout do
|
61
|
+
raise "Please specify desired migration name with NAME=my_migration_name" unless name
|
62
|
+
|
63
|
+
migration_name = name.strip.chomp
|
64
|
+
migrations_path = "#{base}/db/migrate"
|
65
|
+
migrations_template = File.expand_path("#{File.dirname(__FILE__)}/../../resources/migration.template")
|
66
|
+
|
67
|
+
# Find the highest existing migration version or set to 1
|
68
|
+
if (existing_migrations = Dir[File.join(migrations_path, '*.rb')]).length > 0
|
69
|
+
version = File.basename(existing_migrations.sort.reverse.first)[/^(\d+)_/,1].to_i + 1
|
70
|
+
else
|
71
|
+
version = 1
|
72
|
+
end
|
73
|
+
|
74
|
+
# Read the contents of the migration template into string
|
75
|
+
migrations_template = File.read(migrations_template)
|
76
|
+
|
77
|
+
# Replace the migration name in template with the acutal one
|
78
|
+
migration_content = migrations_template.gsub('__migration_name__', migration_name.camelize)
|
79
|
+
migration_content = migration_content.gsub('__migration_table__', migration_name)
|
80
|
+
|
81
|
+
# Generate migration filename
|
82
|
+
migration_filename = "#{"%03d" % version}_#{migration_name}.rb"
|
83
|
+
|
84
|
+
# Write the migration
|
85
|
+
File.open(File.join(migrations_path, migration_filename), "w+") do |migration|
|
86
|
+
migration.puts migration_content
|
87
|
+
end
|
88
|
+
|
89
|
+
# Done!
|
90
|
+
puts "Successfully created migration #{migration_filename}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def method_missing(method, *args)
|
95
|
+
ActiveRecord::Base.connection.send(method, *args)
|
96
|
+
end
|
97
|
+
|
98
|
+
def redirect_stdout(&block)
|
99
|
+
if env == 'test'
|
100
|
+
stdout = $stdout.dup
|
101
|
+
$stdout = $stdout.reopen(RUBY_PLATFORM =~ /mswin/ ? "NUL" : "/dev/null")
|
102
|
+
end
|
103
|
+
yield
|
104
|
+
if env == 'test'
|
105
|
+
$stdout.reopen(stdout)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
unless defined?(ActiveWrapper::Gems)
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
module ActiveWrapper
|
6
|
+
module Gems
|
7
|
+
class <<self
|
8
|
+
|
9
|
+
attr_accessor :config
|
10
|
+
attr_reader :gemset, :gemsets, :versions
|
11
|
+
|
12
|
+
class SimpleStruct
|
13
|
+
attr_reader :hash
|
14
|
+
|
15
|
+
def initialize(hash)
|
16
|
+
@hash = hash
|
17
|
+
@hash.each do |key, value|
|
18
|
+
self.class.send(:define_method, key) { @hash[key] }
|
19
|
+
self.class.send(:define_method, "#{key}=") { |v| @hash[key] = v }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
Gems.config = SimpleStruct.new(
|
25
|
+
:gemsets => [ "#{File.expand_path('../../../', __FILE__)}/config/gemsets.yml" ],
|
26
|
+
:gemspec => "#{File.expand_path('../../../', __FILE__)}/config/gemspec.yml",
|
27
|
+
:warn => true
|
28
|
+
)
|
29
|
+
|
30
|
+
def activate(*gems)
|
31
|
+
begin
|
32
|
+
require 'rubygems' unless defined?(::Gem)
|
33
|
+
rescue LoadError
|
34
|
+
puts "rubygems library could not be required" if @config.warn
|
35
|
+
end
|
36
|
+
|
37
|
+
self.gemset ||= gemset_from_loaded_specs
|
38
|
+
|
39
|
+
gems.flatten.collect(&:to_sym).each do |name|
|
40
|
+
version = @versions[name]
|
41
|
+
vendor = File.expand_path("../../../vendor/#{name}/lib", __FILE__)
|
42
|
+
if File.exists?(vendor)
|
43
|
+
$:.unshift vendor
|
44
|
+
elsif defined?(gem)
|
45
|
+
gem name.to_s, version
|
46
|
+
else
|
47
|
+
puts "#{name} #{"(#{version})" if version} failed to activate" if @config.warn
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def dependencies
|
53
|
+
dependency_filter(@gemspec.dependencies, @gemset)
|
54
|
+
end
|
55
|
+
|
56
|
+
def development_dependencies
|
57
|
+
dependency_filter(@gemspec.development_dependencies, @gemset)
|
58
|
+
end
|
59
|
+
|
60
|
+
def gemset=(gemset)
|
61
|
+
if gemset
|
62
|
+
@gemset = gemset.to_sym
|
63
|
+
|
64
|
+
@gemsets = @config.gemsets.reverse.collect { |config|
|
65
|
+
if config.is_a?(::String)
|
66
|
+
YAML::load(File.read(config)) rescue {}
|
67
|
+
elsif config.is_a?(::Hash)
|
68
|
+
config
|
69
|
+
end
|
70
|
+
}.inject({}) do |hash, config|
|
71
|
+
deep_merge(hash, symbolize_keys(config))
|
72
|
+
end
|
73
|
+
|
74
|
+
@versions = (@gemsets[gemspec.name.to_sym] || {}).inject({}) do |hash, (key, value)|
|
75
|
+
if !value.is_a?(::Hash) && value
|
76
|
+
hash[key] = value
|
77
|
+
elsif key == @gemset
|
78
|
+
(value || {}).each { |k, v| hash[k] = v }
|
79
|
+
end
|
80
|
+
hash
|
81
|
+
end
|
82
|
+
else
|
83
|
+
@gemset = nil
|
84
|
+
@gemsets = nil
|
85
|
+
@versions = nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def gemset_names
|
90
|
+
(
|
91
|
+
[ :default ] +
|
92
|
+
@gemsets[gemspec.name.to_sym].inject([]) { |array, (key, value)|
|
93
|
+
array.push(key) if value.is_a?(::Hash) || value.nil?
|
94
|
+
array
|
95
|
+
}
|
96
|
+
).uniq
|
97
|
+
end
|
98
|
+
|
99
|
+
def gemspec(reload=false)
|
100
|
+
if @gemspec && !reload
|
101
|
+
@gemspec
|
102
|
+
else
|
103
|
+
data = YAML::load(File.read(@config.gemspec)) rescue {}
|
104
|
+
@gemspec = SimpleStruct.new(data)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
private
|
109
|
+
|
110
|
+
def deep_merge(first, second)
|
111
|
+
merger = lambda do |key, v1, v2|
|
112
|
+
Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2
|
113
|
+
end
|
114
|
+
first.merge(second, &merger)
|
115
|
+
end
|
116
|
+
|
117
|
+
def dependency_filter(dependencies, match)
|
118
|
+
(dependencies || []).inject([]) { |array, value|
|
119
|
+
if value.is_a?(::Hash)
|
120
|
+
array += value[match.to_s] if value[match.to_s]
|
121
|
+
else
|
122
|
+
array << value
|
123
|
+
end
|
124
|
+
array
|
125
|
+
}.uniq.collect(&:to_sym)
|
126
|
+
end
|
127
|
+
|
128
|
+
def gemset_from_loaded_specs
|
129
|
+
if defined?(Gem)
|
130
|
+
Gem.loaded_specs.each do |name, spec|
|
131
|
+
if name == gemspec.name
|
132
|
+
return :default
|
133
|
+
elsif name[0..gemspec.name.length] == "#{gemspec.name}-"
|
134
|
+
return name[gemspec.name.length+1..-1].to_sym
|
135
|
+
end
|
136
|
+
end
|
137
|
+
:default
|
138
|
+
else
|
139
|
+
:none
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def symbolize_keys(hash)
|
144
|
+
return {} unless hash.is_a?(::Hash)
|
145
|
+
hash.inject({}) do |options, (key, value)|
|
146
|
+
value = symbolize_keys(value) if value.is_a?(::Hash)
|
147
|
+
options[(key.to_sym rescue key) || key] = value
|
148
|
+
options
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
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,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,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 => $root + '/spec/fixtures/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'
|
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 = $root + "/spec/fixtures/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,249 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveWrapper::Gems do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@old_config = ActiveWrapper::Gems.config
|
7
|
+
|
8
|
+
ActiveWrapper::Gems.config.gemspec = "#{$root}/spec/fixtures/gemspec.yml"
|
9
|
+
ActiveWrapper::Gems.config.gemsets = [
|
10
|
+
"#{$root}/spec/fixtures/gemsets.yml"
|
11
|
+
]
|
12
|
+
ActiveWrapper::Gems.config.warn = true
|
13
|
+
|
14
|
+
ActiveWrapper::Gems.gemspec true
|
15
|
+
ActiveWrapper::Gems.gemset = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
after(:each) do
|
19
|
+
ActiveWrapper::Gems.config = @old_config
|
20
|
+
end
|
21
|
+
|
22
|
+
describe :activate do
|
23
|
+
it "should activate gems" do
|
24
|
+
ActiveWrapper::Gems.stub!(:gem)
|
25
|
+
ActiveWrapper::Gems.should_receive(:gem).with('rspec', '=1.3.1')
|
26
|
+
ActiveWrapper::Gems.should_receive(:gem).with('rake', '=0.8.7')
|
27
|
+
ActiveWrapper::Gems.activate :rspec, 'rake'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe :gemset= do
|
32
|
+
before(:each) do
|
33
|
+
ActiveWrapper::Gems.config.gemsets = [
|
34
|
+
{
|
35
|
+
:name => {
|
36
|
+
:rake => '>0.8.6',
|
37
|
+
:default => {
|
38
|
+
:externals => '=1.0.2'
|
39
|
+
}
|
40
|
+
}
|
41
|
+
},
|
42
|
+
"#{$root}/spec/fixtures/gemsets.yml"
|
43
|
+
]
|
44
|
+
end
|
45
|
+
|
46
|
+
describe :default do
|
47
|
+
before(:each) do
|
48
|
+
ActiveWrapper::Gems.gemset = :default
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should set @gemset" do
|
52
|
+
ActiveWrapper::Gems.gemset.should == :default
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should set @gemsets" do
|
56
|
+
ActiveWrapper::Gems.gemsets.should == {
|
57
|
+
:name => {
|
58
|
+
:rake => ">0.8.6",
|
59
|
+
:default => {
|
60
|
+
:externals => '=1.0.2',
|
61
|
+
:mysql => "=2.8.1",
|
62
|
+
:rspec => "=1.3.1"
|
63
|
+
},
|
64
|
+
:rspec2 => {
|
65
|
+
:mysql2 => "=0.2.6",
|
66
|
+
:rspec => "=2.3.0"
|
67
|
+
},
|
68
|
+
:solo => nil
|
69
|
+
}
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should set Gems.versions" do
|
74
|
+
ActiveWrapper::Gems.versions.should == {
|
75
|
+
:externals => "=1.0.2",
|
76
|
+
:mysql => "=2.8.1",
|
77
|
+
:rake => ">0.8.6",
|
78
|
+
:rspec => "=1.3.1"
|
79
|
+
}
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should return proper values for Gems.dependencies" do
|
83
|
+
ActiveWrapper::Gems.dependencies.should == [ :rake, :mysql ]
|
84
|
+
ActiveWrapper::Gems.development_dependencies.should == []
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should return proper values for Gems.gemset_names" do
|
88
|
+
ActiveWrapper::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe :rspec2 do
|
93
|
+
before(:each) do
|
94
|
+
ActiveWrapper::Gems.gemset = "rspec2"
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should set @gemset" do
|
98
|
+
ActiveWrapper::Gems.gemset.should == :rspec2
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should set @gemsets" do
|
102
|
+
ActiveWrapper::Gems.gemsets.should == {
|
103
|
+
:name => {
|
104
|
+
:rake => ">0.8.6",
|
105
|
+
:default => {
|
106
|
+
:externals => '=1.0.2',
|
107
|
+
:mysql => "=2.8.1",
|
108
|
+
:rspec => "=1.3.1"
|
109
|
+
},
|
110
|
+
:rspec2 => {
|
111
|
+
:mysql2=>"=0.2.6",
|
112
|
+
:rspec => "=2.3.0"
|
113
|
+
},
|
114
|
+
:solo => nil
|
115
|
+
}
|
116
|
+
}
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should set Gems.versions" do
|
120
|
+
ActiveWrapper::Gems.versions.should == {
|
121
|
+
:mysql2 => "=0.2.6",
|
122
|
+
:rake => ">0.8.6",
|
123
|
+
:rspec => "=2.3.0"
|
124
|
+
}
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should return proper values for Gems.dependencies" do
|
128
|
+
ActiveWrapper::Gems.dependencies.should == [ :rake, :mysql2 ]
|
129
|
+
ActiveWrapper::Gems.development_dependencies.should == []
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should return proper values for Gems.gemset_names" do
|
133
|
+
ActiveWrapper::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe :solo do
|
138
|
+
before(:each) do
|
139
|
+
ActiveWrapper::Gems.gemset = :solo
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should set @gemset" do
|
143
|
+
ActiveWrapper::Gems.gemset.should == :solo
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should set @gemsets" do
|
147
|
+
ActiveWrapper::Gems.gemsets.should == {
|
148
|
+
:name => {
|
149
|
+
:rake => ">0.8.6",
|
150
|
+
:default => {
|
151
|
+
:externals => '=1.0.2',
|
152
|
+
:mysql => "=2.8.1",
|
153
|
+
:rspec => "=1.3.1"
|
154
|
+
},
|
155
|
+
:rspec2 => {
|
156
|
+
:mysql2=>"=0.2.6",
|
157
|
+
:rspec => "=2.3.0"
|
158
|
+
},
|
159
|
+
:solo => nil
|
160
|
+
}
|
161
|
+
}
|
162
|
+
end
|
163
|
+
|
164
|
+
it "should set Gems.versions" do
|
165
|
+
ActiveWrapper::Gems.versions.should == {:rake=>">0.8.6"}
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should return proper values for Gems.dependencies" do
|
169
|
+
ActiveWrapper::Gems.dependencies.should == [:rake]
|
170
|
+
ActiveWrapper::Gems.development_dependencies.should == []
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should return proper values for Gems.gemset_names" do
|
174
|
+
ActiveWrapper::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
describe :nil do
|
179
|
+
before(:each) do
|
180
|
+
ActiveWrapper::Gems.gemset = nil
|
181
|
+
end
|
182
|
+
|
183
|
+
it "should set everything to nil" do
|
184
|
+
ActiveWrapper::Gems.gemset.should == nil
|
185
|
+
ActiveWrapper::Gems.gemsets.should == nil
|
186
|
+
ActiveWrapper::Gems.versions.should == nil
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
describe :gemset_from_loaded_specs do
|
192
|
+
before(:each) do
|
193
|
+
Gem.stub!(:loaded_specs)
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should return the correct gemset for name gem" do
|
197
|
+
Gem.should_receive(:loaded_specs).and_return({ "name" => nil })
|
198
|
+
ActiveWrapper::Gems.send(:gemset_from_loaded_specs).should == :default
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should return the correct gemset for name-rspec gem" do
|
202
|
+
Gem.should_receive(:loaded_specs).and_return({ "name-rspec2" => nil })
|
203
|
+
ActiveWrapper::Gems.send(:gemset_from_loaded_specs).should == :rspec2
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
describe :reload_gemspec do
|
208
|
+
it "should populate @gemspec" do
|
209
|
+
ActiveWrapper::Gems.gemspec.hash.should == {
|
210
|
+
"name" => "name",
|
211
|
+
"version" => "0.1.0",
|
212
|
+
"authors" => ["Author"],
|
213
|
+
"email" => "email@email.com",
|
214
|
+
"homepage" => "http://github.com/author/name",
|
215
|
+
"summary" => "Summary",
|
216
|
+
"description" => "Description",
|
217
|
+
"dependencies" => [
|
218
|
+
"rake",
|
219
|
+
{ "default" => [ "mysql" ] },
|
220
|
+
{ "rspec2" => [ "mysql2" ] }
|
221
|
+
],
|
222
|
+
"development_dependencies" => nil
|
223
|
+
}
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should create methods from keys of @gemspec" do
|
227
|
+
ActiveWrapper::Gems.gemspec.name.should == "name"
|
228
|
+
ActiveWrapper::Gems.gemspec.version.should == "0.1.0"
|
229
|
+
ActiveWrapper::Gems.gemspec.authors.should == ["Author"]
|
230
|
+
ActiveWrapper::Gems.gemspec.email.should == "email@email.com"
|
231
|
+
ActiveWrapper::Gems.gemspec.homepage.should == "http://github.com/author/name"
|
232
|
+
ActiveWrapper::Gems.gemspec.summary.should == "Summary"
|
233
|
+
ActiveWrapper::Gems.gemspec.description.should == "Description"
|
234
|
+
ActiveWrapper::Gems.gemspec.dependencies.should == [
|
235
|
+
"rake",
|
236
|
+
{ "default" => ["mysql"] },
|
237
|
+
{ "rspec2" => [ "mysql2" ] }
|
238
|
+
]
|
239
|
+
ActiveWrapper::Gems.gemspec.development_dependencies.should == nil
|
240
|
+
end
|
241
|
+
|
242
|
+
it "should produce a valid gemspec" do
|
243
|
+
ActiveWrapper::Gems.gemset = :default
|
244
|
+
gemspec = File.expand_path("../../../active_wrapper.gemspec", __FILE__)
|
245
|
+
gemspec = eval(File.read(gemspec), binding, gemspec)
|
246
|
+
gemspec.validate.should == true
|
247
|
+
end
|
248
|
+
end
|
249
|
+
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 = $root + "/spec/fixtures/example_project/log/test.log")
|
7
|
+
$db, $log, $mail = ActiveWrapper.setup(
|
8
|
+
:base => $root + '/spec/fixtures/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,15 @@
|
|
1
|
+
name: name
|
2
|
+
version: 0.1.0
|
3
|
+
authors:
|
4
|
+
- Author
|
5
|
+
email: email@email.com
|
6
|
+
homepage: http://github.com/author/name
|
7
|
+
summary: Summary
|
8
|
+
description: Description
|
9
|
+
dependencies:
|
10
|
+
- rake
|
11
|
+
- default:
|
12
|
+
- mysql
|
13
|
+
- rspec2:
|
14
|
+
- mysql2
|
15
|
+
development_dependencies: null
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'pp'
|
2
|
+
|
3
|
+
$root = File.expand_path('../../', __FILE__)
|
4
|
+
|
5
|
+
require "#{$root}/lib/active_wrapper/gems"
|
6
|
+
|
7
|
+
ActiveWrapper::Gems.gemset = ENV['GEMSET'] if ENV['GEMSET']
|
8
|
+
ActiveWrapper::Gems.activate :rspec
|
9
|
+
|
10
|
+
require "#{$root}/lib/active_wrapper"
|
11
|
+
|
12
|
+
Spec::Runner.configure do |config|
|
13
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
8
|
+
- 3
|
9
|
+
version: 0.4.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Winton Welsh
|
@@ -68,8 +68,30 @@ extensions: []
|
|
68
68
|
|
69
69
|
extra_rdoc_files: []
|
70
70
|
|
71
|
-
files:
|
72
|
-
|
71
|
+
files:
|
72
|
+
- .gitignore
|
73
|
+
- LICENSE
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- active_wrapper.gemspec
|
77
|
+
- config/gemsets.yml
|
78
|
+
- config/gemspec.yml
|
79
|
+
- lib/active_wrapper.rb
|
80
|
+
- lib/active_wrapper/db.rb
|
81
|
+
- lib/active_wrapper/gems.rb
|
82
|
+
- lib/active_wrapper/log.rb
|
83
|
+
- lib/active_wrapper/tasks.rb
|
84
|
+
- resources/migration.template
|
85
|
+
- spec/active_wrapper/db_spec.rb
|
86
|
+
- spec/active_wrapper/gems_spec.rb
|
87
|
+
- spec/active_wrapper/log_spec.rb
|
88
|
+
- spec/fixtures/example_project/Rakefile
|
89
|
+
- spec/fixtures/example_project/config/database.yml
|
90
|
+
- spec/fixtures/example_project/db/migrate/001_test.rb
|
91
|
+
- spec/fixtures/gemsets.yml
|
92
|
+
- spec/fixtures/gemspec.yml
|
93
|
+
- spec/spec.opts
|
94
|
+
- spec/spec_helper.rb
|
73
95
|
has_rdoc: true
|
74
96
|
homepage: http://github.com/winton/active_wrapper
|
75
97
|
licenses: []
|
@@ -102,5 +124,14 @@ rubygems_version: 1.3.7
|
|
102
124
|
signing_key:
|
103
125
|
specification_version: 3
|
104
126
|
summary: Wraps ActiveRecord and Logger for use in non-Rails environments
|
105
|
-
test_files:
|
106
|
-
|
127
|
+
test_files:
|
128
|
+
- spec/active_wrapper/db_spec.rb
|
129
|
+
- spec/active_wrapper/gems_spec.rb
|
130
|
+
- spec/active_wrapper/log_spec.rb
|
131
|
+
- spec/fixtures/example_project/Rakefile
|
132
|
+
- spec/fixtures/example_project/config/database.yml
|
133
|
+
- spec/fixtures/example_project/db/migrate/001_test.rb
|
134
|
+
- spec/fixtures/gemsets.yml
|
135
|
+
- spec/fixtures/gemspec.yml
|
136
|
+
- spec/spec.opts
|
137
|
+
- spec/spec_helper.rb
|