Module: ApipieDSL::Parameter

Included in:
Method, ReturnDescription::ReturnObject, Validator::HashValidator
Defined in:
lib/apipie_dsl/dsl.rb

Constant Summary collapse

SUPPORTED_TYPES =
%i[required optional keyword block].freeze

Instance Method Summary collapse

Instance Method Details

#block(desc_or_options = nil, options = {}, &block) ⇒ Object



119
120
121
122
123
# File 'lib/apipie_dsl/dsl.rb', line 119

def block(desc_or_options = nil, options = {}, &block)
  options[:type] = :block
  name = options[:name] || :block
  param(name, Proc, desc_or_options, options)
end

#default_param_group_scopeObject

Where the group definition should be looked up when no scope given. This is expected to return a class.



161
162
163
# File 'lib/apipie_dsl/dsl.rb', line 161

def default_param_group_scope
  class_scope
end

#define_param_group(name, &block) ⇒ Object



133
134
135
# File 'lib/apipie_dsl/dsl.rb', line 133

def define_param_group(name, &block)
  ApipieDSL.define_param_group(class_scope, name, &block)
end

#keyword(name, validator, desc_or_options = nil, options = {}, &block) ⇒ Object



114
115
116
117
# File 'lib/apipie_dsl/dsl.rb', line 114

def keyword(name, validator, desc_or_options = nil, options = {}, &block)
  options[:type] = :keyword
  param(name, validator, desc_or_options, options, &block)
end

#list(name, desc_or_options = nil, options = {}) ⇒ Object Also known as: splat, rest



125
126
127
128
129
# File 'lib/apipie_dsl/dsl.rb', line 125

def list(name, desc_or_options = nil, options = {})
  options[:type] = :optional
  options[:default] ||= 'empty list'
  param(name, :rest, desc_or_options, options)
end

#optional(name, validator, desc_or_options = nil, options = {}, &block) ⇒ Object



109
110
111
112
# File 'lib/apipie_dsl/dsl.rb', line 109

def optional(name, validator, desc_or_options = nil, options = {}, &block)
  options[:type] = :optional
  param(name, validator, desc_or_options, options, &block)
end

#param(name, validator, desc_or_options = nil, options = {}, &block) ⇒ Object

Describe method’s parameter

Example:

param :greeting, String, :desc => "arbitrary text", :type => :required
def hello_world(greeting)
  puts greeting
end


96
97
98
99
100
101
102
# File 'lib/apipie_dsl/dsl.rb', line 96

def param(name, validator, desc_or_options = nil, options = {}, &block)
  dsl_data[:params] << [name,
                        validator,
                        desc_or_options,
                        options.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 class. If the group was defined in different class, the second param can be used to specify it.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/apipie_dsl/dsl.rb', line 140

def param_group(name, scope_or_options = nil, options = {})
  if scope_or_options.is_a?(Hash)
    options.merge!(scope_or_options)
    scope = options[:scope]
  else
    scope = scope_or_options
  end
  scope ||= default_param_group_scope

  @current_param_group = {
    scope: scope,
    name: name,
    options: options
  }
  instance_exec(&ApipieDSL.get_param_group(scope, name))
ensure
  @current_param_group = nil
end

#required(name, validator, desc_or_options = nil, options = {}, &block) ⇒ Object



104
105
106
107
# File 'lib/apipie_dsl/dsl.rb', line 104

def required(name, validator, desc_or_options = nil, options = {}, &block)
  options[:type] = :required
  param(name, validator, desc_or_options, options, &block)
end