Skip to content

Commit 737ac9f

Browse files
committed
Optional parameters implementation
Added implementation to obtain the parameters optional value * Added optional value accessors * Added optional parameter in documentation
1 parent 638509e commit 737ac9f

7 files changed

+103
-76
lines changed

lib/angus/definitions/representation_field.rb

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module Angus
22
module SDoc
33
class Definitions::RepresentationField
4-
54
# @!attribute [rw] name
65
# @return [String] the name of the representation field.
76
attr_accessor :name
@@ -24,15 +23,21 @@ class Definitions::RepresentationField
2423
# This attribute is used when the representation is a list of objects.
2524
attr_accessor :elements_type
2625

26+
# @!attribute [rw] optional
27+
# @return [Boolean] true if the element is optional
28+
attr_accessor :optional
29+
2730
# @note if the elements_type attribute is different from nil, it means that the
2831
# representation field is a list of objects. In that case, the type parameter
2932
# is not considered.
30-
def initialize(name = nil, description = nil, required = nil, type = nil, elements_type = nil)
31-
self.name= name
32-
self.description= description
33-
self.required= required
34-
self.type= type
35-
self.elements_type= elements_type
33+
def initialize(name = nil, description = nil, required = nil, type = nil, elements_type = nil,
34+
optional = nil)
35+
self.name = name
36+
self.description = description
37+
self.required = required
38+
self.type = type
39+
self.elements_type = elements_type
40+
self.optional = optional
3641
end
3742

3843
# Check if an object is equals to the current instance.
@@ -42,10 +47,10 @@ def initialize(name = nil, description = nil, required = nil, type = nil, elemen
4247
# @return [Boolean] true if all the attributes are equal and false otherwise.
4348
def ==(other)
4449
other.kind_of?(Definitions::RepresentationField) &&
45-
self.name == other.name && self.description == other.description &&
46-
self.required == other.required && self.type == other.type && self.elements_type == other.elements_type
50+
name == other.name && description == other.description &&
51+
required == other.required && type == other.type &&
52+
elements_type == other.elements_type && optional == other.optional
4753
end
48-
4954
end
5055
end
5156
end

lib/angus/definitions/request_element.rb

+18-14
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,21 @@ class Definitions::RequestElement
3030
# @return [String] the elements type of the request element.
3131
attr_accessor :elements_type
3232

33+
# @!attribute [rw] optional
34+
# @return [Boolean] true if the element is optional
35+
attr_accessor :optional
36+
3337

3438
def initialize(name = nil, description = nil, required = nil, type = nil,
35-
constraints = nil, valid_values = nil, elements_type = nil)
36-
self.name= name if name
37-
self.description= description if description
38-
self.required= required if required
39-
self.type= type if type
40-
self.constraints= constraints if constraints
41-
self.valid_values= valid_values if valid_values
42-
self.elements_type= elements_type if elements_type
39+
constraints = nil, valid_values = nil, elements_type = nil, optional = nil)
40+
self.name = name if name
41+
self.description = description if description
42+
self.required = required if required
43+
self.type = type if type
44+
self.constraints = constraints if constraints
45+
self.valid_values = valid_values if valid_values
46+
self.elements_type = elements_type if elements_type
47+
self.optional = optional if optional
4348
end
4449

4550
# Check if an object is equals to the current instance.
@@ -49,13 +54,12 @@ def initialize(name = nil, description = nil, required = nil, type = nil,
4954
# @return [Boolean] true if all the attributes are equal and false otherwise.
5055
def ==(other)
5156
other.instance_of?(Definitions::RequestElement) &&
52-
self.name == other.name && self.description == other.description &&
53-
self.required == other.required && self.type == other.type &&
54-
self.constraints == other.constraints &&
55-
self.valid_values == other.valid_values &&
56-
self.elements_type == other.elements_type
57+
name == other.name && description == other.description &&
58+
required == other.required && type == other.type &&
59+
constraints == other.constraints &&
60+
valid_values == other.valid_values &&
61+
elements_type == other.elements_type && optional == other.optional
5762
end
58-
5963
end
6064
end
6165
end

lib/angus/definitions/response_element.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module Angus
22
module SDoc
33
class Definitions::ResponseElement
4-
54
# @!attribute [rw] name
65
# @return [String] the name of the response element.
76
attr_accessor :name
@@ -26,16 +25,20 @@ class Definitions::ResponseElement
2625
# @return [String] the elements type of the response element.
2726
attr_accessor :elements_type
2827

28+
# @!attribute [rw] optional
29+
# @return [Boolean] true if the element is optional
30+
attr_accessor :optional
31+
2932
def initialize(name = nil, description = nil, required = nil, type = nil, default = nil,
30-
elements_type = nil)
33+
elements_type = nil, optional = nil)
3134
@name = name
3235
@description = description
3336
@required = required
3437
@type = type
3538
@default = default
3639
@elements_type = elements_type
40+
@optional = optional
3741
end
38-
3942
end
4043
end
4144
end

