Class: Grape::Validations::ParamsScope
- Inherits:
-
Object
- Object
- Grape::Validations::ParamsScope
- Includes:
- DSL::Parameters
- Defined in:
- lib/grape/validations/params_scope.rb
Instance Attribute Summary collapse
-
#element ⇒ Object
Returns the value of attribute element.
-
#index ⇒ Object
Returns the value of attribute index.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #brackets(val) ⇒ Object
-
#full_name(name, index: nil) ⇒ String
The proper attribute name, with nesting considered.
-
#initialize(opts) { ... } ⇒ ParamsScope
constructor
Open up a new ParamsScope, allowing parameter definitions per Grape::DSL::Params.
-
#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.
- #meets_dependency?(params, request_params) ⇒ Boolean
-
#nested? ⇒ Boolean
A nested scope is contained in one of its parent’s elements.
-
#required? ⇒ Boolean
Whether or not this scope needs to be present, or can be blank.
-
#root? ⇒ Boolean
Whether or not this scope is the root-level scope.
-
#should_validate?(parameters) ⇒ Boolean
Whether or not this entire scope needs to be validated.
Methods included from DSL::Parameters
#all_or_none_of, #at_least_one_of, #build_with, #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.
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
#element ⇒ Object
Returns the value of attribute element.
4 5 6 |
# File 'lib/grape/validations/params_scope.rb', line 4 def element @element end |
#index ⇒ Object
Returns the value of attribute index.
4 5 6 |
# File 'lib/grape/validations/params_scope.rb', line 4 def index @index end |
#parent ⇒ Object
Returns the value of attribute parent.
4 5 6 |
# File 'lib/grape/validations/params_scope.rb', line 4 def parent @parent end |
#type ⇒ Object (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
#brackets(val) ⇒ Object
85 86 87 |
# File 'lib/grape/validations/params_scope.rb', line 85 def brackets(val) "[#{val}]" if val end |
#full_name(name, index: nil) ⇒ String
Returns the proper attribute name, with nesting considered.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/grape/validations/params_scope.rb', line 71 def full_name(name, index: nil) if nested? # Find our containing element's name, and append ours. [@parent.full_name(@element), [@index || index, name].map(&method(:brackets))].compact.join elsif lateral? # Find the name of the element as if it was at the same nesting level # as our parent. We need to forward our index upward to achieve this. @parent.full_name(name, index: @index) 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.
103 104 105 |
# File 'lib/grape/validations/params_scope.rb', line 103 def lateral? @parent && !@element end |
#meets_dependency?(params, request_params) ⇒ Boolean
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/grape/validations/params_scope.rb', line 48 def meets_dependency?(params, request_params) if @parent.present? && !@parent.meets_dependency?(@parent.params(request_params), request_params) return false end return true unless @dependent_on return params.any? { |param| meets_dependency?(param, request_params) } if params.is_a?(Array) params = params.with_indifferent_access @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.try(:[], dependency_key)) elsif params.respond_to?(:key?) && params.try(:[], dependency).blank? return false end end true end |
#nested? ⇒ Boolean
A nested scope is contained in one of its parent’s elements.
96 97 98 |
# File 'lib/grape/validations/params_scope.rb', line 96 def nested? @parent && @element end |
#required? ⇒ Boolean
Returns whether or not this scope needs to be present, or can be blank.
109 110 111 |
# File 'lib/grape/validations/params_scope.rb', line 109 def required? !@optional end |
#root? ⇒ Boolean
Returns whether or not this scope is the root-level scope.
90 91 92 |
# File 'lib/grape/validations/params_scope.rb', line 90 def root? !@parent end |
#should_validate?(parameters) ⇒ Boolean
Returns whether or not this entire scope needs to be validated.
41 42 43 44 45 46 |
# File 'lib/grape/validations/params_scope.rb', line 41 def should_validate?(parameters) return false if @optional && (params(parameters).blank? || all_element_blank?(parameters)) return false unless meets_dependency?(params(parameters), parameters) return true if parent.nil? parent.should_validate?(parameters) end |