Module: Apipie::DSL::Param
- Included in:
- Concern, Controller, ResourceDescriptionDsl, ResponseDescription, ResponseDescription::ResponseObject, Validator::HashValidator
- Defined in:
- lib/apipie/dsl_definition.rb
Overview
this describes the params, it’s in separate module because it’s used in Validators as well
Instance Method Summary collapse
-
#_default_param_group_scope ⇒ Object
where the group definition should be looked up when no scope given.
-
#param(param_name, validator, desc_or_options = nil, options = {}, &block) ⇒ Object
Describe method’s parameter.
-
#param_group(name, scope_or_options = nil, options = {}) ⇒ Object
Reuses param group for this method.
-
#property(param_name, validator, desc_or_options = nil, options = {}, &block) ⇒ Object
:doc:.
-
#returns(pgroup_or_options, desc_or_options = nil, options = {}, &block) ⇒ Object
Describe possible responses.
Instance Method Details
#_default_param_group_scope ⇒ Object
where the group definition should be looked up when no scope given. This is expected to return a controller.
447 448 449 |
# File 'lib/apipie/dsl_definition.rb', line 447 def _default_param_group_scope self end |
#param(param_name, validator, desc_or_options = nil, options = {}, &block) ⇒ Object
Describe method’s parameter
Example:
param :greeting, String, :desc => "arbitrary text", :required => true
def hello_world(greeting)
puts greeting
end
345 346 347 348 349 350 351 352 |
# File 'lib/apipie/dsl_definition.rb', line 345 def param(param_name, validator, = nil, = {}, &block) #:doc: return unless Apipie.active_dsl? _apipie_dsl_data[:params] << [param_name, validator, , .merge(:param_group => @_current_param_group), block] end |
#param_group(name, scope_or_options = nil, options = {}) ⇒ Object
Reuses param group for this method. The definition is looked up in scope of this controller. If the group was defined in different controller, the second param can be used to specify it. when using action_aware params, you can specify :as => :create or :update to explicitly say how it should behave
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/apipie/dsl_definition.rb', line 366 def param_group(name, = nil, = {}) if .is_a? Hash .merge!() scope = [:scope] else scope = end scope ||= _default_param_group_scope @_current_param_group = { :scope => scope, :name => name, :options => , :from_concern => scope.apipie_concern? } self.instance_exec(&Apipie.get_param_group(scope, name)) ensure @_current_param_group = nil end |
#property(param_name, validator, desc_or_options = nil, options = {}, &block) ⇒ Object
:doc:
354 355 356 357 358 359 |
# File 'lib/apipie/dsl_definition.rb', line 354 def property(param_name, validator, = nil, = {}, &block) #:doc: return unless Apipie.active_dsl? [:only_in] ||= :response [:required] = true if [:required].nil? param(param_name, validator, , , &block) end |
#returns(pgroup_or_options, desc_or_options = nil, options = {}, &block) ⇒ Object
Describe possible responses
Example:
def_param_group :user do
param :user, Hash do
param :name, String
end
end
returns :user, "the speaker"
returns "the speaker" do
param_group: :user
end
returns :param_group => :user, "the speaker"
returns :user, :code => 201, :desc => "the created speaker record"
returns :array_of => :user, "many speakers"
def hello_world
render json: {user: {name: "Alfred"}}
end
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/apipie/dsl_definition.rb', line 406 def returns(, =nil, ={}, &block) #:doc: return unless Apipie.active_dsl? if .is_a? Hash .merge!() elsif !.nil? [:desc] = end if .is_a? Hash .merge!() else [:param_group] = end code = [:code] || 200 scope = [:scope] || _default_param_group_scope descriptor = [:param_group] || [:array_of] if block.nil? if descriptor.is_a? ResponseDescriptionAdapter adapter = descriptor elsif descriptor.respond_to? :describe_own_properties adapter = ResponseDescriptionAdapter.from_self_describing_class(descriptor) else begin block = Apipie.get_param_group(scope, descriptor) if descriptor rescue raise "No param_group or self-describing class named #{descriptor}" end end elsif descriptor raise "cannot specify both block and param_group" end _apipie_dsl_data[:returns][code] = [, scope, block, adapter] end |