citrusbyte-schemer 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/schemer.rb +27 -13
- data/rails/init.rb +1 -2
- data/test/schemer_test.rb +41 -3
- metadata +1 -1
data/lib/schemer.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'activerecord'
|
2
|
+
|
1
3
|
module Schemer
|
2
4
|
def schema(*args)
|
3
5
|
extend ClassMethods
|
@@ -8,27 +10,39 @@ module Schemer
|
|
8
10
|
end
|
9
11
|
|
10
12
|
module ClassMethods
|
13
|
+
# Columns which we don't touch regardless of not being defined in schema
|
14
|
+
# (and are ignored if they are defined in schema)
|
11
15
|
def protected_columns
|
12
16
|
%w( id )
|
13
17
|
end
|
18
|
+
|
19
|
+
# Create the underlying table for this class
|
20
|
+
def create_table
|
21
|
+
ActiveRecord::Migration.create_table(table_name) do |t|;end;
|
22
|
+
end
|
23
|
+
|
24
|
+
# Update ActiveRecord's automatically generated methods so we don't have to
|
25
|
+
# reload for schema changes to take effect
|
26
|
+
def update_methods
|
27
|
+
generated_methods.each { |method| remove_method(method) }
|
28
|
+
@columns = @column_names = @columns_hash = @generated_methods = nil
|
29
|
+
end
|
30
|
+
|
31
|
+
# Update the underlying schema as defined by schema call
|
32
|
+
def update_schema
|
33
|
+
create_table unless table_exists?
|
34
|
+
(schema_columns - column_names).each { |column| ActiveRecord::Migration.add_column(table_name, column, :string) }
|
35
|
+
(column_names - protected_columns - schema_columns).each { |column| ActiveRecord::Migration.remove_column(table_name, column) }
|
36
|
+
end
|
14
37
|
end
|
15
38
|
|
16
39
|
module InstanceMethods
|
17
40
|
def initialize(*args)
|
18
|
-
update_schema
|
41
|
+
self.class.update_schema
|
42
|
+
self.class.update_methods
|
19
43
|
super
|
20
44
|
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def update_schema
|
25
|
-
ActiveRecord::Migration.create_table(self.class.table_name) do |t|;end; unless self.class.table_exists?
|
26
|
-
self.class.schema_columns.each do |column|
|
27
|
-
ActiveRecord::Migration.add_column(self.class.table_name, column, :string) unless respond_to?(column.to_sym)
|
28
|
-
(self.class.column_names - self.class.protected_columns).each do |column|
|
29
|
-
ActiveRecord::Migration.remove_column(self.class.table_name, column) unless self.class.schema_columns.include?(column.to_s)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
45
|
end
|
34
46
|
end
|
47
|
+
|
48
|
+
ActiveRecord::Base.extend(Schemer)
|
data/rails/init.rb
CHANGED
@@ -1,3 +1,2 @@
|
|
1
|
+
raise "We highly suggest you only use Schemer in development mode!" unless Rails.env.to_s == 'development'
|
1
2
|
require 'schemer'
|
2
|
-
raise "We would highly suggest you only use Schemer in development mode!" unless Rails.env.to_s == 'development'
|
3
|
-
ActiveRecord::Base.extend(Schemer)
|
data/test/schemer_test.rb
CHANGED
@@ -1,5 +1,43 @@
|
|
1
|
-
require
|
1
|
+
require 'rubygems'
|
2
|
+
require 'contest'
|
3
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'schemer')
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => 'test/schemer_test.db'
|
6
|
+
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
|
7
|
+
|
8
|
+
class Foo < ActiveRecord::Base;end;
|
9
|
+
ActiveRecord::Migration.drop_table(Foo.table_name) if Foo.table_exists?
|
10
|
+
|
11
|
+
class FooTest < Test::Unit::TestCase
|
12
|
+
context "defining the schema" do
|
13
|
+
setup do
|
14
|
+
Foo.schema :foo, :bar
|
15
|
+
@foo = Foo.new
|
16
|
+
end
|
17
|
+
|
18
|
+
should "create the foos table" do
|
19
|
+
assert Foo.table_exists?
|
20
|
+
end
|
21
|
+
|
22
|
+
should "create the foo column" do
|
23
|
+
assert @foo.respond_to?(:foo)
|
24
|
+
end
|
25
|
+
|
26
|
+
should "create the bar column" do
|
27
|
+
assert @foo.respond_to?(:bar)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "updating the schema" do
|
32
|
+
setup do
|
33
|
+
Foo.schema :foo, :bar
|
34
|
+
@foo = Foo.new
|
35
|
+
Foo.schema :foo
|
36
|
+
@foo = Foo.new
|
37
|
+
end
|
38
|
+
|
39
|
+
should "remove the bar column" do
|
40
|
+
assert !@foo.respond_to?(:bar)
|
41
|
+
end
|
42
|
+
end
|
5
43
|
end
|