forked from openHPI/xikolo-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestify_forwardable.rb
21 lines (20 loc) · 1017 Bytes
/
restify_forwardable.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# frozen_string_literal: true
module RestifyForwardable
# a `def_delegators` compatible method designed to be used on restify
# responses: the field are access via obj['field_name'] not obj.field_name.
# the references object can be a `Restify::Promise`, the promise will be
# resolved transparently on access.
def def_restify_delegators(dest, *fields)
fields.each do |field|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
# delegates to restify resource or promise:
# restify strict modus compatible
def #{field} # def name
value = #{dest} # value = @promise
value = value.value! if value.is_a?(Restify::Promise) # value = value.value! if value.is_a?(Restify::Promise)
value[#{field.to_s.inspect}] # value['name']
end # end
RUBY_EVAL
end
end
end