winton-active_wrapper 0.1.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 +68 -0
- data/Rakefile +54 -0
- data/active_wrapper.gemspec +27 -0
- data/gemspec.rb +17 -0
- data/lib/active_wrapper/db.rb +61 -0
- data/lib/active_wrapper/log.rb +39 -0
- data/lib/active_wrapper/tasks.rb +35 -0
- data/lib/active_wrapper.rb +16 -0
- data/resources/migration.template +11 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +16 -0
- metadata +68 -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,68 @@
|
|
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 = ActiveWrapper.new(
|
21
|
+
:base => File.dirname('__FILE__'),
|
22
|
+
:env => 'development',
|
23
|
+
:log => 'custom',
|
24
|
+
:stdout => true
|
25
|
+
)
|
26
|
+
|
27
|
+
$db.establish_connection
|
28
|
+
$db.generate_migration('my_migration')
|
29
|
+
$db.migrate('001')
|
30
|
+
$db.migrate_reset
|
31
|
+
$log.info('log this')
|
32
|
+
$log.clear
|
33
|
+
</pre>
|
34
|
+
|
35
|
+
<code>ActiveWrapper</code> looks for the following files within the <code>:base</code> directory:
|
36
|
+
|
37
|
+
* <b>config/database.yml</b>
|
38
|
+
* <b>db/migrate/*.rb</b>
|
39
|
+
|
40
|
+
The <code>:env</code> option is <code>"development"</code> by default.
|
41
|
+
|
42
|
+
Logger
|
43
|
+
------
|
44
|
+
|
45
|
+
In the previous example, the log is stored in <b>log/custom.log</b>.
|
46
|
+
|
47
|
+
If no <code>:log</code> name is specified, the <code>:env</code> option is used for the log name.
|
48
|
+
|
49
|
+
You may also set <code>:log</code> to false to disable logging entirely.
|
50
|
+
|
51
|
+
Setting <code>:stdout</code> to true causes stdout and stderr to redirect to the logger. It is false by default.
|
52
|
+
|
53
|
+
Rakefile
|
54
|
+
--------
|
55
|
+
|
56
|
+
Add this to your project's <b>Rakefile</b> for database migration and log tasks:
|
57
|
+
|
58
|
+
<pre>
|
59
|
+
require 'rubygems'
|
60
|
+
require 'rake'
|
61
|
+
require 'active_wrapper/tasks'
|
62
|
+
|
63
|
+
ActiveWrapper::Tasks.new(:log => 'custom') do
|
64
|
+
# Put stuff you would normally put in the environment task here
|
65
|
+
end
|
66
|
+
</pre>
|
67
|
+
|
68
|
+
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,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{active_wrapper}
|
5
|
+
s.version = "0.1.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-06-14}
|
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/tasks.rb", "lib/active_wrapper.rb", "MIT-LICENSE", "Rakefile", "README.markdown", "resources", "resources/migration.template", "spec", "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
|
+
else
|
24
|
+
end
|
25
|
+
else
|
26
|
+
end
|
27
|
+
end
|
data/gemspec.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
GEM_NAME = 'active_wrapper'
|
2
|
+
GEM_FILES = FileList['**/*'] - FileList['coverage', 'coverage/**/*', 'pkg', 'pkg/**/*']
|
3
|
+
GEM_SPEC = Gem::Specification.new do |s|
|
4
|
+
# == CONFIGURE ==
|
5
|
+
s.author = "Winton Welsh"
|
6
|
+
s.email = "mail@wintoni.us"
|
7
|
+
s.homepage = "http://github.com/winton/#{GEM_NAME}"
|
8
|
+
s.summary = "Wraps ActiveRecord and Logger for use in non-Rails environments"
|
9
|
+
# == CONFIGURE ==
|
10
|
+
s.extra_rdoc_files = [ "README.markdown" ]
|
11
|
+
s.files = GEM_FILES.to_a
|
12
|
+
s.has_rdoc = false
|
13
|
+
s.name = GEM_NAME
|
14
|
+
s.platform = Gem::Platform::RUBY
|
15
|
+
s.require_path = "lib"
|
16
|
+
s.version = "0.1.0"
|
17
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module ActiveWrapper
|
2
|
+
class Db
|
3
|
+
|
4
|
+
attr_reader :base, :env
|
5
|
+
|
6
|
+
def initialize(options)
|
7
|
+
@base = options[:base]
|
8
|
+
@env = options[:env]
|
9
|
+
end
|
10
|
+
|
11
|
+
def establish_connection
|
12
|
+
unless ActiveRecord::Base.connected?
|
13
|
+
config = YAML::load(File.open("#{base}/config/database.yml"))
|
14
|
+
ActiveRecord::Base.configurations = config
|
15
|
+
ActiveRecord::Base.establish_connection(env)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def migrate(version=nil)
|
20
|
+
ActiveRecord::Migrator.migrate("#{base}/db/migrate", version)
|
21
|
+
end
|
22
|
+
|
23
|
+
def migrate_reset
|
24
|
+
db_migrate(0)
|
25
|
+
db_migrate
|
26
|
+
end
|
27
|
+
|
28
|
+
def generate_migration(name=nil)
|
29
|
+
raise "Please specify desired migration name with NAME=my_migration_name" unless name
|
30
|
+
|
31
|
+
migration_name = name.strip.chomp
|
32
|
+
migrations_path = "#{base}/db/migrate"
|
33
|
+
migrations_template = File.expand_path("#{File.dirname(__FILE__)}/../../resources/migration.template")
|
34
|
+
|
35
|
+
# Find the highest existing migration version or set to 1
|
36
|
+
if (existing_migrations = Dir[File.join(migrations_path, '*.rb')]).length > 0
|
37
|
+
version = File.basename(existing_migrations.sort.reverse.first)[/^(\d+)_/,1].to_i + 1
|
38
|
+
else
|
39
|
+
version = 1
|
40
|
+
end
|
41
|
+
|
42
|
+
# Read the contents of the migration template into string
|
43
|
+
migrations_template = File.read(migrations_template)
|
44
|
+
|
45
|
+
# Replace the migration name in template with the acutal one
|
46
|
+
migration_content = migrations_template.gsub('__migration_name__', migration_name.camelize)
|
47
|
+
migration_content = migration_content.gsub('__migration_table__', migration_name)
|
48
|
+
|
49
|
+
# Generate migration filename
|
50
|
+
migration_filename = "#{"%03d" % version}_#{migration_name}.rb"
|
51
|
+
|
52
|
+
# Write the migration
|
53
|
+
File.open(File.join(migrations_path, migration_filename), "w+") do |migration|
|
54
|
+
migration.puts migration_content
|
55
|
+
end
|
56
|
+
|
57
|
+
# Done!
|
58
|
+
puts "Successfully created migration #{migration_filename}"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
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,35 @@
|
|
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.new(options)
|
10
|
+
yield
|
11
|
+
end
|
12
|
+
|
13
|
+
namespace :db do
|
14
|
+
desc "Migrate the database with optional VERSION"
|
15
|
+
task :migrate => :environment do
|
16
|
+
$db.migrate(ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
namespace :generate do
|
21
|
+
desc "Generate a migration with given NAME"
|
22
|
+
task :migration => :environment do
|
23
|
+
$db.generate_migration(ENV['NAME'])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
namespace :log do
|
28
|
+
desc "Clear all logs"
|
29
|
+
task :clear => :environment do
|
30
|
+
$log.clear
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/active_wrapper/db"
|
2
|
+
require File.dirname(__FILE__) + "/active_wrapper/log"
|
3
|
+
|
4
|
+
class ActiveWrapper
|
5
|
+
|
6
|
+
def initialize(options={})
|
7
|
+
|
8
|
+
options = {
|
9
|
+
:base => File.dirname($0),
|
10
|
+
:env => 'development',
|
11
|
+
:log => 'development'
|
12
|
+
}.merge(options)
|
13
|
+
|
14
|
+
[ Db.new(options), Log.new(options) ]
|
15
|
+
end
|
16
|
+
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,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: winton-active_wrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Winton Welsh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-14 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: mail@wintoni.us
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.markdown
|
24
|
+
files:
|
25
|
+
- active_wrapper.gemspec
|
26
|
+
- gemspec.rb
|
27
|
+
- lib
|
28
|
+
- lib/active_wrapper
|
29
|
+
- lib/active_wrapper/db.rb
|
30
|
+
- lib/active_wrapper/log.rb
|
31
|
+
- lib/active_wrapper/tasks.rb
|
32
|
+
- lib/active_wrapper.rb
|
33
|
+
- MIT-LICENSE
|
34
|
+
- Rakefile
|
35
|
+
- README.markdown
|
36
|
+
- resources
|
37
|
+
- resources/migration.template
|
38
|
+
- spec
|
39
|
+
- spec/spec.opts
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
has_rdoc: false
|
42
|
+
homepage: http://github.com/winton/active_wrapper
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.2.0
|
64
|
+
signing_key:
|
65
|
+
specification_version: 2
|
66
|
+
summary: Wraps ActiveRecord and Logger for use in non-Rails environments
|
67
|
+
test_files: []
|
68
|
+
|