Class: Grape::Validations::ParamsScope

Inherits:
Object
  • Object
show all
Includes:
DSL::Parameters
Defined in:
lib/grape/validations/params_scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL::Parameters

#all_or_none_of, #at_least_one_of, #exactly_one_of, #group, #mutually_exclusive, #optional, #params, #requires, #use

Constructor Details

#initialize(opts, &block) ⇒ ParamsScope

Returns a new instance of ParamsScope.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/grape/validations/params_scope.rb', line 8

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) if block_given?

  configure_declared_params
end

Instance Attribute Details

#elementObject

Returns the value of attribute element.



4
5
6
# File 'lib/grape/validations/params_scope.rb', line 4

def element
  @element
end

#parentObject

Returns the value of attribute parent.



4
5
6
# File 'lib/grape/validations/params_scope.rb', line 4

def parent
  @parent
end

Instance Method Details

#full_name(name) ⇒ Object



27
28
29
30
# File 'lib/grape/validations/params_scope.rb', line 27

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

#required?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/grape/validations/params_scope.rb', line 36

def required?
  !@optional
end

#root?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/grape/validations/params_scope.rb', line 32

def root?
  !@parent
end

#should_validate?(parameters) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/grape/validations/params_scope.rb', line 21

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