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



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

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



87
88
89
# File 'lib/grape/validations.rb', line 87

def element
  @element
end

#parentObject

Returns the value of attribute parent



87
88
89
# File 'lib/grape/validations.rb', line 87

def parent
  @parent
end

Instance Method Details

#full_name(name) ⇒ Object



165
166
167
168
# File 'lib/grape/validations.rb', line 165

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

#group(element, &block) ⇒ Object



135
136
137
# File 'lib/grape/validations.rb', line 135

def group(element, &block)
  requires(element, &block)
end

#optional(*attrs, &block) ⇒ Object



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

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



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/grape/validations.rb', line 139

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



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

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

#should_validate?(parameters) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
# File 'lib/grape/validations.rb', line 102

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



153
154
155
156
157
158
159
160
161
# File 'lib/grape/validations.rb', line 153

def use(*names)
  named_params = @api.settings[:named_params] || {}
  names.each do |name|
    params_block = named_params.fetch(name) do
      raise "Params :#{name} not found!"
    end
    instance_eval(&params_block)
  end
end