foreign_model 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ doc
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Mike Nicholaides
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = foreign_model
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Mike Nicholaides. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "foreign_model"
8
+ gem.summary = %Q{ActiveRecord-like associations that work between models of different databases and database types.}
9
+ gem.description = %Q{Works for ActiveRecord and Mongoid}
10
+ gem.email = "mike@ablegray.com"
11
+ gem.homepage = "http://github.com/nicholaides/foreign_model"
12
+ gem.authors = ["Mike Nicholaides"]
13
+ gem.add_development_dependency "spec", ">= 1.3.0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/rdoctask'
22
+ Rake::RDocTask.new do |rdoc|
23
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
24
+
25
+ rdoc.rdoc_dir = 'rdoc'
26
+ rdoc.title = "foreign_model #{version}"
27
+ rdoc.rdoc_files.include('README*')
28
+ rdoc.rdoc_files.include('lib/**/*.rb')
29
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,52 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{foreign_model}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Mike Nicholaides"]
12
+ s.date = %q{2010-04-02}
13
+ s.description = %q{Works for ActiveRecord and Mongoid}
14
+ s.email = %q{mike@ablegray.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "foreign_model.gemspec",
27
+ "lib/foreign_model.rb",
28
+ "spec/foreign_model_spec.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/nicholaides/foreign_model}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.6}
34
+ s.summary = %q{ActiveRecord-like associations that work between models of different databases and database types.}
35
+ s.test_files = [
36
+ "spec/foreign_model_spec.rb"
37
+ ]
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
+ s.add_development_dependency(%q<spec>, [">= 1.3.0"])
45
+ else
46
+ s.add_dependency(%q<spec>, [">= 1.3.0"])
47
+ end
48
+ else
49
+ s.add_dependency(%q<spec>, [">= 1.3.0"])
50
+ end
51
+ end
52
+
@@ -0,0 +1,92 @@
1
+ module ForeignModel
2
+ SCOPE_PROCS = {}
3
+
4
+ def self.included(base)
5
+ SCOPE_PROCS[base] ||= {}
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ def self.camelize(str)
10
+ str.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
11
+ end
12
+
13
+ module ClassMethods
14
+ def belongs_to_foreign_model(name, options={})
15
+ options[:class_name] ||= ForeignModel.camelize(name.to_s)
16
+
17
+ ForeignModel::SCOPE_PROCS[self][name.to_s] = begin
18
+ if options[:scope]
19
+ options[:scope]
20
+ elsif options[:polymorphic]
21
+ proc do |r|
22
+ type = r.send("#{name}_type")
23
+ type and eval(type)
24
+ end
25
+ else
26
+ #TODO: this is a hack
27
+ proc{ eval(options[:class_name]) }
28
+ end
29
+ end
30
+
31
+ class_eval %`
32
+ def parent_proc_for_#{name}
33
+ @parent_proc_for_#{name} ||= begin
34
+ ForeignModel::SCOPE_PROCS[self.class]["#{name}"].call(self)
35
+ end
36
+ end
37
+
38
+ def #{name}
39
+ @#{name} ||= parent_proc_for_#{name}.find(#{name}_id) if parent_proc_for_#{name} && #{name}_id
40
+ end
41
+
42
+ def #{name}=(foreign_model)
43
+ @#{name} = foreign_model
44
+ if foreign_model
45
+ send('#{name}_id=', foreign_model.id)
46
+ send('#{name}_type=', foreign_model.class.name) if respond_to? '#{name}_type='
47
+ end
48
+ end
49
+
50
+ def #{name}_id=(foreign_model_id)
51
+ if #{name}_id != foreign_model_id
52
+ write_raw_attribute("#{name}_id", foreign_model_id)
53
+ @#{name} = nil
54
+ end
55
+ end
56
+ `
57
+
58
+ if options[:polymorphic]
59
+ class_eval %`
60
+ def #{name}_type=(foreign_model_type)
61
+ if #{name}_type != foreign_model_type
62
+ write_raw_attribute("#{name}_type", foreign_model_type)
63
+ @#{name} = nil
64
+ end
65
+ end
66
+ `
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ class Object
73
+ def write_raw_attribute(name, value)
74
+ instance_variable_set "@#{name}".to_sym, value
75
+ end
76
+ end
77
+
78
+ module ActiveRecord
79
+ class Base
80
+ def write_raw_attribute(name, value)
81
+ write_attribute name.to_sym, value
82
+ end
83
+ end
84
+ end
85
+
86
+ module Mongoid
87
+ module Document
88
+ def write_raw_attribute(name, value)
89
+ raw_attributes[name.to_s] = value
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,210 @@
1
+ require "spec"
2
+ require File.dirname(__FILE__) + '/../lib/foreign_model'
3
+
4
+ describe "belongs_to_foreign_model" do
5
+ class Parent
6
+ attr_accessor :id
7
+ def initialize(id="some_id")
8
+ @id = id
9
+ end
10
+
11
+ def self.find(id)
12
+ new(id)
13
+ end
14
+
15
+ def ==(other)
16
+ self.class == other.class && self.id == other.id
17
+ end
18
+ end
19
+
20
+ class Child
21
+ attr_reader :parent_id
22
+ include ForeignModel
23
+ belongs_to_foreign_model :parent
24
+ end
25
+
26
+ describe Child do
27
+ describe "#parent=" do
28
+ before :each do
29
+ @parent = Parent.new
30
+ @child = Child.new
31
+ end
32
+ it "should accept nil" do
33
+ @child.parent = nil
34
+ @child.parent.should be_nil
35
+ end
36
+ it "should set parent" do
37
+ @child.parent = @parent
38
+ @child.parent.should == @parent
39
+ end
40
+ it "should set parent id" do
41
+ @child.parent = @parent
42
+ @child.parent_id.should == @parent.id
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "#parent_type_id=" do
48
+ before :each do
49
+ @parent = Parent.new
50
+ @child = Child.new
51
+ @child.parent_id = @parent.id
52
+ end
53
+ it "should set the parent" do
54
+ @child.parent.should == @parent
55
+ end
56
+ it "should set the parent_id" do
57
+ @child.parent_id.should == @parent.id
58
+ end
59
+
60
+ context "when nil" do
61
+ it "should the assiated model should be nil" do
62
+ @child = Child.new
63
+ @child.parent_id = nil
64
+
65
+ @child.parent.should be_nil
66
+ end
67
+ end
68
+ end
69
+
70
+ describe "with :scope => proc{...}" do
71
+ class ChildWithScope
72
+ attr_reader :parent_id
73
+ include ForeignModel
74
+ belongs_to_foreign_model :parent, :scope => proc{ Scope }
75
+ end
76
+
77
+ class Scope
78
+ end
79
+
80
+ it "should find by scope" do
81
+ @child = ChildWithScope.new
82
+ @child.parent_id = "another id"
83
+
84
+ @parent = mock("parent found by scope")
85
+ Scope.should_receive(:find).with("another id").and_return(@parent)
86
+
87
+ @child.parent.should == @parent
88
+ end
89
+
90
+ context "when parent_id is nil" do
91
+ it "should give nil as the assiated record" do
92
+ @child = ChildWithScope.new
93
+ @child.parent_id = nil
94
+ @child.parent.should be_nil
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "with :class_name option" do
100
+ class ChildWithClassName
101
+ attr_reader :parent_id
102
+ include ForeignModel
103
+ belongs_to_foreign_model :parent, :class_name => "SomeModule::OtherParent"
104
+ end
105
+
106
+ module SomeModule
107
+ class OtherParent < ::Parent
108
+ def self.find(id)
109
+ new(id)
110
+ end
111
+ end
112
+ end
113
+
114
+ it "should find by scope" do
115
+ @child = ChildWithClassName.new
116
+ @child.parent_id = "another id"
117
+
118
+ @child.parent.id.should == "another id"
119
+ @child.parent.should be_a(SomeModule::OtherParent)
120
+ end
121
+
122
+ context "when parent_id is nil" do
123
+ it "should give nil as the assiated record" do
124
+ @child = ChildWithClassName.new
125
+ @child.parent_id = nil
126
+ @child.parent.should be_nil
127
+ end
128
+ end
129
+ end
130
+
131
+ describe "with :polymorphic => true" do
132
+ class FosterParent
133
+ attr_accessor :id
134
+ def initialize(id="some_id")
135
+ @id = id
136
+ end
137
+
138
+ def self.find(id)
139
+ new(id)
140
+ end
141
+
142
+ def ==(other)
143
+ self.class == other.class && self.id == other.id
144
+ end
145
+ end
146
+
147
+ class ChildWithPolymorphicParents
148
+ attr_reader :parent_id
149
+ attr_reader :parent_type
150
+ include ForeignModel
151
+ belongs_to_foreign_model :parent, :polymorphic => true
152
+ end
153
+
154
+ before :each do
155
+ @parent = Parent.new
156
+ @foster_parent = FosterParent.new
157
+ @child = ChildWithPolymorphicParents.new
158
+ end
159
+
160
+ it "should set the parent to a Parent" do
161
+ @child.parent = @parent
162
+
163
+ @child.parent.should == @parent
164
+ @child.parent_id.should == @parent.id
165
+ @child.parent_type.should == "Parent"
166
+ end
167
+
168
+ it "should set the parent to a FosterParent" do
169
+ @child.parent = @foster_parent
170
+
171
+ @child.parent.should == @foster_parent
172
+ @child.parent_id.should == @foster_parent.id
173
+ @child.parent_type.should == "FosterParent"
174
+ end
175
+
176
+ it "should set the parent by id and type" do
177
+ @child.parent_id = @foster_parent.id
178
+ @child.parent_type = "FosterParent"
179
+
180
+ @child.parent.should == @foster_parent
181
+ end
182
+
183
+ context "when parent_id is nil" do
184
+ it "should give nil as the assiated record" do
185
+ @child = ChildWithPolymorphicParents.new
186
+ @child.parent_id = nil
187
+ @child.parent_type = "FosterParent"
188
+ @child.parent.should be_nil
189
+ end
190
+ end
191
+
192
+ context "when parent_type is nil" do
193
+ it "should give nil as the assiated record" do
194
+ @child = ChildWithPolymorphicParents.new
195
+ @child.parent_id = "2"
196
+ @child.parent_type = nil
197
+ @child.parent.should be_nil
198
+ end
199
+ end
200
+
201
+ context "when parent_id and parent_type are nil" do
202
+ it "should give nil as the assiated record" do
203
+ @child = ChildWithPolymorphicParents.new
204
+ @child.parent_id = nil
205
+ @child.parent_type = nil
206
+ @child.parent.should be_nil
207
+ end
208
+ end
209
+ end
210
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foreign_model
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Mike Nicholaides
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-02 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: spec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 3
30
+ - 0
31
+ version: 1.3.0
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Works for ActiveRecord and Mongoid
35
+ email: mike@ablegray.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - foreign_model.gemspec
51
+ - lib/foreign_model.rb
52
+ - spec/foreign_model_spec.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/nicholaides/foreign_model
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --charset=UTF-8
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.6
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: ActiveRecord-like associations that work between models of different databases and database types.
83
+ test_files:
84
+ - spec/foreign_model_spec.rb