lib/angus/definitions_reader.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
module Angus
22
module SDoc
33
class DefinitionsReader
4-
5-
OPERATIONS = 'operations'
6-
CONFIGURATIONS = %w{service proxy_operations representations glossary messages}
4+
OPERATIONS = 'operations'.freeze
5+
CONFIGURATIONS =
6+
%w[service proxy_operations representations glossary messages optional].freeze
77

88
class << self
9-
109
# Returns the tree of objects for the service.
1110
#
1211
# @param [String] base_path The base path of the YML files.
@@ -214,6 +213,7 @@ def build_representations(representations_hash)
214213
field.type = field_hash['type']
215214
field.required = field_hash['required']
216215
field.elements_type = field_hash['elements_type']
216+
field.optional = field_hash['optional']
217217
field
218218
end
219219

@@ -273,6 +273,7 @@ def build_operations(operations_hash, messages)
273273
request_element.constraints = element_hash['constraints']
274274
request_element.valid_values = element_hash['valid_values']
275275
request_element.elements_type = element_hash['elements_type']
276+
request_element.optional = element_hash['optional']
276277

277278
request_element
278279
end
@@ -306,6 +307,7 @@ def build_operations(operations_hash, messages)
306307
response_element.type = element_hash['type']
307308
response_element.default = element_hash['default']
308309
response_element.elements_type = element_hash['elements_type']
310+
response_element.optional = element_hash['optional']
309311

310312
response_element
311313
end
@@ -329,9 +331,7 @@ def build_glossary(glossary_hash)
329331
Angus::SDoc::Definitions::Glossary.new(glossary_hash)
330332
end
331333
private :build_glossary
332-
333334
end
334-
335335
end
336336
end
337337
end

lib/angus/sdoc/json_formatter.rb

+46-43
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def glossary_for_json(glossary_definition)
4848
# @option return [String] :description
4949
def glossary_term_for_json(glossary_term_definition)
5050
{
51-
:long_name => glossary_term_definition.long_name,
52-
:description => glossary_term_definition.description
51+
long_name: glossary_term_definition.long_name,
52+
description: glossary_term_definition.description
5353
}
5454
end
5555

@@ -65,10 +65,10 @@ def glossary_term_for_json(glossary_term_definition)
6565
# @option return [Integer] :status_code
6666
def message_for_json(message_definition)
6767
{
68-
:key => message_definition.key,
69-
:description => message_definition.description,
70-
:level => message_definition.level,
71-
:status_code => message_definition.status_code
68+
key: message_definition.key,
69+
description: message_definition.description,
70+
level: message_definition.level,
71+
status_code: message_definition.status_code
7272
}
7373
end
7474

@@ -88,41 +88,41 @@ def message_for_json(message_definition)
8888
# @option return [Array<(see .message_for_json)>] :messages
8989
def operation_for_json(operation_definition)
9090
data = {
91-
:name => operation_definition.name,
92-
:description => operation_definition.description,
93-
:path => operation_definition.path,
94-
:method => operation_definition.http_method
91+
name: operation_definition.name,
92+
description: operation_definition.description,
93+
path: operation_definition.path,
94+
method: operation_definition.http_method
9595
}
9696

9797
if operation_definition.uri_elements && !operation_definition.uri_elements.empty?
9898
uri_elements = operation_definition.uri_elements.map do |uri_element|
9999
uri_element_for_json(uri_element)
100100
end
101-
data.merge!({ :uri => uri_elements })
101+
data.merge!({ uri: uri_elements })
102102
end
103103

104104
if operation_definition.request_elements && !operation_definition.request_elements.empty?
105105
request_elements = operation_definition.request_elements.map do |request_element|
106106
request_element_for_json(request_element)
107107
end
108108

109-
data.merge!({ :request => request_elements })
109+
data.merge!({ request: request_elements })
110110
end
111111

112112
if operation_definition.response_elements && !operation_definition.response_elements.empty?
113113
response_elements = operation_definition.response_elements.map do |response_element|
114114
response_element_for_json(response_element)
115115
end
116116

117-
data.merge!({:response => response_elements})
117+
data.merge!({ response: response_elements })
118118
end
119119

