classproxy 0.7.4 → 0.7.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -46,7 +46,7 @@ class UserDb
46
46
 
47
47
  primary_fetch { |args| where(args).first or (raise NotFound) }
48
48
  fallback_fetch { |args| Octokit.user(args[:username]) }
49
- after_fallback_fetch { |model, obj| model.username = obj.login }
49
+ after_fallback_fetch { |obj| self.username = obj.login }
50
50
 
51
51
  key :name, String
52
52
  key :reverse_name, String
@@ -32,10 +32,10 @@ module ClassProxy
32
32
  # include ClassProxy
33
33
  #
34
34
  # fallback_fetch { |args| Octokit.user(args[:login]) }
35
- # after_fallback_fetch do |model, obj|
35
+ # after_fallback_fetch do |obj|
36
36
  # # obj is what `fallback_fetch` returns
37
- # model.name = obj.name
38
- # model.login = obj.login
37
+ # self.name = obj.name
38
+ # self.login = obj.login
39
39
  # end
40
40
  #
41
41
  # attr_accessor :name, :login
@@ -50,7 +50,7 @@ module ClassProxy
50
50
  # include ClassProxy
51
51
  #
52
52
  # fallback_fetch { |args| Octokit.user(args[:login]) }
53
- # after_fallback_fetch { |model, obj| model.name = obj.name; model.login = obj.login }
53
+ # after_fallback_fetch { |obj| self.name = obj.name; self.login = obj.login }
54
54
  #
55
55
  # attr_accessor :name, :followers :login
56
56
  #
@@ -101,17 +101,23 @@ module ClassProxy
101
101
  private
102
102
 
103
103
  def run_fallback(args, _self=nil)
104
- fallback_obj = @fallback_fetch[args]
104
+ obj = _self || self.new
105
+
106
+ fallback_obj = obj.instance_exec args, &@fallback_fetch
105
107
 
106
108
  # Use the after_fallback_method
107
- obj = _self || self.new
108
- @after_fallback_method[obj, fallback_obj] if @after_fallback_method.is_a?(Proc)
109
+ obj.instance_exec fallback_obj, &@after_fallback_method if @after_fallback_method.is_a? Proc
109
110
 
110
111
  # Go through the keys of the return object and try to use setters
111
112
  if fallback_obj and obj and fallback_obj.respond_to? :keys and fallback_obj.keys.respond_to? :each
112
113
  fallback_obj.keys.each do |key|
113
- next unless obj.respond_to? "#{key}=" and obj.send(key) == nil
114
- obj.send("#{key}=", fallback_obj.send(key))
114
+ next unless obj.respond_to? "#{key}="
115
+
116
+ # check if its set to something else
117
+ get_method = obj.respond_to?("no_proxy_#{key}") ? "no_proxy_#{key}" : key
118
+ if obj.respond_to? get_method and obj.send(get_method) == nil
119
+ obj.send("#{key}=", fallback_obj.send(key))
120
+ end
115
121
  end
116
122
  end
117
123
 
@@ -120,7 +126,9 @@ module ClassProxy
120
126
 
121
127
  def proxy_method(method_name, proc=nil)
122
128
  self.class_eval do
123
- alias_method "no_proxy_#{method_name}".to_sym, method_name
129
+ unless self.instance_methods.include? "no_proxy_#{method_name}".to_sym
130
+ alias_method "no_proxy_#{method_name}".to_sym, method_name
131
+ end
124
132
 
125
133
  define_method(method_name) do |*args|
126
134
  # Use the no_proxy one first
@@ -133,9 +141,11 @@ module ClassProxy
133
141
  # to establish the dirty attribute, since the getter is being replaced
134
142
  # here and the setter is being used when appropriate, the @mutex_in_call_for
135
143
  # prevents endless recursion.
136
- if v == nil and @mutex_in_call_for != method_name
137
- @mutex_in_call_for = method_name
144
+ @mutex_in_call_for ||= []
145
+ if v == nil and not @mutex_in_call_for.include? method_name
146
+ @mutex_in_call_for << method_name
138
147
  method = "_run_fallback_#{method_name}".to_sym
148
+
139
149
  if self.respond_to?(method)
140
150
  v = if self.method(method).arity == 1
141
151
  # Callback method is expecting to receive the fallback object
@@ -148,13 +158,8 @@ module ClassProxy
148
158
  end
149
159
  else
150
160
  # This method has no callback, so just run the fallback
151
- args = {}
152
- (self.methods - self.class.methods).each do |key|
153
- next if key[-1] == '=' # don't run for set
154
- hkey = key.to_s.gsub(/^no_proxy_/, '')
155
- args[hkey.to_sym] = self.send(key)
156
- end
157
- self.class.send :run_fallback, args, self
161
+ args_class = ArgsClass.new(self)
162
+ self.class.send :run_fallback, args_class, self
158
163
 
159
164
  # The value might have changed, so check here
160
165
  v = self.send("no_proxy_#{method_name}".to_sym)
@@ -162,7 +167,7 @@ module ClassProxy
162
167
 
163
168
  # Set the defaults when this class responds to the same method
164
169
  self.send("#{method_name}=".to_sym, v) if v and self.respond_to?("#{method_name}=")
165
- @mutex_in_call_for = nil
170
+ @mutex_in_call_for.delete method_name
166
171
  end
167
172
 
168
173
  return v
@@ -177,4 +182,25 @@ module ClassProxy
177
182
  def self.included(receiver)
178
183
  receiver.extend ClassMethods
179
184
  end
180
- end
185
+
186
+ # This class makes methods accessible as a hash key, useful to pass
187
+ # as a fallback_fetch argument
188
+ class ArgsClass < BasicObject
189
+ def initialize(object)
190
+ @target = object
191
+ end
192
+
193
+ def [](key)
194
+ @target.respond_to?(key) ? @target.send(key) : @target[key]
195
+ end
196
+
197
+ def inspect
198
+ "ArgsClass [#{@target.inspect}] " +
199
+ (@target.methods - @target.class.methods).join(', ')
200
+ end
201
+
202
+ def target
203
+ @target
204
+ end
205
+ end
206
+ end
@@ -1,3 +1,3 @@
1
1
  module ClassProxy
2
- VERSION = '0.7.4'
2
+ VERSION = '0.7.9'
3
3
  end
data/lib/classproxy.rb CHANGED
@@ -1,2 +1,3 @@
1
+ require 'active_support'
1
2
  require 'classproxy/classproxy'
2
3
  require 'classproxy/version'
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
4
+ version: 0.7.9
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-16 00:00:00.000000000 Z
12
+ date: 2012-12-17 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: -1419408935821382140
58
+ hash: 3154749862381023825
59
59
  required_rubygems_version: !ruby/object:Gem::Requirement
60
60
  none: false
61
61
  requirements: