foreign_model 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{foreign_model}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Mike Nicholaides"]
12
- s.date = %q{2010-04-13}
12
+ s.date = %q{2010-07-29}
13
13
  s.description = %q{Works for ActiveRecord and Mongoid}
14
14
  s.email = %q{mike@ablegray.com}
15
15
  s.extra_rdoc_files = [
data/lib/foreign_model.rb CHANGED
@@ -12,9 +12,9 @@ module ForeignModel
12
12
 
13
13
  module ClassMethods
14
14
  def belongs_to_foreign_model(name, options={})
15
- options[:class_name] ||= ForeignModel.camelize(name.to_s)
15
+ options[:class_name] ||= ForeignModel.camelize(name)
16
16
 
17
- ForeignModel::SCOPE_PROCS[self][name.to_s] = begin
17
+ ForeignModel::SCOPE_PROCS[self][name] = begin
18
18
  if options[:scope]
19
19
  options[:scope]
20
20
  elsif options[:polymorphic]
@@ -27,43 +27,47 @@ module ForeignModel
27
27
  proc{ eval(options[:class_name]) }
28
28
  end
29
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 && #{name}_id != ""
30
+
31
+ define_method :_foreign_models do
32
+ @_foreign_models ||= {}
33
+ end
34
+
35
+ define_method :_parent_procs do
36
+ @_parent_procs ||= {}
37
+ end
38
+
39
+ define_method :parent_proc_for do |foreign_model_name|
40
+ _parent_procs[foreign_model_name] ||= ForeignModel::SCOPE_PROCS[self.class][foreign_model_name].call(self)
41
+ end
42
+
43
+ define_method name do
44
+ if parent_proc_for(name) && send("#{name}_id") && send("#{name}_id") != ""
45
+ _foreign_models[name] ||= parent_proc_for(name).find(send("#{name}_id"))
40
46
  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
47
+ end
48
+
49
+ define_method "#{name}=" do |foreign_model|
50
+ _foreign_models[name] = foreign_model
51
+ if foreign_model
52
+ send("#{name}_id=", foreign_model.id)
53
+ send("#{name}_type=", foreign_model.class.name) if respond_to? "#{name}_type="
48
54
  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
+ define_method "#{name}_id=" do |foreign_model_id|
58
+ if send("#{name}_id") != foreign_model_id
59
+ write_raw_attribute("#{name}_id", foreign_model_id)
60
+ _foreign_models[name] = nil
55
61
  end
56
- `
62
+ end
57
63
 
58
64
  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
+ define_method "#{name}_type=" do |foreign_model_type|
66
+ if send("#{name}_type") != foreign_model_type
67
+ write_raw_attribute("#{name}_type", foreign_model_type)
68
+ _foreign_models[name] = nil
65
69
  end
66
- `
70
+ end
67
71
  end
68
72
  end
69
73
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Mike Nicholaides
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-13 00:00:00 +00:00
17
+ date: 2010-07-29 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency