Module: Grape::DSL::Parameters

Extended by:
ActiveSupport::Concern
Included in:
Validations::ParamsScope
Defined in:
lib/grape/dsl/parameters.rb

Instance Method Summary collapse

Instance Method Details

#at_least_one_of(*attrs) ⇒ Object



56
57
58
# File 'lib/grape/dsl/parameters.rb', line 56

def at_least_one_of(*attrs)
  validates(attrs, at_least_one_of: true)
end

#exactly_one_of(*attrs) ⇒ Object



52
53
54
# File 'lib/grape/dsl/parameters.rb', line 52

def exactly_one_of(*attrs)
  validates(attrs, exactly_one_of: true)
end

#group(*attrs, &block) ⇒ Object



60
61
62
# File 'lib/grape/dsl/parameters.rb', line 60

def group(*attrs, &block)
  requires(*attrs, &block)
end

#mutually_exclusive(*attrs) ⇒ Object



48
49
50
# File 'lib/grape/dsl/parameters.rb', line 48

def mutually_exclusive(*attrs)
  validates(attrs, mutual_exclusion: true)
end

#optional(*attrs, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/grape/dsl/parameters.rb', line 36

def optional(*attrs, &block)
  orig_attrs = attrs

  validations = {}
  validations.merge!(attrs.pop) if attrs.last.is_a?(Hash)
  validations[:type] ||= Array if block_given?
  validates(attrs, validations)

  block_given? ? new_scope(orig_attrs, true, &block) :
      push_declared_params(attrs)
end

#params(params) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/grape/dsl/parameters.rb', line 64

def params(params)
  params = @parent.params(params) if @parent
  if @element
    if params.is_a?(Array)
      params = params.flat_map { |el| el[@element] || {} }
    elsif params.is_a?(Hash)
      params = params[@element] || {}
    else
      params = {}
    end
  end
  params
end

#requires(*attrs, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/grape/dsl/parameters.rb', line 21

def requires(*attrs, &block)
  orig_attrs = attrs.clone

  opts = attrs.last.is_a?(Hash) ? attrs.pop : nil

  if opts && opts[:using]
    require_required_and_optional_fields(attrs.first, opts)
  else
    validate_attributes(attrs, opts, &block)

    block_given? ? new_scope(orig_attrs, &block) :
        push_declared_params(attrs)
  end
end

#use(*names) ⇒ Object Also known as: use_scope, includes



8
9
10
11
12
13
14
15
16
17
# File 'lib/grape/dsl/parameters.rb', line 8

def use(*names)
  named_params = @api.settings[:named_params] || {}
  options = names.last.is_a?(Hash) ? names.pop : {}
  names.each do |name|
    params_block = named_params.fetch(name) do
      raise "Params :#{name} not found!"
    end
    instance_exec(options, &params_block)
  end
end