Class: 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.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #full_name(name) ⇒ Object
-
#initialize(opts, &block) ⇒ ParamsScope
constructor
A new instance of ParamsScope.
- #required? ⇒ Boolean
- #root? ⇒ Boolean
- #should_validate?(parameters) ⇒ Boolean
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
#element ⇒ Object
Returns the value of attribute element.
4 5 6 |
# File 'lib/grape/validations/params_scope.rb', line 4 def element @element 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) ⇒ 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
36 37 38 |
# File 'lib/grape/validations/params_scope.rb', line 36 def required? !@optional end |
#root? ⇒ Boolean
32 33 34 |
# File 'lib/grape/validations/params_scope.rb', line 32 def root? !@parent end |
#should_validate?(parameters) ⇒ 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 |