-
Notifications
You must be signed in to change notification settings - Fork 356
/
messages.rb
67 lines (58 loc) · 1.88 KB
/
messages.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright (c) 2009-2012 VMware, Inc.
module VCAP::CloudController::RestController
# Auto generation of Message classes based on the attributes
# exposed by a rest endpoint.
module Messages
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
# Define the messages exposed by a rest endpoint.
def define_messages
[:response, :create, :update].each do |type|
define_message(type)
end
end
private
def define_message(type)
attrs = attributes
to_one = @to_one_relationships ||= []
to_many = @to_many_relationships ||= []
klass = Class.new VCAP::RestAPI::Message do
attrs.each do |name, attr|
unless attr.exclude_in?(type)
if (type == :update || (type == :create && attr.default))
optional name, attr.schema
else
required name, attr.schema
end
end
end
to_one.each do |name, relation|
unless relation.exclude_in?(type)
if (type == :update || (type == :create &&
relation.optional_in?(type)))
optional "#{name}_guid", String
else
required "#{name}_guid", String
end
if type == :response
optional "#{name}_url", VCAP::RestAPI::Message::HTTPS_URL
end
end
end
to_many.each do |name, relation|
unless relation.exclude_in?(type)
if type == :response
optional "#{name}_url", VCAP::RestAPI::Message::HTTPS_URL
else
optional "#{name}_guid", [String]
end
end
end
end
self.const_set "#{type.to_s.camelize}Message", klass
end
end
end
end