120120
if operation_definition.messages && !operation_definition.messages.empty?
121121
messages = operation_definition.messages.map do |message|
122122
message_for_json(message)
123123
end
124124

125-
data.merge!({:messages => messages})
125+
data.merge!({ messages: messages })
126126
end
127127

128128
data
@@ -139,9 +139,9 @@ def operation_for_json(operation_definition)
139139
# @option return [String] :service
140140
def proxy_operation_for_json(proxy_operation_definition)
141141
{
142-
:path => proxy_operation_definition.path,
143-
:method => proxy_operation_definition.http_method,
144-
:service => proxy_operation_definition.service_name
142+
path: proxy_operation_definition.path,
143+
method: proxy_operation_definition.http_method,
144+
service: proxy_operation_definition.service_name
145145
}
146146
end
147147

@@ -170,17 +170,18 @@ def representation_for_json(representation_definition)
170170
# @option return [String] :elements_type
171171
def representation_field_for_json(representation_field_definition)
172172
data = {
173-
:field => representation_field_definition.name,
174-
:description => representation_field_definition.description,
175-
:required => representation_field_definition.required
173+
field: representation_field_definition.name,
174+
description: representation_field_definition.description,
175+
required: representation_field_definition.required,
176+
optional: representation_field_definition.optional
176177
}
177178

178179
if representation_field_definition.type
179-
data.merge!({:type => representation_field_definition.type})
180+
data.merge!({ type: representation_field_definition.type })
180181
end
181182

182183
if representation_field_definition.elements_type
183-
data.merge!({:elements_type => representation_field_definition.elements_type})
184+
data.merge!({ elements_type: representation_field_definition.elements_type })
184185
end
185186

186187
data
@@ -199,17 +200,18 @@ def representation_field_for_json(representation_field_definition)
199200
# @option return [String] :elements_type
200201
def request_element_for_json(request_element_definition)
201202
data = {
202-
:element => request_element_definition.name,
203-
:description => request_element_definition.description,
204-
:required => request_element_definition.required
203+
element: request_element_definition.name,
204+
description: request_element_definition.description,
205+
required: request_element_definition.required,
206+
optional: request_element_definition.optional
205207
}
206208

207209
if request_element_definition.type
208-
data.merge!({:type => request_element_definition.type})
210+
data.merge!({ type: request_element_definition.type })
209211
end
210212

211213
if request_element_definition.elements_type
212-
data.merge!({:elements_type => request_element_definition.elements_type})
214+
data.merge!({ elements_type: request_element_definition.elements_type })
213215
end
214216

215217
data
@@ -228,17 +230,18 @@ def request_element_for_json(request_element_definition)
228230
# @option return [String] :elements_type
229231
def response_element_for_json(response_element_definition)
230232
data = {
231-
:element => response_element_definition.name,
232-
:description => response_element_definition.description,
233-
:required => response_element_definition.required
233+
element: response_element_definition.name,
234+
description: response_element_definition.description,
235+
required: response_element_definition.required,
236+
optional: response_element_definition.optional
234237
}
235238

236239
if response_element_definition.type
237-
data.merge!({:type => response_element_definition.type})
240+
data.merge!({ type: response_element_definition.type })
238241
end
239242

240243
if response_element_definition.elements_type
241-
data.merge!({:elements_type => response_element_definition.elements_type})
244+
data.merge!({ elements_type: response_element_definition.elements_type })
242245
end
243246

244247
data
@@ -282,16 +285,16 @@ def service_for_json(service_definition)
282285
end
283286

284287
{
285-
:service => {
286-
:service => service_definition.name,
287-
:code_name => service_definition.code_name,
288-
:version => service_definition.version
288+
service: {
289+
service: service_definition.name,
290+
code_name: service_definition.code_name,
291+
version: service_definition.version
289292
},
290-
:operations => operations,
291-
:proxy_operations => proxy_operations,
292-
:representations => representations,
293-
:messages => messages,
294-
:glossary => glossary_for_json(service_definition.glossary)
293+
operations: operations,
294+
proxy_operations: proxy_operations,
295+
representations: representations,
296+
messages: messages,
297+
glossary: glossary_for_json(service_definition.glossary)
295298
}
296299
end
297300

@@ -305,8 +308,8 @@ def service_for_json(service_definition)
305308
# @option return [String] :description
306309
def uri_element_for_json(uri_element_definition)
307310
{
308-
:element => uri_element_definition.name,
309-
:description => uri_element_definition.description
311+
element: uri_element_definition.name,
312+
description: uri_element_definition.description
310313
}
311314
end
312315

0 commit comments

Comments
 (0)