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, #declared_param?, #exactly_one_of, #given, #map_params, #mutually_exclusive, #optional, #params, #requires, #use, #with

Constructor Details

#initialize(opts) { ... } ⇒ ParamsScope

Open up a new ParamsScope, allowing parameter definitions per Grape::DSL::Params.

Options Hash (opts):

  • :element (Symbol)

    the element that contains this scope; for this to be relevant, @parent must be set

  • :parent (ParamsScope)

    the scope containing this scope

  • :api (API)

    the API endpoint to modify

  • :optional (Boolean)

    whether or not this scope needs to have any parameters set or not

  • :type (Class)

    a type meant to govern this scope (deprecated)

  • :type (Hash)

    group options for this scope

  • :dependent_on (Symbol)

    if present, this scope should only validate if this param is present in the parent scope

Yields:

  • the instance context, open for parameter definitions



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/grape/validations/params_scope.rb', line 23

def initialize(opts, &block)
  @element      = opts[:element]
  @parent       = opts[:parent]
  @api          = opts[:api]
  @optional     = opts[:optional] || false
  @type         = opts[:type]
  @group        = opts[:group] || {}
  @dependent_on = opts[:dependent_on]
  @declared_params = []
  @index = nil

  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

#indexObject

Returns the value of attribute index



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

def index
  @index
end

#parentObject

Returns the value of attribute parent



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

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type



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

def type
  @type
end

Instance Method Details

#array_indexObject



74
75
76
# File 'lib/grape/validations/params_scope.rb', line 74

def array_index
  "[#{@index}]" if @index.present?
end

#full_name(name) ⇒ String



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/grape/validations/params_scope.rb', line 60

def full_name(name)
  if nested?
    # Find our containing element's name, and append ours.
    "#{@parent.full_name(@element)}#{array_index}[#{name}]"
  elsif lateral?
    # Find the name of the element as if it was at the
    # same nesting level as our parent.
    @parent.full_name(name)
  else
    # We must be the root scope, so no prefix needed.
    name.to_s
  end
end

#lateral?Boolean

A lateral scope is subordinate to its parent, but its keys are at the same level as its parent and thus is not contained within an element.



92
93
94
# File 'lib/grape/validations/params_scope.rb', line 92

def lateral?
  @parent && !@element
end

#nested?Boolean

A nested scope is contained in one of its parent's elements.



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

def nested?
  @parent && @element
end

#required?Boolean



98
99
100
# File 'lib/grape/validations/params_scope.rb', line 98

def required?
  !@optional
end

#root?Boolean



79
80
81
# File 'lib/grape/validations/params_scope.rb', line 79

def root?
  !@parent
end

#should_validate?(parameters) ⇒ Boolean



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

def should_validate?(parameters)
  return false if @optional && (params(parameters).blank? ||
                                 any_element_blank?(parameters))

  @dependent_on.each do |dependency|
    if dependency.is_a?(Hash)
      dependency_key = dependency.keys[0]
      proc = dependency.values[0]
      return false unless proc.call(params(parameters).try(:[], dependency_key))
    elsif params(parameters).try(:[], dependency).blank?
      return false
    end
  end if @dependent_on

  return true if parent.nil?
  parent.should_validate?(parameters)
end