citrusbyte-schemer 0.0.8 → 0.0.9
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/README.markdown +15 -1
- data/lib/schemer/migrator.rb +2 -2
- data/lib/schemer.rb +13 -4
- data/test/schemer_test.rb +22 -0
- metadata +2 -2
data/README.markdown
CHANGED
@@ -11,6 +11,9 @@ updated for you without worrying about migrations. Useful for when you want to
|
|
11
11
|
play around with real data handling during prototyping but you really don't
|
12
12
|
care about keeping the data or how it's defined.
|
13
13
|
|
14
|
+
This isn't meant to be a replacement for migrations, it is simply a way to get
|
15
|
+
started on a project quickly while you iron out your data definitions.
|
16
|
+
|
14
17
|
**WARNING:** This will create and delete data definitions on the fly with no
|
15
18
|
warning! Only use this with volatile data! Never attach it to an existing model
|
16
19
|
you care about as it will start adding and dropping columns without your
|
@@ -50,6 +53,15 @@ Works just fine, and you can drop it at any time!
|
|
50
53
|
|
51
54
|
Removes the `user_id` column the next time the `Widget` class is loaded.
|
52
55
|
|
56
|
+
Have a need for a particular type of column?
|
57
|
+
|
58
|
+
class Widget < ActiveRecord::Base
|
59
|
+
schema :name, { :size => :integer }, :description
|
60
|
+
end
|
61
|
+
|
62
|
+
Will create `size` as an `:integer` column so you can take advantage of Rails'
|
63
|
+
casting.
|
64
|
+
|
53
65
|
Feeling more confident and ready to make that big leap to migrations? Just run:
|
54
66
|
|
55
67
|
rake schemer:migration
|
@@ -66,13 +78,15 @@ To get:
|
|
66
78
|
|
67
79
|
create_table :widgets do |t|
|
68
80
|
t.string :name
|
81
|
+
t.integer :size
|
82
|
+
t.string :description
|
69
83
|
end
|
70
84
|
|
71
85
|
Then you can paste the output into a migration file, setup your columns (don't
|
72
86
|
forget your indexes!) and get rid of your `schema` calls in the listed classes
|
73
87
|
and you're on your way to the big leagues!
|
74
88
|
|
75
|
-
**NOTE:** All columns are created as string columns.
|
89
|
+
**NOTE:** All columns are created as string columns unless type is given.
|
76
90
|
|
77
91
|
Installation
|
78
92
|
------------
|
data/lib/schemer/migrator.rb
CHANGED
@@ -8,8 +8,8 @@ module Schemer
|
|
8
8
|
return nil unless klass.respond_to?(:schema_columns)
|
9
9
|
|
10
10
|
"create_table :#{klass.table_name} do |t|\n" +
|
11
|
-
(klass.schema_columns - klass.protected_columns).collect do |column|
|
12
|
-
" t.
|
11
|
+
(klass.schema_columns.keys - klass.protected_columns).collect do |column|
|
12
|
+
" t.#{klass.schema_columns[column]} :#{column}"
|
13
13
|
end.join("\n") +
|
14
14
|
"\nend"
|
15
15
|
end
|
data/lib/schemer.rb
CHANGED
@@ -5,7 +5,10 @@ module Schemer
|
|
5
5
|
extend ClassMethods
|
6
6
|
|
7
7
|
class_inheritable_accessor :schema_columns
|
8
|
-
self.schema_columns =
|
8
|
+
self.schema_columns = {}
|
9
|
+
args.collect{ |a| a.is_a?(Hash) ? a.stringify_keys : { a.to_s => :string } }.each do |column|
|
10
|
+
self.schema_columns.merge!(column)
|
11
|
+
end
|
9
12
|
|
10
13
|
update_schema
|
11
14
|
update_methods
|
@@ -17,7 +20,7 @@ module Schemer
|
|
17
20
|
def protected_columns
|
18
21
|
%w( id )
|
19
22
|
end
|
20
|
-
|
23
|
+
|
21
24
|
# Create the underlying table for this class
|
22
25
|
def create_table
|
23
26
|
ActiveRecord::Migration.create_table(table_name) do |t|;end;
|
@@ -33,8 +36,14 @@ module Schemer
|
|
33
36
|
# Update the underlying schema as defined by schema call
|
34
37
|
def update_schema
|
35
38
|
create_table unless table_exists?
|
36
|
-
|
37
|
-
(
|
39
|
+
|
40
|
+
(schema_columns.keys - column_names).each do |column|
|
41
|
+
ActiveRecord::Migration.add_column(table_name, column, schema_columns[column])
|
42
|
+
end
|
43
|
+
|
44
|
+
(column_names - protected_columns - schema_columns.keys).each do |column|
|
45
|
+
ActiveRecord::Migration.remove_column(table_name, column)
|
46
|
+
end
|
38
47
|
end
|
39
48
|
end
|
40
49
|
end
|
data/test/schemer_test.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'contest'
|
3
3
|
require 'override'
|
4
|
+
require 'ruby-debug'
|
4
5
|
require File.join(File.dirname(__FILE__), '..', 'lib', 'schemer')
|
5
6
|
require File.join(File.dirname(__FILE__), '..', 'lib', 'schemer', 'migrator')
|
6
7
|
|
@@ -49,6 +50,27 @@ class FooTest < Test::Unit::TestCase
|
|
49
50
|
end
|
50
51
|
end
|
51
52
|
|
53
|
+
context "with types" do
|
54
|
+
setup do
|
55
|
+
Foo.schema :foo, { :bar => :integer }, :baz
|
56
|
+
@foo = Foo.find(Foo.create!(:foo => '5', :bar => 5).id)
|
57
|
+
end
|
58
|
+
|
59
|
+
should "create foo, bar and baz columns" do
|
60
|
+
assert @foo.respond_to?(:foo)
|
61
|
+
assert @foo.respond_to?(:bar)
|
62
|
+
assert @foo.respond_to?(:baz)
|
63
|
+
end
|
64
|
+
|
65
|
+
should "create bar column using integer datatype" do
|
66
|
+
assert_equal 5, @foo.bar
|
67
|
+
end
|
68
|
+
|
69
|
+
should "create foo column using string datatype" do
|
70
|
+
assert_equal '5', @foo.foo
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
52
74
|
context "building a Rails migration" do
|
53
75
|
setup do
|
54
76
|
Foo.schema :foo, :bar
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: citrusbyte-schemer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Alavi
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-07-05 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|