Class: Grape::Validations::ParamsScope

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/validations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, &block) ⇒ ParamsScope

Returns a new instance of ParamsScope.


87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/grape/validations.rb', line 87

def initialize(opts, &block)
  @element  = opts[:element]
  @parent   = opts[:parent]
  @api      = opts[:api]
  @optional = opts[:optional] || false
  @type     = opts[:type]
  @declared_params = []

  instance_eval(&block)

  configure_declared_params
end

Instance Attribute Details

#elementObject

Returns the value of attribute element


85
86
87
# File 'lib/grape/validations.rb', line 85

def element
  @element
end

#parentObject

Returns the value of attribute parent


85
86
87
# File 'lib/grape/validations.rb', line 85

def parent
  @parent
end

Instance Method Details

#exactly_one_of(*attrs) ⇒ Object


137
138
139
# File 'lib/grape/validations.rb', line 137

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

#full_name(name) ⇒ Object


172
173
174
175
# File 'lib/grape/validations.rb', line 172

def full_name(name)
  return "#{@parent.full_name(@element)}[#{name}]" if @parent
  name.to_s
end

#group(*attrs, &block) ⇒ Object


141
142
143
# File 'lib/grape/validations.rb', line 141

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

#mutually_exclusive(*attrs) ⇒ Object


133
134
135
# File 'lib/grape/validations.rb', line 133

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

#optional(*attrs, &block) ⇒ Object


121
122
123
124
125
126
127
128
129
130
131
# File 'lib/grape/validations.rb', line 121

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


145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/grape/validations.rb', line 145

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


106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/grape/validations.rb', line 106

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

#root?Boolean

Returns:

  • (Boolean)

177
178
179
# File 'lib/grape/validations.rb', line 177

def root?
  !@parent
end

#should_validate?(parameters) ⇒ Boolean

Returns:

  • (Boolean)

100
101
102
103
104
# File 'lib/grape/validations.rb', line 100

def should_validate?(parameters)
  return false if @optional && params(parameters).respond_to?(:all?) && params(parameters).all?(&:blank?)
  return true if parent.nil?
  parent.should_validate?(parameters)
end

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


159
160
161
162
163
164
165
166
167
168
# File 'lib/grape/validations.rb', line 159

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