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.
Instance Method Summary collapse
-
#full_name(name) ⇒ 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.
-
#nested? ⇒ Boolean
A nested scope is contained in one of its parent’s elements.
- #parent_index ⇒ Object
-
#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, #declared_param?, #exactly_one_of, #given, #mutually_exclusive, #optional, #params, #requires, #use
Constructor Details
#initialize(opts) { ... } ⇒ ParamsScope
Open up a new ParamsScope, allowing parameter definitions per
Grape::DSL::Params.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/grape/validations/params_scope.rb', line 21 def initialize(opts, &block) @element = opts[:element] @parent = opts[:parent] @api = opts[:api] @optional = opts[:optional] || false @type = opts[:type] @dependent_on = opts[:dependent_on] @declared_params = [] 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 |
Instance Method Details
#full_name(name) ⇒ String
Returns the proper attribute name, with nesting considered.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/grape/validations/params_scope.rb', line 47 def full_name(name) case when nested? # Find our containing element's name, and append ours. "#{@parent.full_name(@element)}#{parent_index}[#{name}]" when 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.
80 81 82 |
# File 'lib/grape/validations/params_scope.rb', line 80 def lateral? @parent && !@element end |
#nested? ⇒ Boolean
A nested scope is contained in one of its parent’s elements.
73 74 75 |
# File 'lib/grape/validations/params_scope.rb', line 73 def nested? @parent && @element end |
#parent_index ⇒ Object
62 63 64 |
# File 'lib/grape/validations/params_scope.rb', line 62 def parent_index "[#{@parent.index}]" if @parent.present? && @parent.index.present? end |
#required? ⇒ Boolean
Returns whether or not this scope needs to be present, or can be blank.
86 87 88 |
# File 'lib/grape/validations/params_scope.rb', line 86 def required? !@optional end |
#root? ⇒ Boolean
Returns whether or not this scope is the root-level scope.
67 68 69 |
# File 'lib/grape/validations/params_scope.rb', line 67 def root? !@parent end |
#should_validate?(parameters) ⇒ Boolean
Returns whether or not this entire scope needs to be validated.
37 38 39 40 41 42 43 44 |
# File 'lib/grape/validations/params_scope.rb', line 37 def should_validate?(parameters) return false if @optional && params(parameters).respond_to?(:all?) && params(parameters).all?(&:blank?) @dependent_on.each do |dependency| return false if params(parameters).try(:[], dependency).blank? end if @dependent_on return true if parent.nil? parent.should_validate?(parameters) end |