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

#all_or_none_of(*attrs) ⇒ Object



92
93
94
# File 'lib/grape/dsl/parameters.rb', line 92

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

#at_least_one_of(*attrs) ⇒ Object



88
89
90
# File 'lib/grape/dsl/parameters.rb', line 88

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

#exactly_one_of(*attrs) ⇒ Object



84
85
86
# File 'lib/grape/dsl/parameters.rb', line 84

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

#mutually_exclusive(*attrs) ⇒ Object



80
81
82
# File 'lib/grape/dsl/parameters.rb', line 80

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

#optional(*attrs, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/grape/dsl/parameters.rb', line 58

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

  opts = attrs.last.is_a?(Hash) ? attrs.pop.clone : {}
  type = opts[:type]

  # check type for optional parameter group
  if attrs && block_given?
    fail Grape::Exceptions::MissingGroupTypeError.new if type.nil?
    fail Grape::Exceptions::UnsupportedGroupTypeError.new unless [Array, Hash].include?(type)
  end

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

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

#params(params) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/grape/dsl/parameters.rb', line 98

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 Also known as: group



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/grape/dsl/parameters.rb', line 42

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

  opts = attrs.last.is_a?(Hash) ? attrs.pop.clone : {}
  opts[:presence] = true

  if 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

Include reusable params rules among current. You can define reusable params with helpers method.

Examples:


class API < Grape::API
  helpers do
    params :pagination do
      optional :page, type: Integer
      optional :per_page, type: Integer
    end
  end

  desc "Get collection"
  params do
    use :pagination
  end
  get do
    Collection.page(params[:page]).per(params[:per_page])
  end
end


29
30
31
32
33
34
35
36
37
38
# File 'lib/grape/dsl/parameters.rb', line 29

def use(*names)
  named_params = Grape::DSL::Configuration.stacked_hash_to_hash(@api.namespace_stackable(:named_params)) || {}
  options = names.last.is_a?(Hash) ? names.pop : {}
  names.each do |name|
    params_block = named_params.fetch(name) do
      fail "Params :#{name} not found!"
    end
    instance_exec(options, &params_block)
  end
end