classproxy 0.7.9 → 0.7.10
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/classproxy/classproxy.rb +39 -33
- data/lib/classproxy/version.rb +1 -1
- metadata +3 -3
@@ -101,27 +101,31 @@ module ClassProxy
|
|
101
101
|
private
|
102
102
|
|
103
103
|
def run_fallback(args, _self=nil)
|
104
|
-
|
104
|
+
_self ||= self.new
|
105
105
|
|
106
|
-
|
106
|
+
_self.instance_eval "@proxied_with_nil ||= []"
|
107
|
+
|
108
|
+
fallback_obj = _self.instance_exec args, &@fallback_fetch
|
107
109
|
|
108
110
|
# Use the after_fallback_method
|
109
|
-
|
111
|
+
_self.instance_exec fallback_obj, &@after_fallback_method if @after_fallback_method.is_a? Proc
|
110
112
|
|
111
113
|
# Go through the keys of the return object and try to use setters
|
112
|
-
if fallback_obj and
|
114
|
+
if fallback_obj and fallback_obj.respond_to? :keys and fallback_obj.keys.respond_to? :each
|
113
115
|
fallback_obj.keys.each do |key|
|
114
|
-
next unless
|
116
|
+
next unless _self.respond_to? "#{key}="
|
115
117
|
|
116
118
|
# check if its set to something else
|
117
|
-
get_method =
|
118
|
-
if
|
119
|
-
|
119
|
+
get_method = _self.respond_to?("no_proxy_#{key}") ? "no_proxy_#{key}" : key
|
120
|
+
if _self.respond_to? get_method and _self.send(get_method) == nil
|
121
|
+
value = fallback_obj.send(key)
|
122
|
+
_self.send("#{key}=", value)
|
123
|
+
_self.instance_eval "@proxied_with_nil << key" if value == nil
|
120
124
|
end
|
121
125
|
end
|
122
126
|
end
|
123
127
|
|
124
|
-
return
|
128
|
+
return _self
|
125
129
|
end
|
126
130
|
|
127
131
|
def proxy_method(method_name, proc=nil)
|
@@ -134,40 +138,42 @@ module ClassProxy
|
|
134
138
|
# Use the no_proxy one first
|
135
139
|
v = self.send("no_proxy_#{method_name}".to_sym, *args)
|
136
140
|
|
137
|
-
# TODO -- Cache if this also returned nil so the fallback is not used
|
138
|
-
# constantly on actual nil values
|
139
|
-
|
140
141
|
# Since AR calls the getter method when using the setter method
|
141
142
|
# to establish the dirty attribute, since the getter is being replaced
|
142
143
|
# here and the setter is being used when appropriate, the @mutex_in_call_for
|
143
144
|
# prevents endless recursion.
|
144
145
|
@mutex_in_call_for ||= []
|
146
|
+
@proxied_with_nil ||= []
|
145
147
|
if v == nil and not @mutex_in_call_for.include? method_name
|
146
|
-
@
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
#
|
152
|
-
|
153
|
-
|
154
|
-
|
148
|
+
unless @proxied_with_nil.include? method_name
|
149
|
+
@mutex_in_call_for << method_name
|
150
|
+
method = "_run_fallback_#{method_name}".to_sym
|
151
|
+
|
152
|
+
if self.respond_to?(method)
|
153
|
+
# arity == 1 means that the callback is expecting the fallback object
|
154
|
+
v = if self.method(method).arity == 1
|
155
|
+
# Callback method is expecting to receive the fallback object
|
156
|
+
fallback_fetch_method = self.class.instance_variable_get(:@fallback_fetch)
|
157
|
+
fallback_obj = fallback_fetch_method[self]
|
158
|
+
self.send(method, fallback_obj)
|
159
|
+
else
|
160
|
+
# Callback method doesn't need the fallback object
|
161
|
+
self.send(method)
|
162
|
+
end
|
155
163
|
else
|
156
|
-
#
|
157
|
-
|
164
|
+
# This method has no callback, so just run the fallback
|
165
|
+
args_class = ArgsClass.new(self)
|
166
|
+
self.class.send :run_fallback, args_class, self
|
167
|
+
|
168
|
+
# The value might have changed, so check here
|
169
|
+
v = self.send("no_proxy_#{method_name}".to_sym)
|
158
170
|
end
|
159
|
-
else
|
160
|
-
# This method has no callback, so just run the fallback
|
161
|
-
args_class = ArgsClass.new(self)
|
162
|
-
self.class.send :run_fallback, args_class, self
|
163
171
|
|
164
|
-
#
|
165
|
-
|
172
|
+
# Set the defaults when this class responds to the same method
|
173
|
+
self.send("#{method_name}=".to_sym, v) if v and self.respond_to?("#{method_name}=")
|
174
|
+
@mutex_in_call_for.delete method_name
|
175
|
+
@proxied_with_nil << method_name if v == nil
|
166
176
|
end
|
167
|
-
|
168
|
-
# Set the defaults when this class responds to the same method
|
169
|
-
self.send("#{method_name}=".to_sym, v) if v and self.respond_to?("#{method_name}=")
|
170
|
-
@mutex_in_call_for.delete method_name
|
171
177
|
end
|
172
178
|
|
173
179
|
return v
|
data/lib/classproxy/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: classproxy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.10
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -55,7 +55,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
55
|
version: '0'
|
56
56
|
segments:
|
57
57
|
- 0
|
58
|
-
hash:
|
58
|
+
hash: 1492446742885892984
|
59
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
60
|
none: false
|
61
61
|
requirements:
|