auto_ar 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,5 @@
1
+ Manifest
2
+ Rakefile
3
+ lib/auto_ar.rb
4
+ test/auto_ar_test.rb
5
+ test/test_helper.rb
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('auto_ar', '0.0.1') do |p|
6
+ p.description = "A gem that makes ActiveRecord subclasses on the fly"
7
+ p.url = "http://github.com/tmaeda/auto_ar"
8
+ p.author = "Tomoki MAEDA"
9
+ p.email = "tmaeda _at_ ruby-sapporo.org"
10
+ end
11
+
data/auto_ar.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{auto_ar}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Tomoki MAEDA"]
9
+ s.cert_chain = ["/home/tmaeda/.gemcert/gem-public_cert.pem"]
10
+ s.date = %q{2010-06-20}
11
+ s.description = %q{A gem that makes ActiveRecord subclasses on the fly}
12
+ s.email = %q{tmaeda _at_ ruby-sapporo.org}
13
+ s.extra_rdoc_files = ["lib/auto_ar.rb"]
14
+ s.files = ["Manifest", "Rakefile", "lib/auto_ar.rb", "test/auto_ar_test.rb", "test/test_helper.rb", "auto_ar.gemspec"]
15
+ s.homepage = %q{http://github.com/tmaeda/auto_ar}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Auto_ar"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{auto_ar}
19
+ s.rubygems_version = %q{1.3.5}
20
+ s.signing_key = %q{/home/tmaeda/.gemcert/gem-private_key.pem}
21
+ s.summary = %q{A gem that makes ActiveRecord subclasses on the fly}
22
+ s.test_files = ["test/auto_ar_test.rb", "test/test_helper.rb"]
23
+
24
+ if s.respond_to? :specification_version then
25
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
26
+ s.specification_version = 3
27
+
28
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
29
+ else
30
+ end
31
+ else
32
+ end
33
+ end
data/lib/auto_ar.rb ADDED
@@ -0,0 +1,84 @@
1
+ require 'active_support'
2
+ require 'active_record'
3
+
4
+ if defined?(ActiveRecord::Base)
5
+ module AutoAR
6
+ def self.included(base)
7
+ base.class_eval do
8
+ unless defined? const_missing_without_auto_ar
9
+ alias_method_chain :const_missing, :auto_ar
10
+ end
11
+ end
12
+ end
13
+ def const_missing_with_auto_ar(const_name)
14
+ begin
15
+ const_missing_without_auto_ar(const_name)
16
+ rescue NameError => e
17
+ begin
18
+ conn = ActiveRecord::Base.connection
19
+ if conn.tables.include?(const_name.to_s.tableize)
20
+ Object.const_set(const_name, Class.new(ActiveRecord::Base))
21
+ else
22
+ raise NameError
23
+ end
24
+ rescue ActiveRecord::ConnectionNotEstablished, ActiveRecord::StatementInvalid
25
+ raise NameError
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ module AutoARRefrection
32
+ def self.included(base)
33
+ base.class_eval do
34
+ unless defined? method_missing_without_auto_ar
35
+ alias_method_chain :method_missing, :auto_ar
36
+ end
37
+ end
38
+ end
39
+
40
+ def method_missing_with_auto_ar(method_sym, *args)
41
+ begin
42
+ method_missing_without_auto_ar(method_sym, *args)
43
+ rescue NoMethodError => e
44
+ method_name = method_sym.to_s
45
+ if method_name == method_name.singularize
46
+ f_key = method_name.singularize.foreign_key
47
+ if self.respond_to?(f_key)
48
+ target_klass = Object.const_get(method_name.classify)
49
+ # target_klass.class_eval("has_many :#{self.class.to_s.pluralize.underscore.intern}")
50
+ # self.class.class_eval("belongs_to :#{method_sym}")
51
+ foo(target_klass, self.class)
52
+ else
53
+ raise NoMethodError
54
+ end
55
+ elsif method_name == method_name.pluralize
56
+ table_name = method_name.tableize
57
+ f_key = self.class.to_s.foreign_key
58
+ conn = self.connection
59
+ if conn.tables.include?(table_name) &&
60
+ conn.columns(table_name).map(&:name).include?(f_key)
61
+ target_klass = Object.const_get(method_name.classify)
62
+ target_klass.class_eval("belongs_to :#{self.class.to_s.singularize.underscore.intern}")
63
+ self.class.class_eval("has_many :#{method_sym}")
64
+ else
65
+ raise NoMethodError
66
+ end
67
+ else
68
+ raise NoMethodError
69
+ end
70
+ self.send(method_sym, *args)
71
+ end
72
+ end
73
+
74
+ def foo(parent_class, child_class)
75
+ parent_class.class_eval("has_many :#{child_class.to_s.pluralize.underscore}")
76
+ child_class.class_eval("belongs_to :#{parent_class.to_s.singularize.underscore}")
77
+ end
78
+ end
79
+ Module.instance_eval{ include AutoAR }
80
+ ActiveRecord::Base.instance_eval{ include AutoARRefrection }
81
+ else
82
+ raise "Error: ActiveRecord required."
83
+ end
84
+
@@ -0,0 +1,71 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class AutArTest < Test::Unit::TestCase
4
+ DBFILE = "test/auto_ar_test.sqlite3"
5
+ def setup
6
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
7
+ :database => DBFILE)
8
+ migrate = Class.new(ActiveRecord::Migration) do
9
+ def self.up
10
+ create_table(:blogs) do |t|
11
+ t.string :title
12
+ t.text :body
13
+ end
14
+ create_table(:comments) do |t|
15
+ t.references :blog
16
+ t.string :name
17
+ t.string :body
18
+ end
19
+ end
20
+ end
21
+ migrate.up
22
+ conn = ActiveRecord::Base.connection
23
+ conn.execute("insert into blogs (id, title, body) values (1, 'first post', 'hello. how are you?')")
24
+ conn.execute("insert into comments (id, blog_id, name, body) values (1, 1, 'tmaeda', 'fine')")
25
+ conn.execute("insert into comments (id, blog_id, name, body) values (2, 1, 'tmaeda2', 'fine')")
26
+
27
+ conn.execute("insert into blogs (id, title, body) values (2, 'second post', 'good night')")
28
+ conn.execute("insert into comments (id, blog_id, name, body) values (3, 2, 'tmaeda', 'sweet dreams')")
29
+ conn.execute("insert into comments (id, blog_id, name, body) values (4, 2, 'tmaeda2', 'good night')")
30
+ conn.execute("insert into comments (id, blog_id, name, body) values (5, 2, 'tmaeda2', 'see you!')")
31
+
32
+ end
33
+ def teardown
34
+ File.unlink(DBFILE)
35
+ end
36
+
37
+ def test_find_parent
38
+ blogs = Blog.find(:all)
39
+ assert_equal 2, blogs.size
40
+ end
41
+
42
+ def test_find_children
43
+ comments = Comments.find(:all)
44
+ assert_equal 5, comments.size
45
+ end
46
+
47
+ def test_relation_parent_to_children
48
+ blog = Blog.find(1)
49
+ comments = blog.comments
50
+ assert_equal 2, comments.size
51
+ end
52
+
53
+ def test_relation_children_to_parent
54
+ comment = Comment.find(3)
55
+ blog = comment.blog
56
+ assert_equal 2, blog.id
57
+ end
58
+
59
+ def test_relation_parent_to_children_to_parent
60
+ blog = Blog.find(1)
61
+ blog = blog.comments[0].blog
62
+ assert_equal 1, blog.id
63
+ end
64
+
65
+ def test_relation_children_to_parent_to_children
66
+ comment = Comment.find(4)
67
+ comments = comment.blog.comments
68
+ assert_equal 3, comments.size
69
+ end
70
+
71
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/auto_ar'
data.tar.gz.sig ADDED
@@ -0,0 +1,4 @@
1
+ ּ�(���貰�{/��;²�$��%��O�� -i��*]Cw��D>�t/�7�O���vC��tזu������P�@�$hW��d:g\kowO��HӸ��A+djJ_�x�ٛ9M��7�?�`���r>�p^<�N�O �.��m�-c�a�
2
+ ��ι���|X�J��܃�{��w�$�n՚2�:,��7��Qh�5�?�:���[��%
3
+ Z~�������$v\P�
4
+ [���+��x@��O9��9L�q?��
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auto_ar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tomoki MAEDA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
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fFkNarOAvtMq1wL/0Tx+s2jS
28
+ QrvtICSUB0krXMKPsKuKKBbOoGrKfERr29H/cLAio3tsl44F9jvZxDggvMK/8fNY
29
+ v+PF/vWBUDUXcWrIoc3OnidxLXVV3DeYASrXlH4Tvyt10fNKyQzKflVBtTRNwiqK
30
+ 0kAGmV8I6si3R9QOfuLD4g==
31
+ -----END CERTIFICATE-----
32
+
33
+ date: 2010-06-20 00:00:00 +09:00
34
+ default_executable:
35
+ dependencies: []
36
+
37
+ description: A gem that makes ActiveRecord subclasses on the fly
38
+ email: tmaeda _at_ ruby-sapporo.org
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - lib/auto_ar.rb
45
+ files:
46
+ - Manifest
47
+ - Rakefile
48
+ - lib/auto_ar.rb
49
+ - test/auto_ar_test.rb
50
+ - test/test_helper.rb
51
+ - auto_ar.gemspec
52
+ has_rdoc: true
53
+ homepage: http://github.com/tmaeda/auto_ar
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --line-numbers
59
+ - --inline-source
60
+ - --title
61
+ - Auto_ar
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "1.2"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project: auto_ar
79
+ rubygems_version: 1.3.5
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: A gem that makes ActiveRecord subclasses on the fly
83
+ test_files:
84
+ - test/auto_ar_test.rb
85
+ - test/test_helper.rb
metadata.gz.sig ADDED
Binary file