classproxy 0.7.1 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +1 -1
- data/Rakefile +2 -2
- data/lib/classproxy/classproxy.rb +44 -24
- data/lib/classproxy/version.rb +1 -1
- metadata +2 -2
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 { |obj|
|
49
|
+
after_fallback_fetch { |model, obj| model.username = obj.login }
|
50
50
|
|
51
51
|
key :name, String
|
52
52
|
key :reverse_name, String
|
data/Rakefile
CHANGED
@@ -26,8 +26,8 @@ task release: :build do
|
|
26
26
|
version = ClassProxy::VERSION
|
27
27
|
system "git tag -a v#{version} -m 'Tagging #{version}'"
|
28
28
|
system "git push --tags"
|
29
|
-
system "gem push classproxy-#{version}"
|
30
|
-
system "rm classproxy-#{version}"
|
29
|
+
system "gem push classproxy-#{version}.gem"
|
30
|
+
system "rm classproxy-#{version}.gem"
|
31
31
|
end
|
32
32
|
|
33
33
|
task default: :spec
|
@@ -32,9 +32,10 @@ module ClassProxy
|
|
32
32
|
# include ClassProxy
|
33
33
|
#
|
34
34
|
# fallback_fetch { |args| Octokit.user(args[:login]) }
|
35
|
-
# after_fallback_fetch do |obj|
|
35
|
+
# after_fallback_fetch do |model, obj|
|
36
36
|
# # obj is what `fallback_fetch` returns
|
37
|
-
#
|
37
|
+
# model.name = obj.name
|
38
|
+
# model.login = obj.login
|
38
39
|
# end
|
39
40
|
#
|
40
41
|
# attr_accessor :name, :login
|
@@ -49,11 +50,11 @@ module ClassProxy
|
|
49
50
|
# include ClassProxy
|
50
51
|
#
|
51
52
|
# fallback_fetch { |args| Octokit.user(args[:login]) }
|
52
|
-
# after_fallback_fetch { |obj|
|
53
|
+
# after_fallback_fetch { |model, obj| model.name = obj.name; model.login = obj.login }
|
53
54
|
#
|
54
|
-
# attr_accessor :name, :login
|
55
|
+
# attr_accessor :name, :followers :login
|
55
56
|
#
|
56
|
-
# proxy_methods uppercase_login: lambda { login.upcase }
|
57
|
+
# proxy_methods :name, :followers, uppercase_login: lambda { login.upcase }
|
57
58
|
# end
|
58
59
|
#
|
59
60
|
# user = GithubUser.find(login: 'heelhook')
|
@@ -65,10 +66,9 @@ module ClassProxy
|
|
65
66
|
methods.each do |method|
|
66
67
|
if method.is_a? Symbol
|
67
68
|
# If given a symbol, store as a method to overwrite and use the default loader
|
68
|
-
proxy_method method
|
69
|
+
proxy_method method
|
69
70
|
elsif method.is_a? Hash
|
70
|
-
# If its a hash it will include methods to overwrite along with
|
71
|
-
# custom loaders
|
71
|
+
# If its a hash it will include methods to overwrite along with custom loaders
|
72
72
|
method.each { |method_name, proc| proxy_method method_name, proc }
|
73
73
|
end
|
74
74
|
end
|
@@ -91,19 +91,26 @@ module ClassProxy
|
|
91
91
|
# @param [ Hash ] args The criteria to use
|
92
92
|
# @options options [ true, false] :skip_fallback Don't use fallback methods
|
93
93
|
def fetch(args, options={})
|
94
|
-
@primary_fetch.is_a?(Proc) ? @primary_fetch
|
94
|
+
@primary_fetch.is_a?(Proc) ? @primary_fetch[args] : (raise NotFound)
|
95
95
|
rescue NotFound
|
96
96
|
return nil if options[:skip_fallback]
|
97
97
|
|
98
|
-
|
98
|
+
run_fallback(args)
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def run_fallback(args, _self=nil)
|
104
|
+
fallback_obj = @fallback_fetch[args]
|
99
105
|
|
100
106
|
# Use the after_fallback_method
|
101
|
-
obj =
|
107
|
+
obj = _self || self.new
|
108
|
+
@after_fallback_method[obj, fallback_obj] if @after_fallback_method.is_a?(Proc)
|
102
109
|
|
103
110
|
# Go through the keys of the return object and try to use setters
|
104
111
|
if fallback_obj and obj and fallback_obj.respond_to? :keys and fallback_obj.keys.respond_to? :each
|
105
112
|
fallback_obj.keys.each do |key|
|
106
|
-
next unless obj.respond_to? "#{key}="
|
113
|
+
next unless obj.respond_to? "#{key}=" and obj.send(key) == nil
|
107
114
|
obj.send("#{key}=", fallback_obj.send(key))
|
108
115
|
end
|
109
116
|
end
|
@@ -111,9 +118,7 @@ module ClassProxy
|
|
111
118
|
return obj
|
112
119
|
end
|
113
120
|
|
114
|
-
|
115
|
-
|
116
|
-
def proxy_method(method_name, proc)
|
121
|
+
def proxy_method(method_name, proc=nil)
|
117
122
|
self.class_eval do
|
118
123
|
alias_method "no_proxy_#{method_name}".to_sym, method_name
|
119
124
|
|
@@ -131,13 +136,31 @@ module ClassProxy
|
|
131
136
|
if v == nil and @mutex_in_call_for != method_name
|
132
137
|
@mutex_in_call_for = method_name
|
133
138
|
method = "_run_fallback_#{method_name}".to_sym
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
139
|
+
if self.respond_to?(method)
|
140
|
+
v = if self.method(method).arity == 1
|
141
|
+
# Callback method is expecting to receive the fallback object
|
142
|
+
fallback_fetch_method = self.class.instance_variable_get(:@fallback_fetch)
|
143
|
+
fallback_obj = fallback_fetch_method[self]
|
144
|
+
self.send(method, fallback_obj)
|
145
|
+
else
|
146
|
+
# Callback method doesn't need the fallback object
|
147
|
+
self.send(method)
|
148
|
+
end
|
138
149
|
else
|
139
|
-
|
150
|
+
# 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
|
158
|
+
|
159
|
+
# The value might have changed, so check here
|
160
|
+
v = self.send("no_proxy_#{method_name}".to_sym)
|
140
161
|
end
|
162
|
+
|
163
|
+
# Set the defaults when this class responds to the same method
|
141
164
|
self.send("#{method_name}=".to_sym, v) if v and self.respond_to?("#{method_name}=")
|
142
165
|
@mutex_in_call_for = nil
|
143
166
|
end
|
@@ -147,14 +170,11 @@ module ClassProxy
|
|
147
170
|
end
|
148
171
|
|
149
172
|
# Now define the fallback that is going to be used
|
150
|
-
self.send(:define_method, "_run_fallback_#{method_name}", &proc)
|
173
|
+
self.send(:define_method, "_run_fallback_#{method_name}", &proc) if proc.is_a? Proc
|
151
174
|
end
|
152
175
|
end
|
153
176
|
|
154
|
-
@default_proc = Proc.new { "hi" }
|
155
|
-
|
156
177
|
def self.included(receiver)
|
157
178
|
receiver.extend ClassMethods
|
158
|
-
|
159
179
|
end
|
160
180
|
end
|
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.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -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: -1419408935821382140
|
59
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
60
|
none: false
|
61
61
|
requirements:
|