-
Notifications
You must be signed in to change notification settings - Fork 13
/
defaults.rb
46 lines (42 loc) · 1.24 KB
/
defaults.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module RestCore
class Defaults
def self.members; [:defaults]; end
include Middleware
# the use of singleton_class is making serialization hard!
# def initialize app, defaults
# super
# singleton_class.module_eval do
# defaults.each{ |(key, value)|
# define_method(key) do |env|
# if value.respond_to?(:call)
# value.call
# else
# value
# end
# end
# }
# end
# end
def method_missing msg, *args, &block
env = args.first
if env.kind_of?(Hash) && (d = defaults(env)) && d.key?(msg)
defaults(env)[msg]
else
super
end
end
def respond_to_missing? msg, include_private=false
# since psych would call respond_to? before setting up
# instance variables when restoring ruby objects, we might
# be accessing undefined ivars in that case even all ivars are
# defined in initialize. we can't avoid this because we can't
# use singleton_class (otherwise we can't serialize this)
return super unless instance_variable_defined?(:@defaults)
if (d = defaults({})) && d.key?(msg)
true
else
super
end
end
end
end